不使用ajax保存的PHP会话变量

时间:2022-09-20 11:13:59

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会话变量未被保留。但是我检查了php。ini会话。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中设置一个静态session_save路径。ini文件。对于大多数付费的web托管服务,它们只是有一个用于会话的默认容器,但它受到负载平衡的影响。releif。

6 个解决方案

#1


24  

I think you're missing session_start() on the page that Ajax calls.

我认为您在Ajax调用的页面上丢失了session_start()。

You need:

你需要:

<?php
session_start();
if(isset($_SESSION['views']))
    { echo "Views: " . $_SESSION['views'];} 
    else 
    { echo "Views: NOT SET";}
?>

#2


5  

You need to start session session_start() in the other PHP file also, the one you are calling through AJAX.

您需要在另一个PHP文件中启动会话session_start(),这是通过AJAX调用的。

#3


1  

In the case of using a paid web hosting service the default session save path is automatically set like this:

在使用付费web托管服务的情况下,默认的会话保存路径会自动设置为:

http://php.net/session.save-path
session.save_path = "/tmp/"

You need to place the static path to your root folder there.

您需要将静态路径放置到根文件夹中。

#4


1  

I ran into what i thought was the same issue when running PHP 7 on IIS Server 2012 today.

我在2012年的IIS服务器上运行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.

在检查会话。save_path值集,C:\Windows\临时在我的情况,我认为最好检查文件夹权限匹配的用户帐户下运行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正在运行的用户帐户立即解决了这个问题。

#5


0  

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)。

#6


0  

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。

http://php.net/session_status

http://php.net/session_status

#1


24  

I think you're missing session_start() on the page that Ajax calls.

我认为您在Ajax调用的页面上丢失了session_start()。

You need:

你需要:

<?php
session_start();
if(isset($_SESSION['views']))
    { echo "Views: " . $_SESSION['views'];} 
    else 
    { echo "Views: NOT SET";}
?>

#2


5  

You need to start session session_start() in the other PHP file also, the one you are calling through AJAX.

您需要在另一个PHP文件中启动会话session_start(),这是通过AJAX调用的。

#3


1  

In the case of using a paid web hosting service the default session save path is automatically set like this:

在使用付费web托管服务的情况下,默认的会话保存路径会自动设置为:

http://php.net/session.save-path
session.save_path = "/tmp/"

You need to place the static path to your root folder there.

您需要将静态路径放置到根文件夹中。

#4


1  

I ran into what i thought was the same issue when running PHP 7 on IIS Server 2012 today.

我在2012年的IIS服务器上运行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.

在检查会话。save_path值集,C:\Windows\临时在我的情况,我认为最好检查文件夹权限匹配的用户帐户下运行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正在运行的用户帐户立即解决了这个问题。

#5


0  

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)。

#6


0  

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。

http://php.net/session_status

http://php.net/session_status