Liam
Liam

Reputation: 9855

If user isn't logged in, then redirect PHP

I'm working on a site that I didn't build. I'm trying to put an HTML redirect inside an if statement, Here's the source:

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

I want to put a redirect within here: if the user isn't logged in, they're redirected to the homepage, I've tried the following with no luck. Can anybody see where I'm going wrong?

<?php 

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

?>

Upvotes: 0

Views: 269

Answers (2)

A_nto2
A_nto2

Reputation: 1096

Try to give the full URL, and close the } bracket :

<?php 

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

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53535

You forgot to close the bracket }

should be:

<?php 

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

Upvotes: 1

Related Questions