Hi,

I'm having trouble reading session data on my ajax request handler php file.

Basically this is the idea:

Index.php -- user is here, logged in.. $_SESSION['userid'] contains the user's unique ID.

On index.php a user can submit an ajax request.. the ajax request then calls ajaxhandler.php

ajaxhandler.php cannot read any session data. I have initialized the session via session_start() etc.. with no avail .

I did notice however, that if I did the following $_SESSION['test'] = "foo";

that test variable is readable back in index.php when refreshed. Now I'm really stumped as to why i cannot read session data but I can write session data.

Is it because when an ajax request is made its considered a NEW session? If so, how can I go about this issue?

thanks

    Hi,

    I personally don't have much experience with sessions when it comes to AJAX, however you always have to be careful when you're passing user/account info through AJAX as the javascript is executed on the user's computer. When you send the http request over it's basically the same as the browser would send, the only difference being you specify the headers. The reason it's not working is because, as you guessed, it's not really picking up the old session.

    I don't know what you're doing, but I generally use another means of passing information, sort of like a hash table, that prevents the computer executing the ajax request from entering false session data, but at the same time passing that information via the AJAX request. For example I may have a database or cache file on the server that associates a key with the session[userid], and then pass this key via ajax. When the handler script receives the request, it looks up the key to determine the session info. You obviously wouldn't want to pass the userid directly as it's easily modifiable.

      Hi,

      Actually, I am not passing any user info through ajax at all. I'm trying to save user prefrences through ajax. I basically have an interactive drop down menu, and I want to save the state of it in the database, so that once the user logs in, it will be in the same form he or she left it in. All the session data is handled through the backend.

      I also have a random ID that is generated every single time a user logs into their account and use that to associate the info with the account.

      Once the prefrences are sent to the php page handling the ajax request. I store it in the database under that user's id. Now the way I would do that is to use the userid that is stored in a session key. Problem is, I cannot access the session key. So when I run my mysql query ' Update.... WHERE userid = $_SESSION['userid'].. it doesn't work.

      Hope that clarifies the situation. I don't know of any other way I get the prefrences and store it under the user...

        Write a Reply...