如果 Cookie 不等于变量,如何重定向
How To Redirect If Cookie Does Not Equal Vairable
如果 cookie 不等于可用值,我很难重定向用户。如果它确实等于变量,那么它应该继续脚本。这是我要重定向的代码:
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) == $id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) != $id)
{
//continues the script
请注意if语句中的变量($id)是来自url查询的变量;例如,如果 url 是 "random.com/test.php?id=17" 并且 cookie 等于 18,脚本应该重定向。但是,如果 url 是 "random.com/test.php?id=17" 并且 cookie 等于 17,则留在同一页面上。对不起,如果听起来很复杂。
此代码不起作用:无论变量等于什么,它都不会重定向。谢谢
你在找这样的东西吗?如果是这样,它应该适用于您的情况:
<?php
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']!=$id)
{
//continues the script
}
?>
A headers 仅在发送给客户端后才会应用。如果你想立即重定向,你可以在 header(...) 之后放置 exit(0) 在这种情况下你将停止执行脚本并将当前 headers 发送到将重定向的浏览器你.
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id) {
header("Location: test.php");
exit(0);
}
//continues the script
问题是您正在比较 isset
(结果)的“值”与 值你的 GET 参数,$id
:
if(isset($_COOKIE['logged_in']) == $id)
这表示的是"determine if $_COOKIE['logged_in']
is set and compare that determination to $id
"。 PHP 将评估 isset
,其中 returns true
或 false
(如 documentation 中所述),并比较 true
或 false
到表达式的另一边 (==
),意思是 $id
,这将永远不会匹配您的示例。如果您查询 "random.com/test.php?id=true"(或 false)可能会满足您的要求。
您的行 不是 的意思是 "determine if $_COOKIE['logged_in']
is set and compare the value of $_COOKIE['logged_in']
to the value of $id
",我相信这就是您要查找的内容。在这种情况下,您要做的是首先检查 $_COOKIE['logged_in']
是否设置为 ,然后 检查 值 是否为 [=15] =] 匹配 $id
,像这样:
if (isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)
如果这没有意义,这里有一个非常明确的版本,可能更清楚实际发生的事情:
if ((isset($_COOKIE['logged_in']) == true) && ($_COOKIE['logged_in'] == $id))
希望对您有所帮助。
您应该添加另一个条件。
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] != $id)
{
//continues the script
或者使用这个脚本
if(isset($_COOKIE['logged_in']))
{
if($_COOKIE['logged_in']==$id){
header("Location: test.php");
}
else{
//another condition to equal is not equal so directly we can use else
//continues the script
}
} else {
echo "Cookie not valid or available";
// redirect user
}
如果 cookie 不等于可用值,我很难重定向用户。如果它确实等于变量,那么它应该继续脚本。这是我要重定向的代码:
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) == $id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) != $id)
{
//continues the script
请注意if语句中的变量($id)是来自url查询的变量;例如,如果 url 是 "random.com/test.php?id=17" 并且 cookie 等于 18,脚本应该重定向。但是,如果 url 是 "random.com/test.php?id=17" 并且 cookie 等于 17,则留在同一页面上。对不起,如果听起来很复杂。
此代码不起作用:无论变量等于什么,它都不会重定向。谢谢
你在找这样的东西吗?如果是这样,它应该适用于您的情况:
<?php
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']!=$id)
{
//continues the script
}
?>
A headers 仅在发送给客户端后才会应用。如果你想立即重定向,你可以在 header(...) 之后放置 exit(0) 在这种情况下你将停止执行脚本并将当前 headers 发送到将重定向的浏览器你.
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id) {
header("Location: test.php");
exit(0);
}
//continues the script
问题是您正在比较 isset
(结果)的“值”与 值你的 GET 参数,$id
:
if(isset($_COOKIE['logged_in']) == $id)
这表示的是"determine if $_COOKIE['logged_in']
is set and compare that determination to $id
"。 PHP 将评估 isset
,其中 returns true
或 false
(如 documentation 中所述),并比较 true
或 false
到表达式的另一边 (==
),意思是 $id
,这将永远不会匹配您的示例。如果您查询 "random.com/test.php?id=true"(或 false)可能会满足您的要求。
您的行 不是 的意思是 "determine if $_COOKIE['logged_in']
is set and compare the value of $_COOKIE['logged_in']
to the value of $id
",我相信这就是您要查找的内容。在这种情况下,您要做的是首先检查 $_COOKIE['logged_in']
是否设置为 ,然后 检查 值 是否为 [=15] =] 匹配 $id
,像这样:
if (isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)
如果这没有意义,这里有一个非常明确的版本,可能更清楚实际发生的事情:
if ((isset($_COOKIE['logged_in']) == true) && ($_COOKIE['logged_in'] == $id))
希望对您有所帮助。
您应该添加另一个条件。
if(empty($_GET)) {
//No variables are specified in the URL.
//Do stuff accordingly
echo "No variables specified in URL...";
} else {
//Variables are present. Do stuff:
$id = htmlspecialchars($_GET["id"]);
echo 'url query is ' . $id;
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)
{
header("Location: test.php");
}
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] != $id)
{
//continues the script
或者使用这个脚本
if(isset($_COOKIE['logged_in']))
{
if($_COOKIE['logged_in']==$id){
header("Location: test.php");
}
else{
//another condition to equal is not equal so directly we can use else
//continues the script
}
} else {
echo "Cookie not valid or available";
// redirect user
}