homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Problem with ajax and php sessions

10+ Year Member

Msg#: 3930079 posted 3:40 am on Jun 10, 2009 (gmt 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.



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

Msg#: 3930079 posted 6:17 pm on Jun 10, 2009 (gmt 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?


5+ Year Member

Msg#: 3930079 posted 6:23 pm on Jun 10, 2009 (gmt 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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved