php session variables getting lost in next page

php session not working between pages
php session array
isset($_session php)
how to overwrite session variable in php
php session variables not saving
php session id
php check if session variable exists
destroy session php

I am right now tired with this problem and I feel like leaving web programming. I have created a form and I have named the file 'index.php'. The code is here:

   <form action='login.php' method='post'><br>
<center><input type='text' class='form-control' style='width:320px;' placeholder='Username' required name='username'><br></center>
<center><input type='password' class='form-control' style='width:320px;' placeholder='Password' required name='password'><br></center><br>

<center><button type='submit' class='btn btn-danger btn-lg' name='login'>Log In</button></center>

Upon clicking submit, the process takes place at 'login.php' and I am inserting the code. Here I have constructed a session variable which works fine in this page only. The code is here:


$servername = "localhost";
$username = "root";
$password = "";
$dbname = "plusyou";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

// variables
$UName = $_POST["username"];
$Password = $_POST["password"];

$sql = "SELECT FName, UName, Password, Email FROM users WHERE UName='$UName' AND Password='$Password'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $_session["Email"] = $row["Email"];
        echo $_session["Email"];

        echo "<a href='home.php'>Home</a>";
} else {
    echo "0 results";



In the home page, the session variable does not work. Please let me know any of my mistakes here. Any sort of help will be appreciated. The code for the 'home.php' file is here:


echo $_session["Email"];



The $_SESSION variable always needs to be uppercase, as it is a superglobal.

html - php session variables getting lost in next page, The $_SESSION variable always needs to be uppercase, as it is a superglobal. If you are using a session variable as a token to use as a handshake on next page load and the token updates on the new page load, but they mysteriously will not match and there is no obvious explanation. I had the following happen and maybe it will save you some time.

$_SESSION needs to be capitalised.

Also, please hash+salt your passwords:

Losing SESSION variables - PHP, If I look at my session.save_path in php.ini I see Quick update, I just did the same thing on another site running on the same web server If you somehow set a cookie for both PHP will get totally confused and mix them up  If you are losing the entire session object, then unless your session has timed out, the other reason is that if you are using inproc session state at your server and ran out of memory - this recycles your worker process causing you to lose the session value. Check your event log to see if this is the case.

It looks like you are using $_session small case.

Please try $_SESSION with uppercase. Replace all $_session with $_SESSION and try again.

May be this will help.

PHP: $_SESSION, An associative array containing session variables available to the current script. If you are using a session variable as a token to use as a handshake on next page load and the if (isset($_REQUEST['_SESSION'])) die("Get lost Muppet!"); ?>. Pass variables between two PHP pages without using a form or the URL of page. I can't figure out why my PHP Session variables are being overwritten on page

PHP :: Bug #15983 :: session variables lost between pages, Consequently changes to the $_SESSION variable are not being saved between pages. My work around is to use the following at the end of each page for $S: seems to be correctly working since from many days on my new  I encouter lost session variables trouble when Response.Redirect() to another aspx page. I set 19 Session variables before redirecting. However, the redirected page's session contains only One variable. The very weir sympton is: If I restart IIS, only for the first IE encounter, the redirected page DOES show all 19 session variables.

$_SESSION[] variables not holding from page to page, Alternatively, you can write those variables to a sessions.php type script, //set flag for comments box to be displayed used on another page  If session.cookie_lifetime is 0, the session cookie lives until the browser is quit. EDIT: Others have mentioned the session.gc_maxlifetime setting. When session garbage collection occurs, the garbage collector will delete any session data that has not been accessed in longer than session.gc_maxlifetime seconds.

PHP session lost after redirect, All of a sudden SOME of my session variables would not persist to the next page. Problem turned out to be ( in php7.1) you header location  The session module supports both methods. Cookies are optimal, but because they are not always available, we also provide an alternative way. The second method embeds the session id directly into URLs. PHP is capable of transforming links transparently. If the run-time option session.use_trans_sid

  • No, because it is a superglobal. What you are doing is essentially creating a new variable called $_session whilst the superglobal is and will always be $_SESSION
  • @KowsarRahmanSadit If it helped, then please accept my answer :)
  • The OP also needs to use prepared statements as their code is vulnerable to SQL Injection attacks