-->

PHP: session isn't saving before header redire

2019-01-15 13:34发布

问题:

I have read through the php manual for this problem and it seems quite a common issue but i have yet to find a solution. I am saving sessions in a database. My code is as follows:

// session
$_SESSION['userID'] = $user->id;        
header('Location: /subdirectory/index.php');

Then at the top of index.php after the session_start(), i have var_dumped the $_SESSION global and the userID is not in there. As i said ive looked through the PHP manual (http://php.net/manual/en/function.session-write-close.php) and neither session_write_close or session_regenerate_id(true) worked for me. Does anybody know a solution?

Edit: I have session_start() at the top of my file. When i var_dump the session global before the header redirect, i see the userID in there, but not in the other file, which is in a subdirectory of this script

回答1:

I know this is an old toppic but I found the solution (for me). I've put a exit after the header.

$_SESSION['session'] = 'this is a session';
header('location: apage.php');
exit;

This works for me



回答2:

@Matt (not able to comment yet...): If:
a) It appears in the session before redirect
b) other keys work

80% of the time the problem is register_globals, and use of a equally named variable $userID somewhere (the other 19% is just overwriting in places one doesn't expect, 1% is unable to write/lock session before redirect and stale data, in which case you could try session_write_close() before the redirect). It goes without saying register_globals should be off :P



回答3:

You should start the session before using the session array.

PHP Code,
session_start();
$_SESSION['userID'] = $user->id;

header('Location: /subdirectory/index.php');



回答4:

Have you got an session_start(); on the top?

Not tested but cant you do something like this:

session_start();
$_SESSION['userID'] = $user->id;
if( $_SESSION['userID'] == $user->id )
{  
    header('Location: /index.php');
}

I never have this Problem before, interesting



回答5:

userID does not have any keyword status.

Only reason to me, is $_SESSION['userID'] is being overwritten or deleted somewhere.

Make sure you use session->start() in all the files you want to add/access the session.

One important thing ( which may not be applicable in your case ) is, if the session is being handled using cookie, cookie can be made to be accessible only under certain directory and subdirectories under that. In your case anyhow, subdirectory will have access to the session.



回答6:

Make sure both pages are the same php version (php5, php4 sometimes have different session paths)



回答7:

I haven't heard of this issue, but I haven't used sessions all that much.

With sessions you MUST do a few things and have a few setting setup:

  • cookies enabled on client side
  • session_start(), before anything happens
  • make sure you don't destroy the session(unless they want to logout)
  • The PHP session id must be the same (relates to cookies)

Another issue could be the $user->id is returning a reference to an object that doesn't exist on the next page. Most likely not, but make sure.

If I saw your code I could help you a lot more. But when debugging check the session key with session_id() and make sure it's the same. If you could try that then tell me I could keep helping.

I too would like to know how this ends up for when I get back into sessions.



回答8:

I had the same problem recently. I'm writting a customized MVC Website for school and, as everyone told, start_session() must be written in the very first lines of code.

My problem was THE LOCATION of "session_start()". It must be the first lines of your global controller, not the first lines of the view. $_SESSION was not accessible in controller's files because it was only initiated when the server render the view.

Then, I'm using session_write_close() after the header('location: xxx.php') call to keep session variables for the next request.

ex:

globalController.php :

//First line
session_start();
require_once('Model/Database.php');
require_once('Model/Shop/Client.php');
...

logonController.php:

...
//Users is validated and redirected.
$_SESSION['client'] = $client;
header('location: index.php');
session_write_close();

Hope it solved your problems.



回答9:

This was annoying as hell but I finally figured out a solution.

config.php i had: include 'session.php';

At the top of session.php, I had: session_start();

By moving session_start() to the top of the config.php file, viola...

Problem solved!