如果用户不是';t登录,然后重定向PHP


If user isn't logged in, then redirect PHP

我在一个我没有构建的网站上工作。我试图在if语句中放入一个HTML重定向,这是源代码:

<?php 
    if(!$usersClass->checkLoggedIn()) { 
?>
// Stuff here
<?php } else { ?>
    // More stuff here
<?php } ?>

我想在这里放一个重定向:如果用户没有登录,他们会被重定向到主页,我尝试了以下操作,但没有成功。有人能看出我哪里错了吗?

<?php 
if(!$usersClass->checkLoggedIn()) { 
header("Location: /index.php");
?>

您忘记关闭括号}

应该是:

<?php 
if(!$usersClass->checkLoggedIn()) { 
  header("Location: /index.php");
}
?>

尝试给出完整的URL,并关闭}括号:

<?php 
if(!$usersClass->checkLoggedIn()) { 
header("location: http://localhost/yoursite/index.php");
}
?>