会话变量未设置第一次但如果重新加载则设置

时间:2014-03-07 11:24:49

标签: javascript php jquery ajax session

我有2个文件。

  1. Home.php
  2. some_func.php
  3. 我在home.php中设置了一个会话变量,我在点击按钮时对some_func.php进行了ajax调用。

    问题:

    当我第一次加载页面时,会话变量在home.php中设置,但是相同的内容没有反映在some_func.php中,如果我重新加载home.php然后会话变量值反映在some_func.php。

    我不知道为什么第一次加载home.php时会反映会话变量值。

    我发现这只发生在我的实时服务器上而不是我的本地主机上。

    请帮忙。

    <?php
    //home.php file
    session_start();
    include 'db.inc.php';
    $db =  mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die('Unable to connect. Check connection parameters');
    mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));
    
    //Checking what is the latest session id available available in my db.
    $query = "SELECT max(ses_id) as max_ses_id FROM `dyn_sess` ";
    $result = mysql_query($query, $db) or die(mysql_error());
    while ($row = mysql_fetch_assoc($result)) 
    {
        $_SESSION['curr_sess'] = $row['max_ses_id']+1; 
    }
    
    echo '<script> alert("Your Session ID : '.$_SESSION['curr_sess'].'");</script>';
    ?>
    
    <html lang="en">    
    <head>    
    </head>
    
    <body>
        <meta charset="utf-8">
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    
        <script type="text/javascript">
                function call_some_func()
                {
                    $.post('some_func.php');
    
                }
        </script>
    
        <input type="button" value="PREV" onClick=call_some_func()>
    
        </body>
    </html>
    

    <?php
    //some_func.php
        session_start();
        require_once('lib/something.php');
        // some php code here.
        // No sessions variable are set. But the session variable curr_sess is used to call an api and that goes as blank for the first time.
        //If I reload home.php it works fine then after from the second time.
    ?>
    
    <html>
    <script> alert(<?php echo $_SESSION['curr_sess'] ?>); </script>
    </html>
    

0 个答案:

没有答案
相关问题