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?

  • 写回答

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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题