简体   繁体   English

登录后引用URL重定向

[英]Referring URL Redirect after login

I have the below PHP code: 我有以下PHP代码:

<?php

// let's add a ref url feature for quick guidance.

$ref = $_SERVER['HTTP_REFERER'];

$actual_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?ref=$ref";

header("Location: $actual_link");

if (empty($ref)) {
    # do nothing...
} else {
    $redirect_to_ref = header("Location: $ref");
    header("Location: test.php");
}

}

?>
<?php

function checkRef() {
    if (isset($redirect_to_ref)) {
        $redirect_to_ref;
    } else {
        header("Location: index.php");
    }
}

$error = false;
if(isset($_POST['login'])){
    $username = htmlspecialchars($_POST['username']);
    $password = md5($_POST['password']);
    if(file_exists('users/' . $username . '.xml')){
        $xml = new SimpleXMLElement('users/' . $username . '.xml', 0, true);
        if($password == $xml->password){
            session_start();
            $_SESSION['username'] = $username;
            checkRef();
            die;
        }
    }
    $error = true;
}
 ?>

This code is for a simple xml login script. 此代码用于简单的xml登录脚本。 This will just log the person in, and if the person came from a page that required one to be logged in, but one wasn't, I would like it to add the referrer in the url bar, and upon login success, redirect the user to that referred url. 这将只登录该人,如果该人来自要求登录的页面,但不是,则希望将其添加到网址栏中,并在登录成功后重定向用户到该引用网址。 However, this is now giving me an error 500. Please help... 但是,这现在给我一个错误500。请帮助...

Try this: 尝试这个:

<?php
//GLOBAL FUNCTION TO GET THE CURRENT URL:
function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

// USE SESSION TO SET A VARIABLE FOR THE REF. URL:
if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
    session_start();
}
if(isset($_SESSION['ref_url'])){
    header("Location: " . $_SESSION['ref_url']);
    unset($_SESSION['ref_url']);
}



//IN ALL OTHER PAGES; SET THE URL OF THE CURRENT PAGE TO THE $_SESSION['ref_url'] VARIABLE LIKE SO
if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
    session_start();
}
$_SESSION['ref_url'] = curPageURL();

Hope this helps... 希望这可以帮助...

I want to redirect users to the referring url after they login successfully 我想在用户成功登录后将其重定向到引荐网址

Assuming the user already logged in successfully, you can check if $_SERVER['HTTP_REFERER'] is set and redirect accordingly, ie: 假设用户已经成功登录,则可以检查$_SERVER['HTTP_REFERER']是否设置并进行相应的重定向,即:

$ref = $_SERVER['HTTP_REFERER'];
$actual_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?ref=$ref";

if(isset($ref)){
    header("Location: $ref");
}else{
   //this doesn't make sense since the redirection will be made to the same page and $ref is empty. 
   //Think about something else when HTTP_REFERER isn't set. 
   //header("Location: $actual_link");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM