php if/else 未按预期工作

发布于 2024-12-05 00:04:42 字数 435 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天荒地未老 2024-12-12 00:04:42

更好的方法:

<?php
session_start();
if (!isset($_SESSION['userID'])){
    header("Location: /login.php");
}
$mainUID=intval($_SESSION['userID']);

?>
<html>
<body>
The entire webpage is in here but removed it for a short example.
</body>
</html>

在使用会话之前检查你的大括号并执行 session_start

a nicer way:

<?php
session_start();
if (!isset($_SESSION['userID'])){
    header("Location: /login.php");
}
$mainUID=intval($_SESSION['userID']);

?>
<html>
<body>
The entire webpage is in here but removed it for a short example.
</body>
</html>

check your braces and do session_start before using the session

泪之魂 2024-12-12 00:04:42

如果您在网页中使用 if else 语句,您可能会发现这种格式更容易:

<?php if(true): ?>
    HTML Here
<?php else: ?>
    HTML Here
<?php endif; ?>

但是您的代码不起作用的原因是您混淆了大括号。

检查此示例并将其与您的代码进行比较:

if(true) {
    // Do stuff
}
else {
    // Do stuff
}

If you are using if else statements within a webpage you may find this format easier:

<?php if(true): ?>
    HTML Here
<?php else: ?>
    HTML Here
<?php endif; ?>

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:

if(true) {
    // Do stuff
}
else {
    // Do stuff
}
泪意 2024-12-12 00:04:42

试试这段代码,你的第一个括号是关闭而不是打开

<?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">'; 
}
?>

Try this code your first curtly bracket was closing instead of opening

<?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">'; 
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文