session variables not working php

session variables not working php  using -'php,session,session-variables'

Here are the code of my login page where the login script checks for the authenticity of the user and then redirects to inbox page using header function.


// Some query processing on database    

if(($id_user_fetched<=$id_max_fetched) && ($id_user_fetched!=0)){
$_SESSION['loggedIn'] = 'yes';
    //echo 'Login Successful';
        echo 'Invalid Login';
        echo'<br /> <a href="index.html">Click here to try again</a>';
    echo mysqli_error("Login Credentials Incorrect!");

The inbox.php page looks like this:

echo 'SESSION ='.$_SESSION['loggedIn'];
if($_SESSION['loggedIn'] != 'yes'){
echo $message = 'you must log in to see this page.';


Now with the above code, the inbox.php always shows the output:
 SESSION=you must log in to see this page.
Which means that either the session variable is not being setup or the inbox.php is unable to retrieve the session variable. Where am i going wrong?

asked Sep 14, 2015 by MickeyBearde
0 votes

3 Answers

0 votes
  1. Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening tag before anything else. Also ensure there are no whitespaces/tabs before the opening tag.
  2. After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();)
  3. Make sure cookies are enabled in the browser you are using to test it on.
  4. Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo(). Refer to [this][1] as to how to turn it off.
  5. Make sure you didn't delete or empty the session
  6. Make sure the key in your $_SESSION superglobal array is not overwritten anywhere
  7. Make sure you redirect to the same domain. So redirecting from a to doesn't carry the session forward.
  8. Make sure your file extension is .php (it happens!)

PHP session lost after redirect

answered Sep 14, 2015 by CatHouston
0 votes

I had the same issue for a while and had a very hard time figuring it out. My problem was that I had the site working for a while with the sessions working right, and then all of the sudden everything broke.

Apparently, your session_save_path(), for me it was /var/lib/php5/, needs to have 777 chmod permissions. I accidentally changed it, breaking sessions completely.

To fix, just do sudo chmod -R 777 /var/lib/php5/ (or whatever your session_save_path() is) on linux.

answered Sep 14, 2015 by BrianneLarue
0 votes

Just talked to the hosting service, it was an issue at their end. he said " your account session.save_path was not set as a result issue arise. I set it for you now."

And it works fine after that :)

answered Sep 14, 2015 by ChaOUI