session variables disappear in internet explorer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
esibobo
Forum Newbie
Posts: 7
Joined: Mon May 31, 2010 7:05 am

session variables disappear in internet explorer

Post by esibobo »

ok, so i've written some code that makes use of some session variables. my session variables and code work fine for all my pages in every browser except explorer. when using explorer, the session variables just disappear for no obvious reason on some of my pages, and hence the code which is dependent on those variables does on work properly. i've tested with mozilla, chrome, safari, and opera and they all work just fine.
Any idea what is going on, please?
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: session variables disappear in internet explorer

Post by Apollo »

I assume the particular version of IE you are using is not configured to deny cookies?

Does it happen randomly, or can you reproduce it consistently with a piece of minimal example code? Can you post it here?
esibobo
Forum Newbie
Posts: 7
Joined: Mon May 31, 2010 7:05 am

Re: session variables disappear in internet explorer

Post by esibobo »

no i dont think so.

also it is not random. it is happening with some particular pages. so i commented everything out from one of the pages leaving just the code below to see if the session was being passed correctly.

Code: Select all

<?php	session_start();
	if(isset($_SESSION['cat'])) echo 'yes the session is set';
	else echo "no the session is not set";
?>
so i simply set the session on one page and link it to the page having ONLY this code. all the other browsers tell me "the session is set" as expected except ie which says "the session is not set";
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: session variables disappear in internet explorer

Post by Apollo »

Interesting... What happens when you have the following two files, and browse to a.php?

a.php:

Code: Select all

<?php
session_start();
$_SESSION['cat'] = 1;
header('Location: b.php');
?>
b.php:

Code: Select all

<?php
$s = print_r($_COOKIE,true);
session_start();
echo "Session: ".print_r($_SESSION,true)."<hr>Cookies: $s<hr>";
echo isset($_SESSION['cat']) ? 'yes the session is set' : 'no the session is not set';
?>
(Important: make sure you don't leave any whitespace or emptylines before the <?php opening tags in either file!)
discoinferno
Forum Newbie
Posts: 4
Joined: Tue Jul 06, 2010 1:48 am

Re: session variables disappear in internet explorer

Post by discoinferno »

i am experiencing a similar problem where I will lose my session in IE. I tried the example you posted and I get:

[in IE]
Session: Array ( )
Cookies: Array ( )
no the session is not set

[in any other browser i get]
Session: Array ( [cat] => 1 )
Cookies: Array ( [__utmz] => 198780920.1273254169.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none) [__utma] => 198780920.305160955.1273254169.1277790464.1277829599.12 [PHPSESSID] => ev15orrn4uc5ubtf5ju2cmd221 )
yes the session is set
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: session variables disappear in internet explorer

Post by Apollo »

Ok, and what if you set some random cookie explicitly at the beginning of a.php?
Does it make a difference if you first explicitly delete all cookies, browser cache, etc. before visiting a.php?

new a.php:

Code: Select all

<?php
setcookie('foo',1,0,'/');
setcookie('bar',1,time()+365*24*60*60,'/');
session_start();
$_SESSION['cat'] = 1;
?>
<a href='b.php'>click me plz!</a>
esibobo
Forum Newbie
Posts: 7
Joined: Mon May 31, 2010 7:05 am

Re: session variables disappear in internet explorer

Post by esibobo »

my problem is becoming a little more peculiar (at least to me)
when i create 2 new pages a.php and b.php and try out what you (Apollo) said, it says my sessions have been set alright. however when i link one of my old pages with all its code to b.php, then every other browser says the session has been set except ie. it does not make any difference if i set some random cookie on that page.
could i be doing something on one of those pages that affects ie session variables?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: session variables disappear in internet explorer

Post by Jade »

Do you have session_start() at the very top of the page? Are cookies turned on in IE settings? Are you redirecting from one page to another without doing a session_write_close()?
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: session variables disappear in internet explorer

Post by Apollo »

discoinferno, do you still have the same problem? Any chance of testing my 2nd example?
discoinferno
Forum Newbie
Posts: 4
Joined: Tue Jul 06, 2010 1:48 am

Re: session variables disappear in internet explorer

Post by discoinferno »

thanks for your suggestion Apollo. I won't have a chance to test out your example until later tonight. I don't have a Windows PC with me right now. I'll be sure to post as soon as I have a chance to test it out. Thanks for your help.
discoinferno
Forum Newbie
Posts: 4
Joined: Tue Jul 06, 2010 1:48 am

Re: session variables disappear in internet explorer

Post by discoinferno »

Jade wrote:Do you have session_start() at the very top of the page? Are cookies turned on in IE settings? Are you redirecting from one page to another without doing a session_write_close()?
I've tried writing session_write_close() in a different example with no success. I'm starting to think it's somewhere in the way my host setup their webserver since Apollo's a.php/b.php example doesn't work for me but works for other people.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: session variables disappear in internet explorer

Post by Jade »

discoinferno wrote: I've tried writing session_write_close() in a different example with no success. I'm starting to think it's somewhere in the way my host setup their webserver since Apollo's a.php/b.php example doesn't work for me but works for other people.
If it's working for other people who are using IE then that says it's something wrong with your IE settings. Have you tried clearing your cache? I know when I work with IE I sometimes have problems where it caches a session and then won't re-load the correct information until I shut down the browser entirely or until I use something like the firefox Web Developer addon to disable the cache.
discoinferno
Forum Newbie
Posts: 4
Joined: Tue Jul 06, 2010 1:48 am

Re: session variables disappear in internet explorer

Post by discoinferno »

okay. i've been doing a bunch of testing and I think I figured out something. Right now the domain that I'm testing on was setup as a masked redirect so I think when the cookie is being set the domains are not matching up therefore invalidating the cookies on IE. For example, foo.com/a.php mask redirects the user to bar.com/sites/foo/www/a.php. The foo.com version doesn't work. I copied a version of a.php into the root of bar.com. When I load bar.com/a.php page and then load the foo.com version in a new tab, all of a sudden the foo.com version works.

I think my problem is a domain issue. It's probably a different problem from Esibobo's original posting (although he'll have to confirm that himself). Thanks Apollo/Jade for your input. I hope my solution helps some other people who have the same problem.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: session variables disappear in internet explorer

Post by Apollo »

Thanks for your feedback! And also thanks to Jade for the session_write_close comment, I thought I was doing that already but I found in some circumstances my redirect may occur before session_write_close.
esibobo
Forum Newbie
Posts: 7
Joined: Mon May 31, 2010 7:05 am

Re: session variables disappear in internet explorer

Post by esibobo »

hmmm, i dont really know what happened but everything just started working. i dont think it was a cache problem because i've been having this problem for two whole weeks and i cleared ie's cache more times than i can count. i really wish i could find out exactly what was happening so it does not occur again especially on some user's machine when i put it online.
well, the only thing i had not tried was the session_write_close() suggestion by Jade. so i'll make sure i work that one out. But thanks to all of you who helped. Thanks very much.
Post Reply