简体   繁体   English

PHP页面之间的会话ID不同

[英]Different session id between php pages

I want to share a variable between pages through the $SESSION variable. 我想通过$SESSION变量在页面之间共享一个变量。 In file1.php I have 在file1.php我有

<?php session_start();
   require_once('connect.php');
   global $gb;
   $_SESSION['myvar'] = "somestring";
   // Some other code
?>

And in my second fille2.php I have 在第二个fille2.php中,我有

<?php session_start();
   require_once('connect.php');
   $myvar = $_SESSION['myvar'];
?>

and $myvar is empty. $myvar为空。 I first make an ajax call to file1.php and then file2.php. 我首先对file1.php进行ajax调用,然后对file2.php进行。 I've tried echoing session_id() and it's different. 我试过了echo session_id() ,这是不同的。 What's wrong here? 怎么了

Edit: I am calling my server side PHP script from localhost and using Chrome with the CORS plugin enabled if that matters 编辑:我要从本地主机调用服务器端PHP脚本,并在启用了CORS插件的情况下使用Chrome(如果重要)

I'm not sure what you mean, but If I have understood properly, both files are being called by an js script using AJAX. 我不确定您的意思,但是如果我正确理解,这两个文件都将由使用AJAX的js脚本调用。 If you could include the js code where the call is being done, that would be really helpful. 如果您可以在执行调用的地方包含js代码,那将非常有帮助。 Anyway, I'm pretty sure that the second file is being downloaded before the first one. 无论如何,我很确定第二个文件是在第一个文件之前下载的。 Something you can do to workaround this is to abstract both session_start() and the $_SESSION['myvar'] variable declaration in another file or in the top of your current file, for example in a file called 'session_init.php' It should look like this : 解决此问题的方法是,将session_start()$_SESSION['myvar']变量声明抽象到另一个文件或当前文件的顶部,例如,一个名为“ session_init.php”的文件中。看起来像这样:

session_init.php : session_init.php:

<?php

//init session and session variable
session_start(); 
/* $_SESSION['myvar'] = 'value'; */ 

your current file (the file where you are making the AJAX request) : 您当前的文件(正在发出AJAX请求的文件):

<?php

//include this folder on the top of the page where the ajax petition is made
require_once('session_init.php');
$_SESSION['myvar'] = 'Some Value';
?>

<script type="text/javascript">

  //js code to make ajax petitio to file 1

  $.ajax({

    //parameters and other stuff

    success : function(resp){

          //js code to make ajax petition to file 2
          $.ajax({

             //parameters and other stuff
          });
      }
  })
 </script>

Now $_SESSION['myvar'] should be accessible in both files as long as you declare session_start() at the top of each file. 现在,只要您在每个文件的顶部声明session_start() ,就可以在两个文件中访问$_SESSION['myvar'] Remember that is not a good practice to nest AJAX petitions. 请记住,嵌套AJAX请求不是一个好习惯。 Also you can take a different approach to send the data trough the files, for example, sending the value of 'myvar' as a parameter on the AJAX request. 您也可以采用其他方法通过文件发送数据,例如,将“ myvar”的值作为AJAX请求的参数发送。

It sounds like you're not calling session_start() from the file making the AJAX calls, which will result in two different sessions. 听起来您好像没有从进行AJAX调用的文件中调用session_start() ,这将导致两个不同的会话。 That, or you're regenerating the session ID somewhere within connect.php 那,或者您正在connect.php某个位置重新生成会话ID

If that doesn't help you resolve your issue, then please provide all relevant code so I can attempt to reproduce it. 如果那不能帮助您解决问题,请提供所有相关代码,以便我尝试重现它。

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

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