在 php 中使用 $_SERVER['HTTP_REFERER'] 登录后无法重定向 [英] Unable to redirect after login in using $_SERVER['HTTP_REFERER'] in php

查看:79
本文介绍了在 php 中使用 $_SERVER['HTTP_REFERER'] 登录后无法重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的用户重定向到用户单击 login 的页面,因此我将以下代码添加到 config 文件,以便每个文件都可以访问 redirect代码

I want my user to redirected to page where user clicked login so i added following code to config file so that each and ever file has access to redirect code

config.php

if (isset($_SERVER['HTTP_REFERER']) && basename($_SERVER['HTTP_REFERER']) != basename($_SERVER['PHP_SELF'])) {
    $_SESSION['redirect_url'] = $_SERVER['HTTP_REFERER'];
}

这样,当我需要重定向时,我可以像下面这样简单调用

So that when ever i need a redirect i can simple call like bellow

header('location:' . $_SESSION['redirect_url']);
exit();

当我尝试上面的代码时,我得到错误显示

and when i try above code i get error showing

ERR_TOO_MANY_REDIRECTS

ERR_TOO_MANY_REDIRECTS

我什至尝试了 echo $_SESSION['redirect_url'] 并且它显示了一个有效的 requested URL

i even tried echo $_SESSION['redirect_url'] and it showed a valid requested URL

但它没有重定向.有人可以帮助我我做错了什么吗.

But it doesn't redirected. Can some one help me what wrong im doing.

这里是

.htaccess

RewriteEngine On

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

推荐答案

经过长期研究.

echo "<a href='/login.php?ref=". urlencode($_SERVER['REQUEST_URI']) ."'>login</a>";

在 login.php 中

and in login.php

if ($_GET['ref'] != '') {
    $url = $_GET['ref'];
} else {
    $url = "/";
}

if ($user->login($username, $password)) {
        $_SESSION['username'] = $username;
        header("location:http://" .$url);
        exit();
    }

我正在使用这种简单的方法重定向到请求的页面.

am using this simple method to redirect to requested page.

这篇关于在 php 中使用 $_SERVER['HTTP_REFERER'] 登录后无法重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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