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

如何访问另一个网页中的一个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.

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

在访问另一个网页中的会话变量时遇到一些问题

我有 尝试使用jquery刷新创建会话的页面(data.php),数据库连接和sql查询工作正常,独立测试它们。

test.php

 &lt;?php 
 session_start(); 
?&gt;  
 
&lt;!DOCTYPE html&gt; 
&lt; html&gt; 
&lt; head&gt; 
&lt; meta charset =“utf-8”&gt; 
&lt; meta http-equiv =“X-UA- 兼容的“content =”IE = edge“&gt; 
&lt; title&gt;&lt; / title&gt; 
 
&lt; / head&gt; 
&lt; body&gt; 
&lt; div id =”show“&gt;  &lt; / div&gt; 
 
&lt; script type =“text / javascript”src =“jquery.js”&gt;&lt; / script&gt; 
&lt; script type =“text / javascript”&gt; 
 $  (document).ready(function(){
 setInterval(function(){
 $('#show')。load('data.php')// reloades data.php repetedly 
},1000);  
}); 
&lt; / script&gt; 
 
&lt;?php echo $ _SESSION [“id”];  ?&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
   
 
 

data.php

 &lt;?php 
session_start(); 
 
 
 $ conn = new mysqli(“localhost”,“id6207501_datausername”,“123456789”,“id6207501_dataname”); 
 if(n)  $ conn-&gt; connect_error){
 die(“连接错误:”。$ conn-&gt; connect_error); 
} 
 $ result = $ conn-&gt; query(“SELECT status,id FROM logs order by  id desc limit 1“); 
if($ result-&gt; num_rows&gt; 0){
while($ row = $ result-&gt; fetch_assoc()){
 $ _SESSION [”id“] = $ row [  'id']; 
 $ _SESSION [“status”] = $ row ['status']; 
 
} 
} 
 
?&gt; 
   
  
 

我希望test.php打印会话变量 $ _ SESSION [“id”] 的值,但它没有。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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条)

相关推荐 更多相似问题