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 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题