PHP session variables not preserved with ajax

前端 未结 6 1076
臣服心动
臣服心动 2020-12-03 17:36

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 ht

相关标签:
6条回答
  • 2020-12-03 17:54

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

    0 讨论(0)
  • 2020-12-03 17:58

    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.

    0 讨论(0)
  • 2020-12-03 18:02

    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";}
    ?>
    
    0 讨论(0)
  • 2020-12-03 18:02

    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

    0 讨论(0)
  • 2020-12-03 18:04

    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.

    0 讨论(0)
  • 2020-12-03 18:15

    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)

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