【问题标题】:How To Redirect If Cookie Does Not Equal Vairable如果 Cookie 不等于变量,如何重定向
【发布时间】:2015-04-05 09:16:29
【问题描述】:

如果 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,则留在同一页面上。对不起,如果听起来很复杂。

它不像这段代码那样工作:无论变量等于什么,它都不会重定向。谢谢

【问题讨论】:

  • 你能解释一下吗? "random.com/test.php?id=17" and the cookie equals 18 the script should redirect 当你的脚本说如果相等它应该重定向。你能复习一下你的问题吗?

标签: php redirect cookies


【解决方案1】:

您是否正在寻找这样的东西。如果是这样,它应该适用于您的情况:

<?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
}
?>

【讨论】:

    【解决方案2】:

    只有在发送到客户端后才会应用标头。如果您想立即重定向,您可以将 exit(0) 放在 header(...) 之后,在这种情况下,您将停止执行脚本并将当前标头发送到将重定向您的浏览器。

    if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id) {
        header("Location: test.php");
        exit(0);
    }
    
    //continues the script
    

    【讨论】:

      【解决方案3】:

      问题是您将isset(结果)的“”与您的GET参数$id进行比较:

      if(isset($_COOKIE['logged_in']) == $id)
      

      这句话的意思是“确定是否设置了$_COOKIE['logged_in'],并将该确定与$id 进行比较”。 PHP 将评估isset,它返回truefalse(正如它在documentation 中所说),并将truefalse 与表达式的另一侧(==)进行比较,意思是$id,给定你的例子,它永远不会匹配。如果您查询“random.com/test.php?id=true”(或 false)可能会满足您的需求。

      您所拥有的行 not 的意思是“确定是否设置了 $_COOKIE['logged_in'] 并将 $_COOKIE['logged_in'] 的值与 的值进行比较> $id",我相信这就是您要找的。在这种情况下,您要做的是首先检查$_COOKIE['logged_in'] 是否已设置,然后 检查$_COOKIE['logged_in'] 是否与$id 匹配,如下所示:

      if (isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id)
      

      如果这没有意义,这里有一个非常明确的版本,可能更清楚实际发生了什么:

      if ((isset($_COOKIE['logged_in']) == true) && ($_COOKIE['logged_in'] == $id))
      

      希望对您有所帮助。

      【讨论】:

        【解决方案4】:

        您应该添加另一个条件。

         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 
        }
        

        【讨论】:

          猜你喜欢
          • 2015-12-30
          • 2013-04-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-07-04
          • 1970-01-01
          • 2021-09-21
          • 1970-01-01
          相关资源
          最近更新 更多