Jump to content

[SOLVED] Redirect if not logged in


abch624

Recommended Posts

Hi Guys,

 

I want to put an if statement on all pages that should not be viewed if the user is not logged in. I use this

if (empty($_SESSION['accountDetailsID']) || empty($_SESSION['email'])) {

Header("Location:index.php");
exit;
}

 

On my login page I have a create session part that is like this

// Register $email, $mypassword and redirect to file "viewprofile.php"
session_start();
session_register("email");
session_register("mypassword");
$_SESSION['accountDetailsID']=$accountDetailsID;
$_SESSION['email']=$email;
header("location:viewprofile.php");
die();
}

 

Now what happens is: Even though I am logged in I can not navigate to the viewprofile.php and it just redirects me back to index.php page

 

Please help - Thanks

Link to comment
https://forums.phpfreaks.com/topic/112478-solved-redirect-if-not-logged-in/
Share on other sites

First, please echo both sessions to see if they truly are empty.

Also make sure when you register the sessions the values given to them are not empty. Make sure you are using session_start for all pages that require sessions validation.

 

Also, why is there a bracket at the end of the 2nd script, it tells me there is an if statement, if so please post the entire script.

 

thanks.

 

edit: Replace empty with if(isset($_SESSION['accountDetailsID'])){

Hi,

 

This now works. The problem was this: I had written the if statement

if (empty($_SESSION['accountDetailsID']) || empty($_SESSION['email'])) {

Header("Location:index.php");
exit;
}

before the session start statement.

 

But now it is like this

session_start();
if (empty($_SESSION['accountDetailsID']) || empty($_SESSION['email'])) {

Header("Location:index.php");
exit;
}

 

All works

 

and ya you spoted it right there was an if statement.  :P

Cheers - Zahid

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.