dongliqin6939 2013-07-02 15:03
浏览 58

在为客户端显示存储的会话数据时,什么是正确的事件链

Is my following chain is correct?

First I display the client home.php which will create a session and will store inside $_SESSION['name'] = 'random_name' and will Greet end user client:

<?php
  session_start();
  $_SESSION['name'] = 'random_name';
?>


 <!DOCTYPE html>
 <html>
  <head>
        <title></title>
  </head>
  <body>
        <h1>Greeting</h1>
        <a id="1" href="index.php">link</a>
  </body>
  </html>  

Then I will press on the link which will take me to the second page index.php which will take me to another page which will display for user the value inside$_SESSION['name'] after certain logic inside the page will be executed as described below:

 <?php
    session_start();
  ?>
  <!DOCTYPE html>
  <html>
  <head>
        <script src="test.js"> </script>
        <title></title>
   </head>
   <body>
       <p id="para"></p>
       <input type="button" onclick="checkAuth()" value="submit">
   </body>
   </html>

Now when I press the button it will activate checkAuth() function inside test.js that will by using AJAX will retrieve from server side(damn.php) the value inside SESSION['name']:

 function checkAuth()
 {
       var xmlhttp;
       if (window.XMLHttpRequest)
       {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
       }
       else
       {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       }

              xmlhttp.onreadystatechange=function()
       {
       if (xmlhttp.readyState==4 && xmlhttp.status==200)
       {
          var response = xmlhttp.responseText;
          document.getElementById("para").innerHTML = response;
       }
   }

    xmlhttp.open("POST","damn.php",true);
    xmlhttp.send();
 }

Now damn.php looks like that:

       <?php

          session_start();

          if (isset($_SESSION['name'])) {
                   echo $_SESSION['name'];
          }

My question is that the easiest way there is to do it in order to display the client the items inside $SESSION is like that?

Is there a simpler way without using javascript at all? Is it also correct to insert session_start() in every page? or only from pages I want to retrieve the SESSION['items']?

  • 写回答

1条回答 默认 最新

  • duangutang3940 2013-07-02 15:09
    关注

    You could quite simply just have

    <?php
    
    session_start();
    $_SESSION['name'] = 'foo';
    
    ?>
    
    <script type="text/javascript">
    var name = <?php echo json_encode($_SESSION['name']) ; ?>;
    </script>
    

    If you know that you're going to need a particular bit of session data inside a web page, and that data's available at the time you're generating the page, then just embed that data. The only time you'd want to make an AJAX call to fetch the data is you think the data in the session might have changed before you actually need it in the javascript code.

    session_start() is required anywhere you want to use $_SESSION to save/retrieve data. If a particular page doesn't need to modify or access session data, then you don't need to session_start() at all.

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)