PHP session lost after redirect
First, carry out these usual checks:
- 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
declaration before anything else. Also ensure there are no whitespaces/tabs before the opening<?php
declaration. - After the
header
redirect, end the current script usingexit();
(Others have also suggestedsession_write_close();
andsession_regenerate_id(true)
, you can try those as well, but I'd useexit();
) - Make sure cookies are enabled in the browser you are using to test it on.
- Ensure
register_globals
is off, you can check this on thephp.ini
file and also usingphpinfo()
. Refer to this as to how to turn it off. - Make sure you didn't delete or empty the session
- Make sure the key in your
$_SESSION
superglobal array is not overwritten anywhere - Make sure you redirect to the same domain. So redirecting from a
www.yourdomain.com
toyourdomain.com
doesn't carry the session forward. - Make sure your file extension is
.php
(it happens!)
Now, these are the most common mistakes, but if they didn't do the trick, the problem is most likely to do with your hosting company. If everything works on localhost
but not on your remote/testing server, then this is most likely the culprit. So check the knowledge base of your hosting provider (also try their forums etc). For companies like FatCow and iPage, they require you to specify session_save_path
. So like this:
session_save_path('"your home directory path"/cgi-bin/tmp');
session_start();
(replace "your home directory path" with your actual home directory path. This is usually within your control panel (or equivalent), but you can also create a test.php
file on your root directory and type:
<?php echo $_SERVER['SCRIPT_FILENAME']; ?>
The bit before 'test.php' is your home directory path. And of course, make sure that the folder actually exists within your root directory. (Some programs do not upload empty folders when synchronizing)
Session lost after page redirect in php
You need to put exit();
after your header redirection, otherwise you have just loaded two pages of content into 1 page.
Also make sure you have session_start();
at the top of all your scripts.
PHP Session variable is empty after redirecting to another page
session_start();
needed on each page on top where you are going to deal with SESSION
in any way (create,update,delete).
So add it in your first page like below:-
<?php
session_start();
$_SESSION['loggedin'] = true;
$_SESSION['username'] = $username;
$_SESSION['start'] = time();
$_SESSION['expire'] = $_SESSION['start'] + (5 * 60);
echo "Bienvenido! " . $_SESSION['username'];
header('location:login-success.php');
exit();
php session lost after header() redirect
You need to call session_start();
in both scripts to start and resume the session.
See: http://php.net/manual/en/function.session-start.php
session lost after redirect header location in safari and edge (only from webmail)
After some hours struggeling i found the solution.
The Problem of losing the session after redirect with header location,
The new "samesite" attribute from PHP 7.3.
If this is Strict
, you will lose the session after header( 'Location: /foo' , true, 302);
use Lax
and fine..
At this moment i dont know why this just happens from webmail and only on safari and edge..
the session_starts at the top of the scripts:
$sessionSet = array(
'path' => '/',
'domain' => $_SERVER[ 'HTTP_HOST' ],
'secure' => TRUE,
'httponly' => TRUE,
'samesite' => 'Lax', // Strict will lose the session for some reason in some case..
'lifetime' => 18000
);
ini_set( 'session.save_path', '/dir/to/sessions' );
ini_set( 'session.cookie_lifetime', $sessionSet[ 'lifetime' ] );
ini_set( 'session.gc_maxlifetime', $sessionSet[ 'lifetime' ] );
ini_set( 'session.gc_probability', 1 );
ini_set( 'session.gc_divisor', 3 );
ini_set( 'session.cookie_samesite', $sessionSet[ 'samesite' ] );
session_set_cookie_params( $sessionSet );
session_start();
PHP Session lost after redirection
You forget session_start();
. (-brombeer)
You need session_start();
to initialize a new session or use existing session.
<?php
session_start();
include("authenticate.php");
// check to see if user is logging out
if(isset($_GET['out'])) {
// destroy session
session_unset();
$_SESSION = array();
unset($_SESSION['user'],$_SESSION['access']);
session_destroy();
}
// check to see if login form has been submitted
if(isset($_POST['userLogin'])){
// run information through authenticator
if(authenticate($_POST['userLogin'],$_POST['userPassword']))
{
// authentication passed
header("Location: http://192.168.0.20:8090/index.php");
die();
} else {
// authentication failed
$error = 1;
}
}
// output error to user
if(isset($error)) echo "Login failed: Incorrect user name, password, or rights<br />";
// output logout success
if(isset($_GET['out'])) echo "Logout successful";
?>
Related Topics
Run a Ffmpeg Process in the Background
How to Get Page Number on Dompdf PDF When Using "View"
How to Use Array_Unique on an Array of Arrays
How to Get the Last N Items in a PHP Array as Another Array
Algorithm for Generating a Random Number
While ($Row = MySQL_Fetch_Array($Result)) - How Many Loops Are Being Performed
Differencebetween PHP Require and Include
Dynamic Shipping Fee Based on Custom Radio Buttons in Woocommerce
How Does PHP Compare Strings with Comparison Operators
How to Bind an Arbitrary Number of Values to a Prepared Statement in MySQLi
Is MySQLi Extension Enabled in This PHP Configuration
How to Correctly Link PHP-Fpm and Nginx Docker Containers
How to Properly Handle Session and Access Token with Facebook PHP Sdk 3.0
Load Blade Assets with Https in Laravel
How to Inject a Repository into a Service in Symfony
How to Get CSV File to Download on Ie? Works on Firefox
How to Access Service Container in Symfony2 Global Helper Function (Service)