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
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'])){

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.