Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

AJAX - Php session variables are null on other php files.

Duly note, I am using session_start(); preceding any code in both these php files:

Problem in a minimum reduced format:

index.php
PHP Code

<?php
  session_start
();
  
$_SESSION['msg'] = 'hello';
?>

Inside index.php, posting to add_to_cart.php using AJAX.
PHP Code

<?php 
$.ajax({
                
type:"POST",
                
url:"add_to_cart.php",
                
async: false, //tested with this true and now false
                
data:{test_product:productName},
                
success:function(response){
                  
alert(response);
                }
              }); 

add_to_cart.php:

PHP Code

<?php
    session_start
();

    echo 
json_encode($_SESSION['msg']);
?>
The output of the alert is null
in add_to_cart.php do this and see what happens.
PHP Code

<?php
    session_start
();
    
print_r($_SESSION);
    echo 
json_encode($_SESSION['msg']);
?>
Hi,

I have done a var_dump on two servers. My home nginx, and my apache.

The vardump reveals session is intact in apache, however null on nginx. I will tweak the settings accordingly. There seems to be no issue with the code.

Thanks to the above answers.

Thread closed.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Javascript]  by InvestorMay 14, 2022 01:30 PM 7 341 May 14, 2022 01:30 PM
Last Post: Nachfolger
[Javascript]  by HackersJan 26, 2022 08:07 PM 5 241 Jan 26, 2022 08:07 PM
Last Post: Hackers
[Javascript]  by HardStoNJun 9, 2021 04:12 AM 2 179 Jun 9, 2021 04:12 AM
Last Post: HardStoN