如何在PHP中像SO那样实现returnurl? [英] How to implement the returnurl like SO in PHP?

查看:145
本文介绍了如何在PHP中像SO那样实现returnurl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它也应该在重写URL后起作用.

It should also work after url rewriting.

有可能吗?否则我只能对所有内容进行硬编码.

Is it possible?Otherwise I can only hardcode everything.

推荐答案

如果您尝试创建具有重定向功能的登录系统,则需要使用HTTP_REFERER标头来查找用户的来源,以便将其发送回.最好的方法是,在您的登录表单中使用以下内容:

If you are trying to create a login system with redirection you need to use the HTTP_REFERER header to find out where the user came from so you can send him back there. The best way to do this is, in your login form use something like this:

//- Login.php
<?php
    if ($_POST['username'] && $_POST['password'])
    {
        //- Run username and password verifying script
        header("Location: ".$_POST['returnurl']);
    }
    $RedirectURL = $_SERVER['HTTP_REFERER'];
?>
<form action="/login.php" method="post">
    <input type="text" name="username" />
    <input type="password" name="password" />
    <input type="hidden" name="returnurl" value="<?php echo $RedirectURL; ?>" />
</form>

请注意,某些ISP,浏览器或代理将删除引用标头,因此您不能依靠它来为每个用户完美地工作.如果使用if语句检查标头是否存在,则可以将这些用户发送回首页或其他任何内容.

Please note that some ISPs, browsers or proxies will strip out the referer header, so you can't rely on it to work perfectly for every user. If you use an if statement to check whether the header exists or not, you can send those users back to the homepage or whatever.

编辑

如果您要使用$ _SERVER ['REQUEST_URI'],则在运行Apache时应将其设置为漂亮的URL.如果您运行的是IIS,则需要检查$ _SERVER ['X_HTTP_ORIGINAL_URL'].像这样:

If you're looking to use $_SERVER['REQUEST_URI'] it should be set to the pretty URL after a rewrite if you're running Apache. If you're running IIS, in which case you need to check for $_SERVER['X_HTTP_ORIGINAL_URL']. Something like this:

<?php
    if (!isset($_SERVER['X_HTTP_ORIGINAL_URL']))
        $ReturnURL = $_SERVER['X_HTTP_ORIGINAL_URL'];
    else
        $ReturnURL = $_SERVER['REQUEST_URI'];

    header("Location: /login.php?returnurl=".$ReturnURL);
?>

//- Login.php
<?php
    if ($_POST['username'] && $_POST['password'])
    {
        //- Run username and password verifying script
        header("Location: ".$_POST['returnurl']);
    }
    $RedirectURL = isset($_GET['returnurl'] ? $_GET['returnurl'] : "/index.php";
?>
<form action="/login.php" method="post">
    <input type="text" name="username" />
    <input type="password" name="password" />
    <input type="hidden" name="returnurl" value="<?php echo $RedirectURL; ?>" />
</form>

这篇关于如何在PHP中像SO那样实现returnurl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆