3

I have an application that has been working with session variables no problem. I start the session before the headers on every page that uses when, it has been fine then it seems all of a sudden I'm getting an undefined index error when I navigate to a page other than the one that sets up the session variables. But only on some browsers. Sometimes sessions are maintained and sometimes they aren't.

It seems that cookies aren't being stored some of the time. I've done checks using different browsers and sometimes cookies are stored and sometimes not.

I did an experiment. I was using firefox to use to app and I was keeping an eye on the tmp folder where the sessions are stored. I cleaned it out. Using firefox I started using the app, using all the pages that sessions were in use and at the end I checked the tmp folder and it had one session file in there.

Did the exact same with internet explorer and there are now 7 different session files.

I'm using PHP 5.3.0 with the WAMP stack. Apache 2.2.11. Session support is enabled in my phpinfo().

I call a var dump on the first page and it prints out the session data. On any subsequent pages the session variable is empty.

<?php var_dump($_SESSION); ?>

array(0){}

Can anyone help me figure out a solution to this?

UPDATE - PHP INI SESSION settings

Directive        Local Value     Master Value
session.auto_start Off           Off 
session.bug_compat_42 On         On 
session.bug_compat_warn On       On 
session.cache_expire 180         180 
session.cache_limiter nocache    nocache 
session.cookie_domain 82.68.26.169   82.68.26.169 
session.cookie_httponly Off      Off 
session.cookie_lifetime 0        0 
session.cookie_path /            / 
session.cookie_secure Off        Off 
session.entropy_file no value    no value 
session.entropy_length 0         0 
session.gc_divisor 1000 1000 
session.gc_maxlifetime 1440      1440 
session.gc_probability 1         1 
session.hash_bits_per_character 5 5 
session.hash_function 0 0 
session.name PHPSESSID           PHPSESSID 
session.referer_check no value   no value 
session.save_handler files       files 
session.save_path c:/wamp/tmp    c:/wamp/tmp 
session.serialize_handler php    php 
session.use_cookies On           On 
session.use_only_cookies On      On 
session.use_trans_sid 0          0 

UPDATE - Solution

Because my app was using iframes pulling in pages from another domain (which i created) the cookies i was trying to set were being blocked. Setup a P3P header and the problem seems to be solved!

iamjonesy
  • 24,732
  • 40
  • 139
  • 206
  • I've read your question again and I've noticed the subject doesn't really match the content. You should clarify what your problem is: whether values are not stored in session or they're not retrieved from the database in the first place. – Álvaro González Dec 05 '10 at 12:23

6 Answers6

3

Do you call session_start() on every page that accesses session data?

Edit: And do you receive the same session ID every time?
Also, could there be some error or warning you're missing (e.g. headers already sent) due to settings?

aib
  • 45,516
  • 10
  • 73
  • 79
  • Yup i have session_start() on every page. It has been fine until it just stopped saving sessions – iamjonesy Dec 05 '10 at 12:43
  • I have no session data at all... when I do a var_dump($_SESSION) I get empty. Sessions are stored in www/tmp. I deleted all the files and refreshed the page that sets them and the file appears in the tmp directory. Still I go to another page and it's empty still. How will I get session IDs? – iamjonesy Dec 05 '10 at 13:54
  • Call `session_id()` after _start. You should be getting the same ID each time. You could also examine the browser request (`Cookie: PHPSESSID=...`). – aib Dec 05 '10 at 14:36
3

My suggestion from your previous question still stands: please compare session ids.

The solution might be as simple as your browser not accepting session cookies.

You retrieve the session id by calling session_id(). Do that right after session_start() it should give you a constant value if the session is the same. Otherwise for every request a new session is instantiated.

Also check C:\wamp\tmp. A gazillion files in this directory might indicate fresh sessions for each request.

EDIT Since we've confirmed new sessions per request, it's time to find out whether session cookies are accepted. Check the settings of your browser and confirm that a cookie for your domain (I guess it's "localhost") with the name PHPSESSID can be found.

Community
  • 1
  • 1
Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
  • Yeah, I think your last point might be correct. I cleared the contents of tmp. Refreshed my page got 2 new files. Navigated to a new page and got an extra 3 new files. Could this be the problem? – iamjonesy Dec 05 '10 at 14:08
  • Yes. We're getting closer. This is an indication for new sessions per request. See my edit. – Linus Kleen Dec 05 '10 at 14:14
  • Using Firefox is good for starters. Follow [Firefox' Support KB](http://support.mozilla.com/en-US/kb/Cookies) for instructions how to enable/disable cookies. – Linus Kleen Dec 05 '10 at 14:35
  • A cookie with the name PHPSESSID isn't in my cookie list. But strange discovery. When I check it on another computer the sessions work fine. On the same computer I check it on chrome works fine, but firefox on that computer getting session problems – iamjonesy Dec 05 '10 at 14:36
  • hey I'm still getting this problem and it's really giving me some issues with the project. On some browsers the sessions are maintained across the site, and on others they are not :( any more help most appreciated – iamjonesy Dec 21 '10 at 20:01
  • I just tried creating a cookie and it seems that the cookie is deleted when I navigate to another page. though the same as the session only on some browsers. – iamjonesy Dec 22 '10 at 10:58
  • @iamjonesy if the cookie gets dropped, you might want to read [this forum thread](http://www.daniweb.com/forums/thread78695.html) about IE dropping cookies... – Linus Kleen Dec 22 '10 at 13:02
  • my mistake it's not just IE. Will have a look at that article thanks – iamjonesy Dec 22 '10 at 14:40
  • @iamjonesy Is it a sporadic issue? I.e. is the session cookie *present* and simply *disappears* or is it even never established at all? – Linus Kleen Dec 22 '10 at 20:06
  • I've made a breakthrough. It's because I'm using external cookies. I pull in pages from a different domain using IFRAMES. Now I just have to figure out how to get browsers to accept the cookies :) – iamjonesy Dec 22 '10 at 20:10
  • @iamjonesy Yeah, after having found your new question, I figured as much. There's a couple comments on p3p in that post I suggested earlier. Good Luck. – Linus Kleen Dec 22 '10 at 20:50
  • goreSplatter, thanks for all your help with this. I couldn't have figured out what was happening without your suggestion to check out my cookies. I am still a newbie and didn't really understand what cookies had to do with session variables. So big thanks and +100 points coming your way! – iamjonesy Dec 23 '10 at 13:57
1

here is the sense in

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
  $_SESSION['saveddata'] = $row;
}

it rewrites $_SESSION['saveddata'] value on each iteration. may be you meant something like

 $_SESSION['saveddata'][] = $row;

it makes sense for $atid = $_SESSION['saveddata']['autotaskid'];

ajreal
  • 46,720
  • 11
  • 89
  • 119
heximal
  • 10,327
  • 5
  • 46
  • 69
  • thanks, that aside though, why would my session variables not be maintained across pages? I've updated my question with var dumps on the page that sets the session and another page – iamjonesy Dec 05 '10 at 11:59
1

Review your session settings. You have a full list with:

<?php

phpinfo();

?>

Scroll down to the "Session" table.

Particularly, make sure that the session.save_path directory exists and is writeable.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
0

When a new session ID is created with each request, most likely it is an issue with your session paths (save_path and cookie_path) and chances of this happening are greater if you're hosting different applications on one server (shared hosting) and some of these applications also implement sessions. This results in conflicts in your /tmp directory. You could change the config of your ini file, but it's best to configure these parameters during runtime.

session_set_cookie_params(0, "/app", ".domain.com");//set session cookie parameters
session_save_path("/home/../public_html/app/sess");//set directory of this app's session data
session_start();//start session

I hope that helps everyone having this issue. #CodeOn

kawerewagaba
  • 1,107
  • 2
  • 15
  • 21
0

I solved this problem on my local WAMP by clearing out the \tmp directory of old sessions.

John Kiernan
  • 79
  • 10