登录后引用URL重定向

时间:2016-04-29 22:57:58

标签: php

我有以下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;
}
 ?>

此代码适用于简单的xml登录脚本。这只是记录下来的人,如果该人来自需要登录的页面,但是一个人不是,我希望在网址栏中添加引荐来源,并且在登录成功后,将用户重定向到该引用的URL。但是,这现在给我一个错误500.请帮助......

2 个答案:

答案 0 :(得分:1)

试试这个:

<?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();

希望这会有所帮助......

答案 1 :(得分:0)

  

我想在用户登录后将用户重定向到引荐网址   成功

假设用户已成功登录,您可以检查是否已成功登录 设置$_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");
}