php if/else 未按预期工作
我是 PHP 新手,不明白为什么它不能正常工作。我想要发生的是,如果找到会话,则执行网页,否则重定向到 login.php。发生的情况是网页正在执行,用户看到 sql 错误,然后发生重定向。就好像 if 和 else 都在执行一样。请帮忙。
<?php
if (isset($_SESSION['userID']))
{
$mainUID=$_SESSION['userID'];
?>
<body>
The entire webpage is in here but removed it for a short example.
</body>
</html>
<?
}
else
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login.php">';
}
?>
I am new to PHP and cannot figure out why this is not working properly. What I want to happen is if a session is found execute the web page otherwise redirect to login.php. What is happening is the webpage is being executed and the user sees sql errors then the redirect happens. It's as if both the if and the else are being executed. Please help.
<?php
if (isset($_SESSION['userID']))
{
$mainUID=$_SESSION['userID'];
?>
<body>
The entire webpage is in here but removed it for a short example.
</body>
</html>
<?
}
else
{
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login.php">';
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更好的方法:
在使用会话之前检查你的大括号并执行 session_start
a nicer way:
check your braces and do session_start before using the session
如果您在网页中使用 if else 语句,您可能会发现这种格式更容易:
但是您的代码不起作用的原因是您混淆了大括号。
检查此示例并将其与您的代码进行比较:
If you are using if else statements within a webpage you may find this format easier:
But the reason why your code isn't working is because you have got your curly braces mixed up.
Examine this example and compare it with your code:
试试这段代码,你的第一个括号是关闭而不是打开
Try this code your first curtly bracket was closing instead of opening