douyanning3724 2019-04-01 06:18
浏览 101
已采纳

如何访问另一个网页中的一个php变量

Facing some problem while accessing session variable in another web page

i have tried using jquery to refresh the page which create the session(data.php) also the database connection and sql query works fine, tested them independently.

test.php

    <?php
    session_start();
    ?>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>

    </head>
    <body>
         <div id="show">   </div>

         <script type="text/javascript" src="jquery.js"></script>
         <script type="text/javascript">
                $(document).ready(function() {
                setInterval(function () {
                $('#show').load('data.php')  // reloades data.php repetedly
                }, 1000);
        });
        </script>

        <?php   echo $_SESSION["id"];  ?>
    </body>
    </html>

data.php

<?php    
session_start();


$conn = new mysqli("localhost","id6207501_datausername","123456789","id6207501_dataname");
    if ($conn->connect_error) {
    die("Connection error: " . $conn->connect_error);
    }
$result = $conn->query("SELECT status,id FROM logs order by id desc limit 1");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
    $_SESSION["id"]=$row['id'];
    $_SESSION["status"]=$row['status'];

     }
}

?>

i expect the test.php to print the value of session variable $_SESSION["id"], which it does not.

  • 写回答

3条回答 默认 最新

  • dpthuyh1678 2019-04-01 07:20
    关注

    You have to call AJAX get method and set return your text in data.php file.

    test.php

     <!DOCTYPE html>
     <html>
     <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <title></title>
    
    <head>
    
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script type="text/javascript">
        setInterval(function () {
    
                $.get("data.php", function(data, status){
                    $('#show').html(data);
                });
                }, 1000);
        </script>
    
     </head>
     <body>
        <div id="show"> <?php   echo $_SESSION["id"];  ?>  </div>
     </body>
     </html>
    

    data.php

    <?php    
    session_start();
    
    // You can uncomment it and test
    
    // $conn = new mysqli("localhost","id6207501_datausername","123456789","id6207501_dataname");
    //     if ($conn->connect_error) {
    //     die("Connection error: " . $conn->connect_error);
    //     }
    // $result = $conn->query("SELECT status,id FROM logs order by id desc limit 1");
    // if ($result->num_rows > 0) {
    // while ($row = $result->fetch_assoc()) {
    //     echo $_SESSION["id"]=$row['id'];
    //     echo $_SESSION["status"]=$row['status'];
    
    //      }
    // }
    
        // Comment when you uncomment DB
        // FOR example purpose 
        $_SESSION["id"]= rand(1,2000);
    
        echo $_SESSION["id"];
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题