dqy27359 2016-11-26 22:44
浏览 96
已采纳

PHP If(!isset(...)||!isset(...))

I'm trying to check if the first $_COOKIE['one'] exists OR the second $_COOKIE['two'] exists and if none exists to redirect the user.

Only one of those two cookies are going to exist when this script is running.

if (!isset($_COOKIE['one']) || !isset($_COOKIE['two'])) {
    header('Location: ./');
} else {
    ...
}

I tried many things but every time I get into this if altough one of those cookies always exist.

  • 写回答

3条回答 默认 最新

  • dtb81443 2016-11-26 22:48
    关注

    This is a simple case of inverted logic. As Mark pointed out, you need to be using the boolean && (AND) operator. You are trying to see if both don't exist, then send the header. Currently, if either exists, you send the header anyway.

    Just change if (!isset($_COOKIE['one']) || !isset($_COOKIE['two'])) {

    to

    if (!isset($_COOKIE['one']) && !isset($_COOKIE['two'])) {

    Or (||) returns true if either the left or right of the statement is true. And (&&) returns true only if both parts of the statement are true. The Not(!) operator reverses true->false and false-> true.

    isset tells you if the cookie exists. If the cookie exists, it returns true. You are correct in using not on this, as you want it to tell you if the cookie doesn't exist (opposite). However, you only want to send the header if BOTH cookies don't exist. Or will send it if one doesn't exist.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀