douhuang2218 2016-08-13 10:24
浏览 58

如何在PHP中设置cookie和重定向? [重复]

This question already has an answer here:

I want to set a cookie, if a user visits Page "Test-2". When the cookie is set and the visitor tries to visit Page "Test" within 24 hours he should be automatically redirected to Page "Test-2".

Here is the Code I inserted to the function.php file of my Wordpress Theme:

if( is_page('test-2’)){
    if(isset($_COOKIE['dz_offer'])){
        $cookie = $_COOKIE['dz_offer'];
    }
else{
    setcookie('dz_offer', time() + 60*60*24, '/');
    }
}

if( is_page('test‘)){   
    if (isset($_COOKIE['dz_offer‘])){
        header(„Location: https://esample.com/test-2“);
        exit;
    }
}

However now I've the following error: "Parse error: syntax error, unexpected 'dz_offer' (T_STRING)"

Any ideas how to fix this and get it working?

Thanks for your help!

+++ UPDATE +++

The Error is gone now. However the cookie isn't stored, when I visit page "test-2"

Here is the updated Code I'm using:

if( is_page('test-2')){
if(isset($_COOKIE['dz_offer'])){
    $cookie = $_COOKIE['dz_offer'];
}
else{
setcookie('dz_offer',$val, time() + 60*60*24, '/');
}
}

if( is_page('test')){   
if (isset($_COOKIE['dz_offer'])){
    header("Location: https://example.com/test-2");
    exit;
}
}
</div>
  • 写回答

1条回答 默认 最新

  • duanlian1978 2016-08-13 10:30
    关注

    There are a lot of errors. Everywhere you put a ` instead of '

    if( is_page('test-2')){
    if(isset($_COOKIE['dz_offer'])){
        $cookie = $_COOKIE['dz_offer'];
    }
    else{
    //cookies should be changed in the following way and $val is the value you have to save in the cookie 'dz_offer'.
    setcookie('dz_offer',$val, time() + 60*60*24);
    }
    }
    
    if( is_page('test')){   
    if (isset($_COOKIE['dz_offer'])){
        header("Location: https://esample.com/test-2");
        exit;
    }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看