iKevinShah

iKevinShah

PHP Sessions not working OR getting a new session id for each PHP request?


Posted by iKevinShah in Webmaster18 February, 2017

So a few days ago, while working on a new project, my sessions suddenly were not working. session_start() on each page / requests created a new session altogether, discarding the older ones. I was eventually successful in fixing it, here are the possible things to check for reference:

  • Check session_save_path: Check what the current path is for php to save sessions. Same can be checked by session_save_path().

  • If you are using a custom path to save sessions, make sure the path exists and is writable by PHP. You can check via following


if ( !is_writable(session_save_path()) ) {
 echo 'Session save path "'.session_save_path().'" is not writable!'; 
}

  • Are you forcing sessions to use https? Check the php.ini for session.cookie_secure , if thats ON and you are accessing ( requesting ) via http, PHP will create session, set the session cookie but since the connection is not secure; on subsequent requests, the PHPSESSID cookie will be ignored and the session will be restarted. Try turning it off via ini_set('session.cookie_secure','Off'); OR directly in php.ini if you have access to it.

  • Make sure you are not using session_regenerate_id on each request. This will regenerate the session ID for each request and invalidate the older ones. session_regenerate_id has its own use case, but not this way.

  • Check if browser cookies are enabled?


  February 18, 2017, 1:09 pm