AJAX 请求是否保留 PHP 会话信息? [英] Do AJAX requests retain PHP Session info?

查看:31
本文介绍了AJAX 请求是否保留 PHP 会话信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个用户登录到我的网站,将他的 ID 存储在 $_SESSION 中,然后他从他的浏览器中单击了一个保存"按钮,该按钮将向服务器发出 AJAX 请求.他的 $_SESSION 和 cookie 是否会保留在此请求中,我可以安全地依赖 $_SESSION 中存在的 id 吗?

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 the server. Will his $_SESSION and cookies be retained in this request, and can I safely rely on the id being present in the $_SESSION?

推荐答案

答案是肯定的:

会话在服务器端维护.就服务器而言,AJAX 请求和常规页面请求之间没有区别.它们都是 HTTP 请求,它们都以相同的方式在 header 中包含 cookie 信息.

Sessions are maintained server-side. As far as the server is concerned, there is no difference between an AJAX request and a regular page request. They are both HTTP requests, and they both contain cookie information in the header in the same way.

从客户端,相同的 cookie 将始终发送到服务器,无论是常规请求还是 AJAX 请求.Javascript 代码不需要做任何特殊的事情,甚至不需要意识到这种情况的发生,它的工作方式与常规请求相同.

From the client side, the same cookies will always be sent to the server whether it's a regular request or an AJAX request. The Javascript code does not need to do anything special or even to be aware of this happening, it just works the same as it does with regular requests.

这篇关于AJAX 请求是否保留 PHP 会话信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆