简体   繁体   English

Session 变量在 php 页面中不同

[英]Session variables not the same across php pages

Duly note, I am using session_start();请注意,我正在使用session_start(); preceding any code in both these php files:在这两个 php 文件中的任何代码之前:

Problem in a minimally-reduced format:最小简化格式的问题:

index.php index.php

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

When invoking add_to_cart.php using ajax in index.php:在 index.php 中使用 ajax 调用 add_to_cart.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: add_to_cart.php:

<?php
    session_start();

    echo json_encode($_SESSION['msg']);
?>

The output of the alert is null警报的 output 是null

I am running php 7.4 on nginx, on ubuntu.我在 ubuntu 上的 nginx 上运行 php 7.4。

Please try this.请试试这个。

Remove $_SESSION['cart'] = array();删除$_SESSION['cart'] = array(); from index.php来自索引.php

And save item in cart as following in add_to_cart.php.并将商品保存在购物车中,如下所示 add_to_cart.php。

$_SESSION['cart'][] = [$productName,$productQty];

I have tested on another machine running Apache instead of Nginx and it worked.我已经在另一台运行 Apache 而不是 Nginx 的机器上进行了测试,它工作正常。 The var_dump of session immediately on the ajax php file was null on nginx however, had contents intact on the apache server. The var_dump of session immediately on the ajax php file was null on nginx however, had contents intact on the apache server.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM