在我的PHP + Ajax代码中,为什么$ _SESSION在执行期间会消失?

JDe*_*age 1 php ajax

我正在进行一个简单的Ajax练习,我将查询,Ajax和Ajax调用的url分开.简而言之,我在一个页面中运行查询并将结果数组附加到$_SESSION,然后我显示一些html,Ajax代码调用第三页,通过连接到$_GET超全局的计数器逐个从数组中获取元素.这三个文件链接在一起require_once().

最初加载页面时,所有内容都符合预期.该$_SESSION包含从MySQL拉到整个阵列,并且$_GET为空.

一旦我点击按钮执行Ajax代码,$_GET值就会改变并按预期接收计数器的值.

但是,$_SESSION不复存在.var_dump现在返回null,我得到一个错误Notice: Undefined variable: _SESSION in C:\wamp\www\.....\ajax.php.我不明白为什么会这样.

这是我的代码.首先,index.php:

<?php

session_start();

$dbhost = "localhost";
$dbuser = "admin";
$dbpass = "XXXXXXX";
$dbname = "test";
mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname) or die(mysql_error());
$query = "SELECT ae_name FROM ajax_example";
$qry_result = mysql_query($query) or die(mysql_error());
$result;
while($row = mysql_fetch_array($qry_result,MYSQL_ASSOC)){
    $result[]=$row;
}

$_SESSION['array']=$result;

require_once ("viewUsers.php");

require_once ("ajax.php");
?>
Run Code Online (Sandbox Code Playgroud)

然后是html和ajax代码viewUsers.php:

<html>
<body>
<script type="text/javascript">
<!-- 
function createRequest() {
    try {
      request = new XMLHttpRequest();
    } catch (tryMS) {
      try {
        request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (otherMS) {
        try {
          request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (failed) {
          request = null;
        }
      }
    }
    return request;
  }
var indx=0;
function calcIndex(){
  return indx++;
}
function ajax(){
  ajaxRequest = createRequest();
  var index=calcIndex();
  var url="ajax.php?index=" + index;
  ajaxRequest.open("GET",url, true);
  ajaxRequest.onreadystatechange = display;
  ajaxRequest.send(null); 
}
function display(){
  if(ajaxRequest.readyState == 4){
    var ajaxDisplay = document.getElementById('ajaxDiv');
    ajaxDisplay.innerHTML = ajaxRequest.responseText;
  }
}
//-->
</script>
<form name='myForm'>
<input type='button' onclick='ajax()' value='Show next name' />
</form>
<div id='ajaxDiv'>Your result will be displayed here</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

然后从$ _SESSION接收数组的PHP和(应该)根据$ _GET ['index']的值返回下一个项目.该文件是ajax.php.

<?php
var_dump('Get value in ajax.php',$_GET); // The values are as expected
var_dump('Session value in ajax.php',$_SESSION); // This global cease to exist after I click the button
if(isset($_SESSION['array'])){
  $array=$_SESSION['array'];
  $cnt=count($array);
  $index=null;
  if(isset($_GET['index'])){
    $index=$_GET['index'];
    if($index>=$cnt){
      $str="And that's it....";
    }else{
    $str="The next name is ".$array[$index]['ae_name'];
    }
    echo $str;
  }
}
?>
Run Code Online (Sandbox Code Playgroud)

Zaf*_*ffy 18

问题是ajax.php中的会话未启动/恢复.

当你调用index.php时,它是:

index.php - > .. - > ajax.php(SESSION EXISTS(在index.php中调用session_start()))

然后你ajax.php通过ajax 请求你:

html - > ajax.php(SESSION DOESNT EXISTS(之前没有调用session_start()因为我们没有来自index.php))

您只需要在ajax.php中初始化/恢复会话,但是您必须检查它是否已从index.php初始化.把这块代码放到你的ajax.php文件中:

if(!session_id())     // check if we have session_start() called
     session_start(); // if not, call it
Run Code Online (Sandbox Code Playgroud)