Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Problem with ajax and php sessions

3:40 am on Jun 10, 2009 (gmt 0)

New User

10+ Year Member

joined:Sept 14, 2004
votes: 0

I have a skeleton php page that loads other php pages as includes into the page. I have session_start declared at the very beginning of this skeleton page and the site has complete access to these session variables in all the pages that I have ever called in the site.

Now, however, I have started to mess with ajax calls to other php pages that do some of the form processing, and I am having some trouble accessing the session variables. I am simply trying to set session variables in the ajax php pages and then access them back in the main php pages.

I tested it for a while and first came to the conclusion that I just couldn't get the session variables to set or access them if I set them in the ajax php files. However I finally figured out that they weren't setting in the ajax php files because I hadn't redeclared session_start in that page. So, when I redeclare that method, I can set the variable and I can then load one of the main pages and I can access the variable. However, now I have a new problem in that I am declaring this second session_start from an ajax call after the main page has already been sent to the browser and so I get header errors saying content has already been sent. So, I am stuck. If I don't call the second session_start, I don't have access the variables. If I do, I get header errors.

Does this make sense? Please help.

6:17 pm on June 10, 2009 (gmt 0)


WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
votes: 0

my gut says that you probably have to find a different way to do what you need to then

obviously the other script needs to not use the session vars, how can you pull them out of the session and put them into scope for the file?

6:23 pm on June 10, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:July 21, 2008
votes: 0

I basically make an include called session.php (with all the data in there) and include it on my skeleton page.

Then you can include this on your ajax page(s) with the same settings.

e.g. I ajax/jquery $.get to a page which has that include on and it works fine

Also make sure your session lasts (not just session_start)

$expireTime = 60*60*24*3; // 3 days