doudizhi947129
2013-04-02 19:34
浏览 44
已采纳

PHP会话变量未在其他页面上显示

I have a page a to which I am posting the data from a python script. I am processing that data, storing the data into SQL. I want to store the time when this post request is made. Note that the post request is made continuously. So the session variable keeps on changing. Well here is the little code for that-

<?PHP
session_start();
include 'db.inc.php';

if(isset($_POST['data']){
$data_decode = (array)json_decode($_POST['data']);
$data_var1 = $data_decode['var1'];
$data_var2 = $data_decode['var2'];
$data_var3 = $data_decode['var3'];
$date = date('Y-m-d H:i:s');
$_SESSION['time'] = $date;  //Set the session with current time
$sql = "UPDATE `table` SET row1=$data_var1, row2=$data_var2 WHERE row3=$data_var3";
$result = mysqli_query($db,$link) or die('Table could not be updated');
echo "Following data inserted: row1=$data_var1, row2=$data_var2, at $_SESSION['time']"; //Echoing the data and time as expected
?>

Now, I have another page on the same domain, which makes the ajax request and fetches out the data every second from the SQL table I just inserted the data into. So here is the whole scenario- The client python script makes the post request to the webpage, which updates the sql with the received data, sets the session variable. Another page simultaneously fetches that data out using ajax and shows it.

Now I am calling the session variable on another page as shown-

<?php
session_start();
if(isset($_SESSION['time']){ echo $_SESSION['time']; } //Didn't echoes anything
?>

I used <?php print_r($_SESSION) ?> to check whether the session variable is set or not, it echoed "Array()", which means my session variable is not set. So my question is, What I am doing wrong and how can I correct it?

图片转代码服务由CSDN问答提供 功能建议

我有一个页面,我从python脚本发布数据。 我正在处理该数据,将数据存储到SQL中。 我想存储发布此帖子请求的时间。 注意发布请求是连续的。 因此会话变量不断变化。 那么这里有一些小代码 -

 &lt;?PHP 
session_start(); 
include'db.inc.php'; 
 
if(isset($  _POST ['data']){
 $ data_decode =(array)json_decode($ _ POST ['data']); 
 $ data_var1 = $ data_decode ['var1']; 
 $ data_var2 = $ data_decode ['var2  ']; 
 $ data_var3 = $ data_decode ['var3']; 
 $ date = date('Ymd H:i:s'); 
 $ _SESSION ['time'] = $ date; //设置 与当前时间的会话
 $ sql =“UPDATE`table` SET row1 = $ data_var1,row2 = $ data_var2 WHERE row3 = $ data_var3”; 
 $ result = mysqli_query($ db,$ link)或die('表可以 不被更新'); 
echo“插入数据后:row1 = $ data_var1,row2 = $ data_var2,at $ _SESSION ['time']”; //按预期回显数据和时间
?&gt; 
 <  / code>  
 
 

现在,我在同一个域上有另一个页面,它生成ajax请求,并从我刚刚插入数据的SQL表中每秒取出数据。所以这里 整个场景 - 客户端python脚本向网页发出post请求,该页面用receiv更新sql ed data,设置会话变量。 另一个页面同时使用ajax取出该数据并显示它。

现在我在另一个页面上调用会话变量,如图所示 -

 &lt;?php 
session_start(); 
if(  isset($ _ SESSION ['time']){echo $ _SESSION ['time'];} //没有回应任何东西
?&gt; 
   
 
 

我用&lt;?php print_r($ _ SESSION)?&gt; 检查会话变量是否设置,它回显“Array()”,这意味着我的会话变量没有设置。所以 我的问题是,我做错了什么以及如何纠正它?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dttl3933 2013-04-02 21:20
    已采纳

    I am trying to reconstruct which components are acting in your scenario.

    So first you mention a Python script. How does it work together with PHP? Does it actually emit a HTTP request itself, or does it only send HTML to a browser that is then going to PHP? If Python is acting like a HTTP client, this is important, because PHP sessions use cookies, so your Python script has to be able to accept the "Set-cookie" HTTP header and deal with it, especially send it back on later requests.

    Second info is that there is some Ajax going on. This points to a browser and some Javascript as a client, and this combo is clearly able to accept any cookie that is set by PHP.

    But how do the browser and the Python script communicate? How does the browser know which cookie the Python script got back from PHP so that the session data can be loaded?

    I hope you see the hole in your approach. Posting data from Python into the database is one thing, saving the time of this event probably is a global information (date of last DB update), and not a session information (is different per Python client). As such, the session is the wrong location for storage. And the other obstacle is that your Ajax client cannot share the session ID from your Python client! And the last downside to sessions: If it would work, you would constantly interfere with both requests, because sessions use locking to prevent two requests deleting each others data. This is especially bad because a hanging Ajax request might prevent the Python script from writing to the database, and vice versa.

    Find a way to store a global information on your server. You already have a database, why not use it for this?

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douna5529 2013-04-02 19:36

    Make sure to call session_start() at the beginning of every page...

    You forgot the semicolon after the declaration.

    $_SESSION['time'] = $date
    

    should be

    $_SESSION['time'] = $date;
    
    评论
    解决 无用
    打赏 举报
  • drlh197610 2013-04-02 19:36

    You forgot session_start()

    <?php
    session_start();
    if(isset($_SESSION['time']){ echo $_SESSION['time']; } //Didn't echoes anything
    ?>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题