PHP Session Variables Not Preserved with Ajax

PHP session variables not preserved with ajax

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";}
?>

Cannot Keep Ajax Preserved Session Variables

There are few issues with your code, such as:

  • Normal code flow: When you first visit index.php, it will trigger the AJAX request, and will subsequently set the session variable; so that when you visit update.php page, you'll get the desired result.
  • Your code flow: Having said the above point, if you directly visit the update.php page without visiting index.php in the first place, you'll get this error,

    Notice: Undefined index: numSum in ...

    And that's because $_POST['numSum'] is not set, in fact, the entire superglobal $_POST array is empty.

    So the solution is this,

    Wrap this statement $_SESSION['numSum1'] = $_POST['numSum']; inside an if block, like this:

    if(!isset($_SESSION['numSum1']) || empty($_SESSION['numSum1'])){
    $_SESSION['numSum1'] = isset($_POST['numSum']) ? $_POST['numSum'] : array();
    }
  • There's also a small syntax error in your code,

    function atest() {

    var id_toc = <?php echo json_encode($_SESSION['numSum1']); ?>;
    window.alert(id_toc);
    { <============ See here, it should be }

    You forgot to add a closing parentheses }

  • Finally, from the documentation,

    ... Session data path. If specified, the path to which data is saved will be changed. session_save_path() needs to be called before session_start() for that purpose.

Edited:

Change your if block in the following way,

if(!isset($_SESSION['numSum1']) || empty($_SESSION['numSum1']) || (isset($_POST['numSum']) && $_POST['numSum'] != $_SESSION['numSum1'])){
$_SESSION['numSum1'] = isset($_POST['numSum']) ? $_POST['numSum'] : array();
}

Also, learn about comparison operators in PHP, especially about ternary operator. The question mark(?:) you're talking about is related to ternary operator. Here are the necessary references,

  • http://php.net/manual/en/language.operators.comparison.php (Comparison operators)

  • http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary (Ternary operator)

php session not working with ajax

According with the official documentation, you must call the session_start() method in each one of pages where you are going to use the session, so try to call session_start() in your index.php.

This method starts new or resumes existing session, also check your PHPSESSID cookie sent with the AJAX request that match with your index.php PHPSESSID.

PHP Session Variables Not Preserved

The session ID has to be carried along in some way in order that the same session can be used over several pages. In general this is done with a cookie (see session.use_cookies) but it can also be done in the URL or in forms (see session.use_trans_sid).

So first you have to make sure that the session ID is transmitted so that PHP can load the right session and session data.

See also Is my understanding of PHP sessions correct?



Related Topics



Leave a reply



Submit