douyong1908 2016-04-29 22:57
浏览 22
已采纳

登录后引用URL重定向

I have the below PHP code:

<?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. 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...

  • 写回答

2条回答 默认 最新

  • duanouyong4228 2016-04-29 23:55
    关注

    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...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题