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)
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 are lost after redirect in CodeIgniter 3
i just solved my problem turns out i have old version of codeigniter 3 i upgrade my CI to the latest version available on the website . thank you all for the help
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 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 Variables Not Working After Redirect
You should learn more about sessions to avoid making mistakes and not leaving your codes vulnerable!
Know that to work with sessions, you must start them right at the beginning of each script
Also, after you create your session, you don't need to use the 'echo' command and right after redirecting to the success page, in fact, it is on the success page that you should work with the 'echo' command, and create some variables to store the value of those sessions, to make it easier to work with, and to make the code cleaner!
Please try it:
Signin
<?php
session_start();
//Start the session in the top of the script
if (password_verify($rawpassword, $row["passwordHash"])) {
$_SESSION["email"] = $email;
$_SESSION["fname"] = $row["firstName"];
header("Location: home.php");
exit();
} else {
header("Location: signin.php?addlComment=3True");
exit();
}
Home
<?php
session_start();
session_regenerate_id(true); //It can help you to protect against attacks, try to learn it!
$email = $_SESSION['email'];
$first_name = $_SESSION['fname'];
//If the user try to access the page without make login, then redirect to the signin page
if(!email || !first_name)
{
header("Location: signin.php");
exit();
}
//Test the sessions variables
echo "Welcome, you're logged in! I know your first name is: {$first_name}";
Related Topics
Telegram Bot Api: How to Send a Photo Using PHP
Multiple Column With Same Search String in Laravel
Laravel - Page Not Found Even That the Route Exists
Finding the Longest Gap Between Multiple Intervals
Success Message Pop-Up After from Submit Button Clicked
Stop Inserting Data in the Database When Refreshing the Page
Displaying a Base64 Images from a Database Via PHP
Displaying Data from an Array in a HTML Table Using PHP
How to Get Values of Checkbox Array in Laravel 5
Convert Doc/Docx Files to Pdf Using PHP
How to Edit Specific Json Key Values Using PHP
Loop Row in Bootstrap Every 3 Columns
Laravel Unique Validation on Multiple Columns
How to Change the Background Color of Dropdown List in Select Tag