php PHP会话变量没有用ajax保留
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11768816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP session variables not preserved with ajax
提问by Quinma
I have a one page website that uses AJAX to load new php files and update the display.
我有一个单页网站,它使用 AJAX 加载新的 php 文件并更新显示。
I start my php session on the main page but when I use ajax to update inner html I need those session variables for the new php file being loaded.
我在主页面上启动我的 php 会话,但是当我使用 ajax 更新内部 html 时,我需要这些会话变量来加载新的 php 文件。
This post is similar to this one: PHP Session Variables Not Preserved. But I checked and my php.ini has session.use_cookies = 1
这篇文章类似于这篇文章:PHP Session Variables Not Preserved。但我查了一下,我的 php.ini 有session.use_cookies = 1
Main Page PHP:
主页 PHP:
<?php
session_start();
if(isset($_SESSION['views']))
{$_SESSION['views']=$_SESSION['views']+1;}
else
{$_SESSION['views']=1;}
?>
After User Input I use ajax to call a php file and load a subsection of the page:
在用户输入后,我使用 ajax 调用一个 php 文件并加载页面的一个子部分:
<?php
if(isset($_SESSION['views']))
{ echo "Views: " . $_SESSION['views'];}
else
{ echo "Views: NOT SET";}
?>
Can someone please tell me what important step I am missing? Thank you.
有人可以告诉我我错过了什么重要的步骤吗?谢谢你。
Update:After adding session_id() call to both the main and sub pages I see that both pages have the same Session_ID. However it still cannot pull the session variable and if i do assign it a value the two same name session variables stay independent of one another.
更新:将 session_id() 调用添加到主页面和子页面后,我看到两个页面都具有相同的 Session_ID。但是,它仍然无法拉取会话变量,如果我确实为它分配了一个值,则两个同名会话变量将保持相互独立。
Answer to the question that this question created:I found that I had to set a static session_save path in my php.ini file. With most paid webhosting services they just have a default container for sessions but it is affected by load balancing. What a releif.
回答这个问题产生的问题:我发现我必须在我的 php.ini 文件中设置一个静态 session_save 路径。对于大多数付费虚拟主机服务,他们只有一个默认的会话容器,但它会受到负载平衡的影响。多么令人放松啊。
回答by drew010
I think you're missing session_start()on the page that Ajax calls.
我认为您缺少session_start()Ajax 调用的页面。
You need:
你需要:
<?php
session_start();
if(isset($_SESSION['views']))
{ echo "Views: " . $_SESSION['views'];}
else
{ echo "Views: NOT SET";}
?>
回答by Kalpesh
You need to start session session_start()in the other PHP file also, the one you are calling through AJAX.
您还需要session_start()在另一个 PHP 文件中启动会话,即您通过 AJAX 调用的那个。
回答by Quinma
In the case of using a paid web hosting service the default session save path is automatically set like this:
在使用付费网络托管服务的情况下,默认会话保存路径会自动设置如下:
http://php.net/session.save-path
session.save_path = "/tmp/"
You need to place the static path to your root folder there.
您需要将静态路径放在您的根文件夹中。
回答by Majickal
I ran into what i thought was the same issue when running PHP 7 on IIS Server 2012 today.
我今天在 IIS Server 2012 上运行 PHP 7 时遇到了我认为相同的问题。
I had added:
我补充说:
if(!isset($_SESSION))
{
session_start();
}
to the start of each AJAX file but kept recieving the following PHP Notice:
到每个 AJAX 文件的开头,但一直收到以下 PHP 通知:
PHP Notice: A session had already been started - ignoring session_start()
A bit of searching lead me to this thread which pointed me in the right direction to resolving the issues I encountered. Hopefully the following information will assist others encountering the same issue.
一些搜索将我带到了这个线程,它为我指明了解决我遇到的问题的正确方向。希望以下信息能帮助其他遇到相同问题的人。
After checking the session.save_path value was set, in my case C:\Windows\Temp, I thought it best to check the folder permissions match those of the user account I was running IIS under.
在检查 session.save_path 值是否设置后,在我的情况下是 C:\Windows\Temp,我认为最好检查文件夹权限与我在其下运行 IIS 的用户帐户的权限匹配。
In my case it turned out that the directory I had nominated for session storage (in php.ini) did not have the same user (security permissions) assigned to it as the one which was running the IIS site.
就我而言,结果证明我为会话存储(在 php.ini 中)指定的目录与运行 IIS 站点的用户没有相同的用户(安全权限)。
Interestingly sessions worked fine when not using AJAX requests prior to me adding the new user permissions. However AJAX did not pick up the session until I had corrected the permissions issue. Adding the same user account that IIS is running under immediately resolved this issue.
有趣的是,在我添加新用户权限之前不使用 AJAX 请求时,会话工作正常。然而,在我纠正了权限问题之前,AJAX 并没有接听会话。添加运行 IIS 的同一用户帐户立即解决了此问题。
回答by Mohammed Safeer
Need to initialize the session before you trying to login through ajax call.
在尝试通过 ajax 调用登录之前需要初始化会话。
session_start();
Initialize on the top of the page from where you start the login ajax call.
在开始登录ajax调用的页面顶部初始化。
So that the SESSIONID will be created and stored the browser cookie. And sent along with request header during the ajax call, if you do the ajax request to the same domain
这样 SESSIONID 将被创建并存储浏览器 cookie。并在 ajax 调用期间与请求标头一起发送,如果您对同一个域执行 ajax 请求
For the successive ajax calls browser will use the SESSIONID that created and stored initially in browser cookie, unless we clear the browser cookie or do logout (or set another cookie)
对于连续的 ajax 调用,浏览器将使用最初创建并存储在浏览器 cookie 中的 SESSIONID,除非我们清除浏览器 cookie 或注销(或设置另一个 cookie)
回答by Goldbug
You're trying to use existing session data from your application in an ajax call. To do that, change how you're calling session_start like so:
您正在尝试在 ajax 调用中使用应用程序中的现有会话数据。为此,请更改您调用 session_start 的方式,如下所示:
// With ajax calls
if (session_status()==1) {
session_start();
}
When making ajax calls to php scripts that need existing session data, use session_start after session_status.
在对需要现有会话数据的 php 脚本进行 ajax 调用时,请在 session_status 之后使用 session_start。

