Page 1 of 1
Can't get sessions to work reliably
Posted: Mon Sep 07, 2009 4:50 am
by alhermette
I have a site that I send traffic to with information in the query string. All the traffic gets sent to one page where a decision is made as to where to redirect it to within the site and the information contained in the query string is recorded to session variables. From there I need to be able to access the session variables from any page on the site. The problem is that I am losing a significant proportion of the session information in the redirect.
I am aware that sessions can be lost when using a header redirect (which is what I am doing) so I am passing the session ID in the query string from my redirect page in an attempt to overcome this - Code on redirect page:
Code: Select all
$Destination = 'http://' . $Domain . $Path . '?' . 'SID=' . Session_ID();
session_write_close();
header("Location: $Destination");
exit;
And on the page where it goes to ($Destination) I am trying to pick up the session ID from the redirected traffic like this:
Code: Select all
if(isset($_GET['SID']))
{session_id(strip_tags($_GET['SID']));}
$Cookie_Domain = str_replace('www','',$_SERVER['HTTP_HOST']); //Returns .example.com to allow access on all subdomains
session_name('myname');
session_set_cookie_params(0, '/', $Cookie_Domain);
session_start();
With the session_set_cookie_params I am trying to make the session available across all subdomains just in case there was a problem with the site being accessed through the domain with and without the www.
Currently I see that I am losing the session about 20% of the time. Can anyone point me in the right direction to nail down where the problem is and how to overcome it? My current best thinking is that it could be a browser specific issue (IE6 accounts for around 20% of the traffic).
Re: Can't get sessions to work reliably
Posted: Mon Sep 07, 2009 10:25 am
by jackpf
Re: Can't get sessions to work reliably
Posted: Mon Sep 07, 2009 5:26 pm
by alhermette
I read that one earlier but it didn't help.
I am already using session_write_close and exit.
It's driving me nuts, I just can't figure out what is wrong.
Re: Can't get sessions to work reliably
Posted: Mon Sep 07, 2009 6:02 pm
by jackpf
Oh, that's all I had. Sorry

Re: Can't get sessions to work reliably
Posted: Mon Sep 07, 2009 6:22 pm
by Eric!
Can you create a log and identify which $_SERVER['HTTP_USER_AGENT'] is working and which one fails?
You might check the garbage collector timeout too.
Code: Select all
$garbage_timeout = 3600; // 3600 seconds = 60 minutes = 1 hour
ini_set('session.gc_maxlifetime', $garbage_timeout);
//also the cookie timeout something other than non-zero...?
session_set_cookie_params(3600, '/', $Cookie_Domain);
Re: Can't get sessions to work reliably
Posted: Tue Sep 08, 2009 6:38 am
by alhermette
Can you create a log and identify which $_SERVER['HTTP_USER_AGENT'] is working and which one fails?
Looks like I may have to do something to do this.
session.gc_maxlifetime is set in the server configuration to 30 minutes so I doubt that putting the setting in at the page level will help. The cookie lifetime is set to 0 so that it expires on closing the browser window. Although that is what I want I am not sure if there can be undesirable effects when doing this. If I set it to say 120 seconds will it expire 120 seconds after it was created, after it was last modified or after the browser window closes?
Any other bright ideas?
Re: Can't get sessions to work reliably
Posted: Tue Sep 08, 2009 10:11 am
by Eric!
The lifetime timestamp is set relative to the server time, which is not necessarily the same as the time in the client's browser....so it might be a bit complicated depending on where your users are. I guess 0 is probably a better choice unless some browser is doing something it shouldn't during the redirect.
When you say you're loosing "a significant proportion of the session information" what does that mean? Is the session lost or just some of the data?
Have you tried using a javascript redirect instead? I've read sometimes this prevents session data loss in some IE browsers.
Code: Select all
if($not_user)
{
header("Location: login.php);
}else
{
echo '<html>
<body
onload=eval("window.location.href=\'http://your.user.page/default.php?parameters\';")
;></body></html>';
}
(Note inside the window.location there should be a backslash then single quote before http and the same at the end of the word parameters, but they are not displaying properly.)
Re: Can't get sessions to work reliably
Posted: Wed Sep 09, 2009 5:52 am
by alhermette
When you say you're loosing "a significant proportion of the session information" what does that mean? Is the session lost or just some of the data?
It looks like all the data is lost in instances where I see the problem and it is happening around 20% of the time. I haven't seen any partial loss of session data yet. It was 33% of the time but after I added code to specifically name the session and cookie params it dropped.
I hadn't considered using javascript redirects for a variety of reasons but your idea of using it only in certain circumstances may well prove very useful and wasn't something that had occured to me - thanks. I have put in some code to feed back the browser version to me in instances where the session data is lost and I'll see if there is any discernable pattern.
Re: Can't get sessions to work reliably
Posted: Wed Sep 09, 2009 10:22 am
by Eric!
Loss of the session could definately be a browser issue and probably isn't a server problem. If you figure it out, could you let us know the solution?

Re: Can't get sessions to work reliably
Posted: Wed Sep 09, 2009 4:07 pm
by alhermette
I was actually hoping that someone would provide me with the solution.
Having said that I have been working on some code to return the browser version to me when sessions are lost - although that's not as simple as it sounds. If I find anything I will post back here.