我正在尝试编写一个脚本,根据会话状态和页面URL中的ID(例如www.example.com/profile.php?id=1)更改变量内容,这样就可以了显示一组内容,如果他们登录并查看别人的个人资料,另一个如果有登录和自己的个人资料,另一个如果登录并查看别人的个人资料。
首先,脚本从url获取ID:
if (isset($_GET['id'])) {
$id = preg_replace('#[^0-9]#i', '', $_GET['id']); // filter everything but numbers
} else if (isset($_SESSION['idx'])) {
$id = $logOptions_id;
} else {
header("location: index.php");
exit();
}
然后它运行一些我不会包含的其他代码,然后是这段代码:
// ------- DECIDES WHAT TO DISOPLAY, DEPENDING ON VERIABLES ---------
if (isset($_SESSION['idx']) && $logOptions_id == $id) { // If session is set and ID matches the profiles ID
$content = ""Your viewing your own profile";
} else if (isset($_SESSION['idx']) && $logOptions_id != $id) { // If SESSION is set, but ID dosent match profiles ID
$follow_option = "Your viewing someone elses profile";
} else {
$content = "Your are not logged in";
}
// ------- END DECIDES WHAT TO DISOPLAY, DEPENDING ON VERIABLES ---------
print $content;
现在我的问题是,它所做的只是显示登录和查看别人个人资料的选项“你正在查看别人的资料”。如果您发现任何导致此问题的错误,请在下面回答。谢谢! :)
答案 0 :(得分:1)
当$logOptions_id != $id
运行时,您的变量不会保持预期值,或者您忘记启动会话。我没有看到$ logOptions_id被分配的参考。使用IDE工具调试代码。