Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Strange PHP Session Issue

Featured Replies

So I'm working on a project and everything has been going smoothly and then today my sessions would randomly destroy themselves, and I say randomly with emphasis. In some cases I'd log in and and I'd be logged out upon refresh, other times I might navigate a page or two before being logged out and sometimes I could refresh or navigate 20+ times before being logged out and I'm running out of ideas as to why this is happening.

 

My php.ini is using the default cookie settings so the session shouldn't be destroyed until the browser resets or until it is manually destroyed and I only have session_destroy() in my logout script which is definitely not being accessed at any time I have experienced this random logout.

 

I've also looked over my code to make sure there's nothing strange going on and I can't seem to find anything out of the ordinary.

 

What's confusing me most is the randomness... I mean, I can stay on the same page and refresh a few times and it will log me out with no script or settiing being altered during that time so why does one page load not log me out and then an identical page load does?

 

I'm using the following session script which is being called once at the beginning of each page.

function sec_session()
{
	$session_name = 'user_login';
	$secure = false; // change this to true on a HTTPS
	$http_only = true;

	if(ini_set('session.use_only_cookies', 1) == false)
	{
	          exit();
	}

	// COOKIES

	$cookie_params = session_get_cookie_params();
	session_set_cookie_params(
		$cookie_params['lifetime'],
		$cookie_params['path'],
		$cookie_params['domain'],
		$secure,
		$http_only
	);

	// SESSION INITIATION

	session_name($session_name);
	session_start();
	session_regenerate_id(true);
}

I am completely baffled so any help would be greatly appreciated.

  • Author

Tested in both Chrome and Firefox and the problem is consistent in both.

 

However, it seems that the culprit is session_regenerate_id(true).

 

It would seem as though the browser is sometimes searching for the previous session id. Going to do some research into this and try to find a solution that doesn't involve compromising security by keeping previous session id's... maybe slightly delaying the previous id deletion would do the trick :p I'll report back with a solution if I find one for anyone else that encounters this issue.

  • Author

Okay, so after some research I managed to fix the problem by adding a session variable called "last_regen" and incrementing it each time a session is started and once the counter reaches 10, I regenerate the session id and delete the previous one to prevent fixation.

 

However, I noticed something odd... whenever I loaded the page, the counter was incrementing 3 times (which would explain why the SID wasn't matching and logging me out). I've toyed around and I'm not sure why this is happening. The secure session function is only being executed once but it's incrementing the session variable 3 times... and it's not looping because I've tested it with output which only occurs once... If anyone knows why this happening I'd love to know. I'm guessing it has something to do with session callbacks but I'm surprised it would cause the execution of the incrementation code three times :fool:

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.