PDA

View Full Version : PHP, AJAX, Cookies, Sessions for Streamlining



blitzen
12-12-2011, 08:20 PM
Hi,

I'm pondering the best way for a 1-page checkout using PHP and AJAX, and I have a few questions about cookies and session variables.
Variable values will change on the fly without reloading the page (I hope I can avoid reloading the page) as the customer proceeds through steps in checkout.

I have an AJAX call to return values of php session variables. This seems to run much more slowly than javascript reading cookies. Thus, I'd prefer to store information in cookies.

However, there is sensitive information and long values that cannot be stored in a cookie. Thus, I'd like to use php session variables when necessary.

The php_value session.auto_start "on" is in htaccess and is working.

1. Javascript can write a cookie or change a cookie value without reloading the page.
Q. If that cookie changes without a page load, will a javascript function that is executed later read the new cookie value?

2. Javascript changed the value of a cookie without a page reload.
A php script is executed via AJAX.
Will the called php script
a) Read the original cookie value when the "html" page was initially loaded?
b) Or, get the new value when the php script (that is called by AJAX) is executed? Get the new value set by the Javascript?

3. The php file that was called by AJAX changed a cookie value. Will javascript running later read the new value?

Now, the same for PHP Session variables.
4. A php script that was called by AJAX changed a session variable.
If I do another AJAX call to a second php script, will the second php script read the new or old session value?

If this is browser-dependent, I'm sunk.

I hope this makes sense.
Thanks!

DaveSawers
12-14-2011, 10:09 AM
I haven't actually tried what you ask, but it should work OK with the latest changes being returned on request. For a definitive answer I think you're going to need to setup a little test.

holyhttp
12-15-2011, 03:06 PM
1) Each request transmits the cookie information to the server. So yes, the a javascript function will be able to ready the cookie information previously set.

2) that would be b) unless the two cookie variables are different in names.

3) AJAX usually start a new session different from the non AJAX initial one.
So the AJAX call to the second script will ready the new session variable instead.

So you are better of relying first on the cookie to preserve the old session variables on the first AJAX request then find a way to transfer the old session data to a new one initiated by the first AJAX call. From then on everything should be transparent on both client and server-side.


In any case test, test and test again.