问题描述
我本质上是在尝试制作一个登录系统,让用户返回到他们所在的页面.我知道有人问过这个问题,我已经查看了 SO 上的其他答案,但我找不到解决我的特定问题的方法.
I'm essentially trying to make a login system that will return the user back to the page they were on. I know this question has been asked a bit, and I've looked at the other answers on SO, but I cannot find a solution to my particular problem.
我的网站有一个包含参考 ID 号的表格(例如:10001、10003、10004、... 53401 等).这些数字也是链接.所有链接都指向一个页面(mypage.php"),并且参考 ID 号(10004)成为该 url 的查询字符串:
My site has has a table with reference id numbers (ex: 10001, 10003, 10004, ... 53401, etc.). These numbers are also links. All links point to one page ("mypage.php"), and the reference id number (10004) becomes a query string to that url:
<td><?php echo '<a href="mypage.php?query_ecr=', urlencode($num), '">'; ?><?php echo $num; ?></a></td>
在我的header.php"上,它位于网站的每个页面上,菜单上有一个按钮,可以打开下面的表单供用户登录.
On my "header.php", which is on every page of the site, there is a button on the menu that will open the form below for the user to log in.
<form action='login/process.php' method='post'>
<label for='name'>Username:</label>
<input type='text' id='userid' name='user_name'/>
<label for='password'>Password:</label>
<input type='password' name='password' id='userpassword'/>
<input type='submit' value='Log In' />
<input type='hidden' name='login' value='1'>
<input type='hidden' value='".$_SERVER['PHP_SELF']."' name='redirurl'/>
</form>
注意 name='redirurl' 的隐藏输入".我想捕获用户所在的当前页面.我已经有一个登录脚本,可以检查用户名和密码并将它们重定向到他们需要的页面.
Notice the "hidden input" with the name='redirurl'. I want to capture the current page the user is on. I already have a login script that will check the username and password and redirect them to the page they need to be.
//login/process.php
...blah blah blah other stuff...
global $database,$session;
$this->user_status=$database->CheckUserPass($_POST['user_name'],$_POST['password']);
$url = $_POST['redirurl'];
if($this->user_status==1)
{
$session->StartSession($_POST['user_name'],$_POST['password']);
header("Location: ".$url);
} else {
...blah blah blah.....
}
我的问题是,如果 GUEST 点击链接(例如:10004),他们将被带到网址:
My problem is that if the GUEST clicks on a link (ex: 10004), they are taken to the url:
http://www.XXXXXX.XXXX/mypage.php?query_ecr='10004'
但是,在该页面上,'redirurl' 的值为:
However, on that page the value for 'redirurl' is:
http://www.XXXXXX.XXXX/mypage.php
它忽略查询字符串.因此,当用户从该页面登录,并且我的登录脚本将他们重定向回该页面时,该页面会出现大量错误,因为它需要查询字符串.
It disregards the query string. So when the user logs in from that page, and my login script re-directs them back to that page, the page has a ton of errors because it needs the query string.
如何在 $_SERVER['PHP_SELF'] 中包含查询字符串?
How do I include the query string in: $_SERVER['PHP_SELF'] ?
推荐答案
您可以使用:
$_SERVER['REQUEST_URI']
获取域名文件夹、查询字符串等之后的所有内容.然后,您可以使用其他 $_SERVER 变量来添加域,或者只要您不更改主机,就可以在没有域名的情况下使用.
to get everything after the domain name folders, query strings etc. You could then either use the other $_SERVER variables to add the domain, or it should be fine to use without a domain name provided you're not changing hosts.
这篇关于php $_SERVER['PHP_SELF'] 包含查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!