当 url 包含参数时,header() 在 php 中不起作用?
header() not working in php when the url contains parameters?
所以我正在使用 $_GET
捕获 URL 以便稍后使用,但是当我使用 $_GET
时它不会重定向!
所以这是我的示例代码:
URL : http://localhost/project/active.php/?s=ieugfshd&h=qwuyrbcq&i=1
php代码:
<?php
include 'init.php';
$s = trim($_GET['s']);
$h = trim($_GET['h']);
$i = trim($_GET['i']);
$q = key_check($s,$h,$i);
if($q == 1)
{
header("location:password_active.php");
exit;
}
if($q == 0)
{
header("location:login_failed.php");
exit;
}
?>
编辑:
key_check( ) 函数
function key_check($k1,$k2,$id)
{
$query = mysql_query("select key1 from users where user_id = '$id'");
$key1 =mysql_result($query,0);
$query = mysql_query("select key2 from users where user_id = '$id'");
$key2 =mysql_result($query,0);
$y=strcmp($k1,$key1);
$z=strcmp($k2,$key2);
if($y || $z == 0)
{
return 1;
}
else
{
return 0;
}
}
现在,当我尝试这个时,我得到了“1”,但我得到了
This web page has a redirect loop
但是我的 password_active.php
没有任何重定向。这只是一个 html 页面。
尝试在 file.php 之后删除 /。喜欢 index.php?i=sa
您用来访问脚本的 URL 是:
http://localhost/project/active.php/?s=ieugfshd&h=qwuyrbcq&i=1
这会加载 active.php
,发挥其作用,然后尝试发送以下内容 header :
header("location:password_active.php");
浏览器收到此 header,并尝试通过在查询字符串(即 ?s=xxx
字符串)之前的最后一个斜杠后添加 password_active.php
来解析该相对 URL .
因此您的浏览器加载:
http://localhost/project/active.php/password_active.php?s=ieugfshd&h=qwuyrbcq&i=1
这会再次加载 active.php
,它会再次发挥作用,然后再次发送相同的 header,然后加载此页面:
http://localhost/project/active.php/password_active.php?s=ieugfshd&h=qwuyrbcq&i=1
再次。然后再次。然后再次。尝试几次后,您的浏览器会发现出了问题并停止了。
您应该在 HTTP header:
中使用绝对 URL
header("Location: /project/password_active.php");
此外,请注意根据标准应如何编写 HTTP headers。
随机笔记:
- 根据文件名,
$s
和$h
都是密码。您应该 hash them,而不是通过 URL. 传递它们
if($y || $z == 0)
不太可能像您想象的那样工作,因为它将在伪代码中被评估为 if y or not z
,而您可能希望 if not y and not z
用于密码检查。
此外,在 Location
header 之后调用 exit()
也有好处。你永远不应该忘记这一点,因为它非常重要,如果你忘记它们可能会给你的脚本带来一些麻烦。
所以我正在使用 $_GET
捕获 URL 以便稍后使用,但是当我使用 $_GET
时它不会重定向!
所以这是我的示例代码:
URL : http://localhost/project/active.php/?s=ieugfshd&h=qwuyrbcq&i=1
php代码:
<?php
include 'init.php';
$s = trim($_GET['s']);
$h = trim($_GET['h']);
$i = trim($_GET['i']);
$q = key_check($s,$h,$i);
if($q == 1)
{
header("location:password_active.php");
exit;
}
if($q == 0)
{
header("location:login_failed.php");
exit;
}
?>
编辑:
key_check( ) 函数
function key_check($k1,$k2,$id)
{
$query = mysql_query("select key1 from users where user_id = '$id'");
$key1 =mysql_result($query,0);
$query = mysql_query("select key2 from users where user_id = '$id'");
$key2 =mysql_result($query,0);
$y=strcmp($k1,$key1);
$z=strcmp($k2,$key2);
if($y || $z == 0)
{
return 1;
}
else
{
return 0;
}
}
现在,当我尝试这个时,我得到了“1”,但我得到了
This web page has a redirect loop
但是我的 password_active.php
没有任何重定向。这只是一个 html 页面。
尝试在 file.php 之后删除 /。喜欢 index.php?i=sa
您用来访问脚本的 URL 是:
http://localhost/project/active.php/?s=ieugfshd&h=qwuyrbcq&i=1
这会加载 active.php
,发挥其作用,然后尝试发送以下内容 header :
header("location:password_active.php");
浏览器收到此 header,并尝试通过在查询字符串(即 ?s=xxx
字符串)之前的最后一个斜杠后添加 password_active.php
来解析该相对 URL .
因此您的浏览器加载:
http://localhost/project/active.php/password_active.php?s=ieugfshd&h=qwuyrbcq&i=1
这会再次加载 active.php
,它会再次发挥作用,然后再次发送相同的 header,然后加载此页面:
http://localhost/project/active.php/password_active.php?s=ieugfshd&h=qwuyrbcq&i=1
再次。然后再次。然后再次。尝试几次后,您的浏览器会发现出了问题并停止了。
您应该在 HTTP header:
中使用绝对 URLheader("Location: /project/password_active.php");
此外,请注意根据标准应如何编写 HTTP headers。
随机笔记:
- 根据文件名,
$s
和$h
都是密码。您应该 hash them,而不是通过 URL. 传递它们
if($y || $z == 0)
不太可能像您想象的那样工作,因为它将在伪代码中被评估为if y or not z
,而您可能希望if not y and not z
用于密码检查。
此外,在 Location
header 之后调用 exit()
也有好处。你永远不应该忘记这一点,因为它非常重要,如果你忘记它们可能会给你的脚本带来一些麻烦。