Accessing $_SESSION while AJAXing

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
avivm3
Forum Commoner
Posts: 31
Joined: Mon Jul 12, 2010 5:29 pm

Accessing $_SESSION while AJAXing

Post by avivm3 »

Hi there. I think my question is simple (obvious?) enough that I don't need to insert my code.

The PHP file that my AJAX function uses works, except that it isn't able to access the Global, $_SESSION or $_COOKIE variables. It can only work with the values I pass to it. Is there a way to fix this, so that I'm not passing info (a Username, for example) through a user-visible Javascript function?

Thanks!
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Accessing $_SESSION while AJAXing

Post by JakeJ »

did you do: session_start() at the beginning of the php file your ajax form processed?

The other solution is to have AJAX send as POST instead of GET.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Accessing $_SESSION while AJAXing

Post by JakeJ »

Thinking better of my previous answer, I should say for the record, I just started teaching myself AJAX.

I did a test. I had session_start() in the right place on both pages. I set $_SESSION['test'] = 'test' in the main page. Then, in the processing page, I told it to echo $_SESSION['test'] in to the table which AJAX returned to my other page. So session data can be passed from the originating page TO the processing page, but it cannot pass session date from the processing page back to the originating page since what it sends back is essentially a string that is then parsed by html and has no bearing on the state of php in the page it's returned to. That makes sense too since you're not reloading the page, how would your session get informed of any changes, updates or additions? The answer is, you can't.

What you can do, is send back the value of a session in, for instance a hidden field returned to the originating page. The users can't see the html output of that by viewing the source, so it's safe. Then you just access the value by the elementID and use it as you wish.

I hope that answers your question better. And since I am a n00b to AJAX, I look forward to being corrected if I'm wrong (and yes, I really want to know if I'm wrong) and what a better solution to the problem might be.
avivm3
Forum Commoner
Posts: 31
Joined: Mon Jul 12, 2010 5:29 pm

Re: Accessing $_SESSION while AJAXing

Post by avivm3 »

I'm new too, so no worries there. And thanks for the reply.

Yes, I am including session_start(); at the top. And I should clarify something in my process: The processing page only needs to access the $_SESSION variables for its algorithms, it doesn't need to return them. The reason I have those values exposed now is because I'm currently including them as parameters when I call my AJAX function, in order to get them in the processing page, since - for the moment - I seem unable to get them as any kind of global variable. So, that will go away once I'm able to do what you said you were able to do, simply invoke the $_SESSION[name] and voila, you have it.

Maybe I'm screwing myself up by trying to recycle the file? When the main page loads, I require sample_functions.php to provide some functions to my page. But I've also included at the top of sample_functions an IF statement that looks for posted data (i.e., my AJAX request) and then provides the necessary response. So I am sending my AJAX requests to the same file that was already "required" when the page was generated. Maybe that's bad PHP etiquette and screwing up the session in some way? But why would it not like $_COOKIE either? Isn't that just dependent on the domain generating the cookie, not the session?

(I kind of like having to explain this in real words, but if this is irritating anyone, let me know and I'll post some code...thanks)
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Accessing $_SESSION while AJAXing

Post by JakeJ »

Posting code is always helpful, but you're passing your ajax request to a page that is included the originating ajax page?

You should definitely change that though I can't be certain if that's what's causing your problem.

As a test, you could copy that page, give it a new name and submit you AJAX request to it and see if that helps.
avivm3
Forum Commoner
Posts: 31
Joined: Mon Jul 12, 2010 5:29 pm

Re: Accessing $_SESSION while AJAXing

Post by avivm3 »

Hmmm. I just did a test run on a separate file and still get nothing.

Here's the relevant code:

JS function included in the main page:
function ajaxTest()
{
runAJAX("StatusWindow", "ajax_functions.php", "action_sc=test");
//parameters are 1)Element to contain the Response Text; 2)PHP processor file; 3)Parameters to be included in the .send() as I am using the POST method
}

The main page has a button -> <input type="button" onclick="ajaxTest()" value="test ajax" />

The ajax_functions.php code:

Code: Select all

<?php session_start();
//require a few functions
require("file1.php");
require("file2.php");

if (isset($_POST["action_sc"]))
{

	switch ($_POST["action_sc"])
        {
		
		case "test":
			echo "The session variable for the client id is: " . $_SESSION["customer_sc_clientid"];
			break;
         }
}
The main page can call that SESSION variable successfully, so I know it's there. The AJAX request returns the line above from the PHP file successfully, but without the "customer_sc_clientid", so I know the rest of the code works, it's just not grabbing the variable...
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Accessing $_SESSION while AJAXing

Post by JakeJ »

Just try echoing out the session without putting it inside any conditional restraints. See what happens.

Also, post the code for the originating page.
avivm3
Forum Commoner
Posts: 31
Joined: Mon Jul 12, 2010 5:29 pm

Re: Accessing $_SESSION while AJAXing

Post by avivm3 »

Moved the echo statement outside of the IF conditional. Same result.

Was there a particular part of the main page? The main page is an open source shopping cart and there is a lot of code that makes it happen.
avivm3
Forum Commoner
Posts: 31
Joined: Mon Jul 12, 2010 5:29 pm

Re: Accessing $_SESSION while AJAXing

Post by avivm3 »

Just to close this up, in case someone wanted to know how this ended (thanks JakeJ for trying), I ended up using the shopping cart's "application_top.php" file, which defined all their session settings and the functions they were using to create/manage the globals. Once I "required" that file at the top of my page, I had access to all the variables I needed.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Accessing $_SESSION while AJAXing

Post by superdezign »

JakeJ wrote:So session data can be passed from the originating page TO the processing page, but it cannot pass session date from the processing page back to the originating page since what it sends back is essentially a string that is then parsed by html and has no bearing on the state of php in the page it's returned to. That makes sense too since you're not reloading the page, how would your session get informed of any changes, updates or additions? The answer is, you can't.

What you can do, is send back the value of a session in, for instance a hidden field returned to the originating page. The users can't see the html output of that by viewing the source, so it's safe. Then you just access the value by the elementID and use it as you wish.
Or, you can just use your AJAX to make the relevant changes to the page rather than saving the returned data in the HTML.
avivm3 wrote:I ended up using the shopping cart's "application_top.php" file, which defined all their session settings and the functions they were using to create/manage the globals.
So, you're saying that this file creates the session data without saving it, or that your code was preventing the session data from ever being created, thus making it inaccessible?
avivm3
Forum Commoner
Posts: 31
Joined: Mon Jul 12, 2010 5:29 pm

Re: Accessing $_SESSION while AJAXing

Post by avivm3 »

The session data was being created with some additional security, to prevent session hijacking - thanks to the cart. So, I wasn't able to access it until the processor page session was initialized in the same way that the cart was. Then I was able to access the variables and incorporate them into my logic.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Accessing $_SESSION while AJAXing

Post by superdezign »

Oh. Sounds like a nice piece of software you've got there. Also sounds like one you had to pay for it. :P

Would you mind telling us the name of it, so that other users with your problem will find it via Google?
avivm3
Forum Commoner
Posts: 31
Joined: Mon Jul 12, 2010 5:29 pm

Re: Accessing $_SESSION while AJAXing

Post by avivm3 »

Sure, the cart is open source. It's osCommerce.
Post Reply