PHP session variables not preserved with ajax

こ雲淡風輕ζ 提交于 2019-11-26 18:28:43

问题


I have a one page website that uses AJAX to load new php files and update the display.

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.

This post is similar to this one: PHP Session Variables Not Preserved . But I checked and my php.ini has session.use_cookies = 1

Main Page 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:

<?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.

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.


回答1:


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

You need:

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



回答2:


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




回答3:


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.




回答4:


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

I had added:

if(!isset($_SESSION)) 
{ 
    session_start(); 
} 

to the start of each AJAX file but kept recieving the following PHP Notice:

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.

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.

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.




回答5:


Need to initialize the session before you trying to login through ajax call.

session_start();

Initialize on the top of the page from where you start the login ajax call.

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

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)




回答6:


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:

// 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.

http://php.net/session_status



来源:https://stackoverflow.com/questions/11768816/php-session-variables-not-preserved-with-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!