Accessing Session After Ajax Request

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
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Accessing Session After Ajax Request

Post 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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Accessing Session After Ajax Request

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Re: Accessing Session After Ajax Request

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Accessing Session After Ajax Request

Post 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.)
There are 10 types of people in this world, those who understand binary and those who don't
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Re: Accessing Session After Ajax Request

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Accessing Session After Ajax Request

Post 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
There are 10 types of people in this world, those who understand binary and those who don't
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Re: Accessing Session After Ajax Request

Post by AliasBDI »

You are so right!!!! I have a freaking space before it! Ahhhh.. I'm so stupid. Thanks for your patience.
Post Reply