我有一段代码无法正常工作。
if ($_SESSION['active'] != 2 && $_SESSION['org_id'] == 0 && $_SESSION['part_id'] == 0)
{
header('Location: index');
die();
}
代码必须执行以下操作,它会检查会话。如果'active'不是2且org_id为0且part_id也为零,则必须自动转到index.php。最后一部分不起作用。因为我测试了它并且它保持在同一页面上,但我的SESSION给出的数组就是这个
"Array ( [notify] => OK [user] => [email protected] [user_id] => 346 [fnln] => test2 [type] => 4 [org_id] => 108 [part_id] => 79 [active] => 2 )"
所以它必须工作。我不知道出了什么问题......
您编写的if语句不会将您重定向到任何地方,因为它没有满足它的条件。
在你的数组中,active
是2,在你的if语句中你说active
不能是2.而org_id
不是0而part_id
不是0,你也说它必须是。
看不出有什么问题。如果您希望被重定向,那么您在代码中做错了。
如果你想让它重定向到index.php
,那么你必须写
header('location:index.php');
根据您的示例数据,这可以按预期工作:
<?php
$data =
[
'notify' => 'OK',
'user' => '[email protected]',
'user_id' => 346,
'fnln' => 'test2',
'type' => 4,
'org_id' => 108,
'part_id' => 79,
'active' => 2
];
if (
$data['active'] != 2
&& $data['org_id'] == 0
&& $data['part_id'] == 0
)
{
echo 'Redirect here.';
} else {
echo 'No redirect.';
}
输出:
No redirect.
你为什么不这么想?
使用上面给出的数据样本,第一个条件$data['active'] != 2
将评估为false。它将在那里短路而不进一步检查,因此更宽的条件将评估为假。