Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am currently building a website, which until quite recently was purely PHP. However I am now making trying to have the site use more AJAX, to lessen the page reloads. In PHP I had a lovely object orientated user class with methods for updating data, logging out, and so on. When a user logs on this would be stored as a session variable, and then any page that wishes to do anything the user could just grab it from the session and call it's methods. Clearly this use of php objects and the session doesn't really work with ajax. However I don't want to have to scrap storing the user in an object (which neatens things up somewhat), so don't just want to go down the route of defining a ton of js functions that grab the current username, and use the to do mysql queries through ajax. Am I being stupid here, and what route would people recommend I take.

share|improve this question

1 Answer

AJAX requests send the cookie by default. Assuming that you are mapping your user object to the PHP session, the request over AJAX should automatically have access to the session and the user object.

If you're using another method to handle sessions (e.g. a session id added to the query string) a) be wary of session spoofing, and b) append it to the AJAX request manually. And VOILA.

share|improve this answer
How can I modify the user object from JS though as it is a php object? I'm guessing I'll have to echo it out from the server using json_encode. – handuel Mar 25 at 21:29
Do you have to expose it to the front end? – Mike Brown Mar 26 at 15:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.