dqa35710 2015-04-05 09:16 采纳率: 0%
浏览 32
已采纳

如果Cookie不等于变量,如何重定向

I am struggling to redirect the user if the cookie does not equal a vairable. If it does equal the vairable, then it should continue the script. Here is my code to redirect :

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

Please note that the vairable in the if statment ($id) is a vairable from the query of url; for example if the url is, "random.com/test.php?id=17" and the cookie equals 18 the script should redirect. However if url is, "random.com/test.php?id=17" and the cookie equals 17, then stay on the same page. Sorry if it sounds complecated.

It doesnt work as this code: It doesnt redirect no matter what the vairable equals. Thanks

  • 写回答

4条回答 默认 最新

  • dongxieting9623 2015-04-05 11:02
    关注

    you should add another condition.

     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
    

    or use this 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 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息