Page 1 of 1

Accessing Session After Ajax Request

Posted: Wed Apr 30, 2008 4:55 pm
by AliasBDI
I have session_start() set on the parent page. When I make an ajax request and pull the new page in, I cannot set session_start() again since the headers were already sent. Is there a way to access the session on the child page?

Re: Accessing Session After Ajax Request

Posted: Wed Apr 30, 2008 4:58 pm
by VladSun
AJAX would use the same stateless HTTP protocol, so it makes no difference whether the request is made by user or AJAX ...
Maybe if you post some code we could help you.

Re: Accessing Session After Ajax Request

Posted: Wed Apr 30, 2008 5:16 pm
by AliasBDI
Maybe I didn't explain myself well. Page1 has a link that when clicked, makes an Ajax call to Page2 and writes Page2 inside a <div> on Page1. So now Page1 has Page2 inside it. The browser never left the web address and never refreshed. Page1 has the session_start() at the top. Page2 cannot read the session since it was not originally parsed with Page1. You would think that the easiest solution would be to put a session_start() at the top of Page2. But you cannot since the headers are already sent.

So, I'm trying to get Page2 to read the session that Page1 initiated. Make sense? There is really no code to post.

Re: Accessing Session After Ajax Request

Posted: Wed Apr 30, 2008 5:20 pm
by VladSun
Yeah, I know what AJAX is ;)
So ... why don't you just try to use session_start, $_SESSION, etc. in "Page2" and see what happens ;)
Also make sure there is no ouput before session_start in Page2 (that includes new lines, tabs, spaces etc.)

Re: Accessing Session After Ajax Request

Posted: Wed Apr 30, 2008 5:51 pm
by AliasBDI
Didn't work. A session_start() cannot be set on a page called asynchronously (the headers are already passed and so I get an error on that). Then, I added an echo of a session variable that is echoed on the parent page. On the parent page it works fine, on the child page (called in after parent is parsed and in browser), it doesn't work. Long story short, the session must be initiated on all Ajax requested pages. But it cannot be initiated when the page is requested by another page.

I don't mean to insult your smarts (I'm sure you are smarter than me). I just don't know what else to say. I set up a couple of pages to see what I mean. You can go here to see them: http://www.aliasbdi.com/session.php

I can post the code here as well but the ajax call is super long.

Re: Accessing Session After Ajax Request

Posted: Wed Apr 30, 2008 6:07 pm
by VladSun
output started at /mnt/Target01/331444/331704/www.aliasbdi.com/web/content/page2.php:2
;)
So ... there is some output at line 2 in Page2

Re: Accessing Session After Ajax Request

Posted: Wed Apr 30, 2008 6:47 pm
by AliasBDI
You are so right!!!! I have a freaking space before it! Ahhhh.. I'm so stupid. Thanks for your patience.