Session variables not working php

php session not working between pages
php 7.2 session not working
isset($_session php)
php check if session variable exists
php session array
php session not working after redirect
php session not working on server
how to get value from session array in php

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.

<?php
session_start();

include_once('config.php');
$user=htmlentities(stripslashes($_POST['username']));
$password=htmlentities(stripslashes($_POST['password']));
// Some query processing on database    

if(($id_user_fetched<=$id_max_fetched) && ($id_user_fetched!=0)){
$_SESSION['loggedIn'] = 'yes';
    header("Location:http://xyz/inbox.php?u=$id_user_fetched");
    //echo 'Login Successful';
    }else{
        echo 'Invalid Login';
        echo'<br /> <a href="index.html">Click here to try again</a>';
        }
}else{
    echo mysqli_error("Login Credentials Incorrect!");
    }
?>

The inbox.php page looks like this:

<?php
session_start(); 
echo 'SESSION ='.$_SESSION['loggedIn'];
if($_SESSION['loggedIn'] != 'yes'){
echo $message = 'you must log in to see this page.';
//header('location:login.php');
}
 //REST OF THE CODE

?>

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?

  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 <?php tag before anything else. Also ensure there are no whitespaces/tabs before the opening <?php 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 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 www.yourdomain.com to yourdomain.com doesn't carry the session forward.
  8. Make sure your file extension is .php (it happens!).

PHP session lost after redirect

Session variables not working, please help! - PHP, Hi all, I have been working is a website for offering stuff. I have been dealing with a problem from a while, and now I know why it is happening, but not how to  Sounds to me like session_start() is sometimes being called after output (anything echo'd out or not inside php tags). Sir_Arcturua August 30, 2014, 8:46pm #3

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 correct permissions (the user running php, eg www-data needs write access to the directory). I accidentally changed it, breaking sessions completely.

Run sudo chmod -R 700 /var/lib/php5/ and then sudo chown -R www-data /var/lib/php5/ so that the php user has access to the folder.

PHP: $_SESSION, An associative array containing session variables available to the current script. Session variables with a single number will not work, however "1a" will work,  Good day. Please i have a problem that sounds funny. I have a php app with a logn/ folder and a porta/ folder. When i login, it redirects to index.php in the portal/ folder but no session is set. But

Maybe if your session path is not working properly you can try session.save_path(path/to/any folder); function as alternative path. If it works you can ask your hosting provider about default path issue.

session_start - Manual, The problem with this, is that each time you call session_start(), PHP prints a duplicate copy of session_start(); //open the session again for editing a variable Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

If you use a connection script, dont forget to use session_start(); at the connection too, had some trouble before noticing that issue.

$_SESSION[] variables not holding from page to page, I've had problems with temporary files being stored in php default directories. My best advice is to use db sessions (id is stored in cookie,  If they are you should set the INI value session.cookie_domain to .DOMAIN.EXT. To further debug this whole situation, do some simple cookie watching. See if PHPSESSID is present as a cookie on both page requests, if it's not then this is your problem. You can't store cookies cross-domain unless you reconstruct them.

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 :)

[SOLVED] php session not working, sess2.php with the following code: <?php // this starts the session session_start(); // echo variable from the session, we set this on our other  This is the code for session.php but it is not working properly. I have three field in my database log_id,user_email,user_pass. and when I want to login into my website it does not works. I've some issues with session creation.

PHP $_SESSION variable not saving across pages, I've been having some big problems with this all day. I'm trying to save a SESSION variable and then print it onto another PHP file however the  For example, you cannot create a connection to a database and store the connection id as a session variable and expect the connection to still be valid the next time the session is restored. PHP functions that return a resource are identified by having a return type of resource in their function definition.

PHP - Sessions, Make use of isset() function to check if session variable is already set or not. Put this code in a test.php file and load this file many times to see the result −. From this page, we will access the session information we set on the first page ("demo_session1.php"). Notice that session variables are not passed individually to each new page, instead they are retrieved from the session we open at the beginning of each page ( session_start () ).

Php session variables not saving, I am trying to use session variables in php but they don't seem to be saved across page loads. Maybe I am misunderstanding something, if so  The function session_start is not being called properly. This is the most common cause of session data not saving. It is also the easiest to fix. Basically, PHP’s session_start function MUST be called before any session data is saved or any output is sent to the browser.

Comments
  • It should be echo "Login Credentials Incorrect!" ; not echo mysqli_error("Login Credentials Incorrect!");
  • Can you debug to confirm that the conditions of your if statement evaluates to true? (i.e echo a statement in the true and false block to see which one is firing)
  • 1. do you call both scripts from the same domain? 2. do you use any kind of session_name, session_id, session_set_cookie? 3. session_start returns value, do you check it?
  • @Crackertastic Yes i can assure you that they evaluate to true. checked several times
  • These are two else statements for one if
  • could it possibly be a server error? i tried the most basic test examples as under: session.php <?php session_start(); // store session data $_SESSION['views']=1; header('Location:session2.php'); ?> session2.php: <?php session_start(); //retrieve session data echo "Pageviews=". $_SESSION['views']; ?>
  • I have same problem. Cause: there was no free disk space!
  • same problem for me. i try to print the page using google cloud print but the session are closed/cleared. how to fix that
  • Regarding #4: This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0. php.net/manual/en/security.globals.php
  • For me this problem is caused by no disk space, as Cuarculu. I suggest revo to add this cause in his list. Thanks
  • 777 is a bad set of permissions for the session save folder. You need to change the ownership of the folder to allow the web server to write to it and prevent all other access.
  • HOOOOLY SHIT man, all the best in your life, I was literally crying because I couldn't get session onto the next page.