Do AJAX requests retain PHP Session info?

前端 未结 8 890
情书的邮戳
情书的邮戳 2020-11-28 18:52

If I had a user logged onto my site, having his id stored in $_SESSION, and from his browser he clicked a \'Save\' button which would make an AJAX request to th

相关标签:
8条回答
  • 2020-11-28 19:32

    What you're really getting at is: are cookies sent to with the AJAX request? Assuming the AJAX request is to the same domain (or within the domain constraints of the cookie), the answer is yes. So AJAX requests back to the same server do retain the same session info (assuming the called scripts issue a session_start() as per any other PHP script wanting access to session information).

    0 讨论(0)
  • 2020-11-28 19:32

    Well, not always. Using cookies, you are good. But the "can I safely rely on the id being present" urged me to extend the discussion with an important point (mostly for reference, as the visitor count of this page seems quite high).

    PHP can be configured to maintain sessions by URL-rewriting, instead of cookies. (How it's good or bad (<-- see e.g. the topmost comment there) is a separate question, let's now stick to the current one, with just one side-note: the most prominent issue with URL-based sessions -- the blatant visibility of the naked session ID -- is not an issue with internal Ajax calls; but then, if it's turned on for Ajax, it's turned on for the rest of the site, too, so there...)

    In case of URL-rewriting (cookieless) sessions, Ajax calls must take care of it themselves that their request URLs are properly crafted. (Or you can roll your own custom solution. You can even resort to maintaining sessions on the client side, in less demanding cases.) The point is the explicit care needed for session continuity, if not using cookies:

    1. If the Ajax calls just extract URLs verbatim from the HTML (as received from PHP), that should be OK, as they are already cooked (umm, cookified).

    2. If they need to assemble request URIs themselves, the session ID needs to be added to the URL manually. (Check here, or the page sources generated by PHP (with URL-rewriting on) to see how to do it.)


    From OWASP.org:

    Effectively, the web application can use both mechanisms, cookies or URL parameters, or even switch from one to the other (automatic URL rewriting) if certain conditions are met (for example, the existence of web clients without cookies support or when cookies are not accepted due to user privacy concerns).

    From a Ruby-forum post:

    When using php with cookies, the session ID will automatically be sent in the request headers even for Ajax XMLHttpRequests. If you use or allow URL-based php sessions, you'll have to add the session id to every Ajax request url.

    0 讨论(0)
  • 2020-11-28 19:33

    That's what frameworks do, e.g. if you initialize session in Front Controller or boostrap script, you won't have to care about it's initalization either for page controllers or ajax controllers. PHP frameworks are not a panacea, but they do so many useful things like this!

    0 讨论(0)
  • 2020-11-28 19:38

    If the PHP file the AJAX requests has a session_start() the session info will be retained. (baring the requests are within the same domain)

    0 讨论(0)
  • 2020-11-28 19:41

    put your session() auth in all server side pages accepting an ajax request:

    if(require_once("auth.php")) {
    
    //run json code
    
    }
    
    // do nothing otherwise
    

    that's about the only way I've ever done it.

    0 讨论(0)
  • 2020-11-28 19:45

    One thing to watch out for though, particularly if you are using a framework, is to check if the application is regenerating session ids between requests - anything that depends explicitly on the session id will run into problems, although obviously the rest of the data in the session will unaffected.

    If the application is regenerating session ids like this then you can end up with a situation where an ajax request in effect invalidates / replaces the session id in the requesting page.

    0 讨论(0)
提交回复
热议问题