PHP $ _SESSION数组填充在一个div中,而在另一个div中填充

时间:2012-04-13 04:23:33

标签: php jquery ajax session

好吧......我有一些没有逻辑意义的东西,我希望有人可以指出我正在犯的愚蠢错误。我有一个页面,我希望菜单更改用户是否登录。我使用会话变量$ _SESSION ['is_logged_in]来存储登录状态。因此,例如,如果注销,菜单应该说登录。如果登录,则应该说出相同的菜单。这是逻辑部分中的代码。我提前为所有代码片段道歉,但我希望尽可能清楚。请帮忙!

首先,我有一个名为index.php的主页,它有一些空的div:

<div id="wrapper">
   <div id="header">
      <h1>My Page</h1>
      <div id="navigation">
      </div> <!-- end of navigation div -->
   </div> <!-- end if header div -->
   <p>&nbsp;</p>
   <div id="mainContent">
   </div><!-- end mainContent div -->
</div> <!-- end wrapper div -->

在这个页面(index.php)中,我在文档就绪时有一个导航div的jquery加载:

<script type="text/javascript">
  $(document).ready(function(e) {
      //load the default menu
      $('#navigation').load("menu.php");
   });
</script>

好的...现在menu.php有以下html / php:

<?php 
   session_start();
   include 'php/functions.php';

   //following is for debugging, you will see later where this is useful
   if (is_logged_in())
   {     
     echo "logged in value: " . is_logged_in() . "<br />"; 
     print_r($_SESSION);
   }
   else
 echo "not sure what is going on</br >";
   echo "<br />"; 
?>
  <ul id="menu" class="DropdownMenu">
     <li class="sub"><a href="#">Account</a>
        <ul>
           <?php 
              //this is where it should show one link or the other based on the value in $_SESSION
              if (is_logged_in()) { 
                echo "<li><a href=\"#\" class=\"ClickLogout\">Log Out</a></li>\n";
              } else {
                echo "<li><a href=\"#\" class=\"ClickLogin\">Log In</a></li>\n";
              } 
           ?>
        </ul>
     </li>
  </ul>

好的......此时,当页面加载时,在菜单顶部,我看到调试消息“不确定这里发生了什么”,因为我还没有登录。然后我点击Login链接并通过ajax填充index.php中的mainContent div:

这是在menu.php:

$(document).ready(function(e) {
   var options = { 
      target:        '#mainContent',   // target element(s) to be updated with server response 
      success:       reloadMenu,  // post-submit callback 
      delegation:    true
   }; 

   // post-submit callback 
   function reloadMenu(responseText, statusText, xhr, $form)  { 
      $('#navigation').load("menu.php");
   }; 

   //set the jquery form plugin for the login form
   $('.ContentForm').ajaxForm(options);

   //load the login form in the mainContent div
   $('#wrapper').on("click", ".ClickLogin", function(e) {
      $('#mainContent').load("login.php");
   });

});

上面的代码显示,在提交表单时,成功时,post submit callback应该重新加载菜单,就像index.php文件的初始加载就绪一样。通过调试,我确实看到它重新加载它,但它从未在会话变量中看到任何内容,所以即使我已登录,它仍然显示Login链接而不是注销。

现在,把它们放在一起只是为了表明我并不疯狂,这就是login.php中的内容:

<?php
   // start the session
   session_start();
   include 'php/functions.php';

   //this sections handles the post of the form
   $show_form = true;
   if($_SERVER['REQUEST_METHOD'] == "POST") 
   {
      $email = $_POST['email'];
      $password = $_POST['password'];

      if (authenticate_user($email, $password)) //this will set $_SESSION variables
      {
         $show_form = false;
         echo "Congratulations " . get_user_name() . ", you are logged in <br />";

         //following is for debugging, you will see later where this is useful
         if (is_logged_in())
         {   
            echo "logged in value: " . is_logged_in() . "<br />"; 
            print_r($_SESSION);
         }
         else
            echo "not sure what is going on</br >";
      }
      else
      {
         echo "Invalid email or password. Try again <br />";
      }
   }
?>

<?php if ($show_form) { ?>
<div id="formContent">
   <h2 class="ContentHeader">Log In</h2>
   <form id="contentForm" name="registration" class="ContentForm" method="post" action=<?php echo $_SERVER['PHP_SELF']?>>
      <p>
        <label for="email">Email:</label>
        <input type="email" name="email" id="email" required="required">
     </p>
     <p>
        <label for="password">Password:</label>
        <input type="password" name="password" id="password" required="required">
     </p>
     <p>
        <input type="submit" name="insert" id="insertLink" value="Submit It" /> 
        <input type="reset" name="cancel" id="cancel" value="Reset" />
     </p>
     <p>Forgot Password? Click <a href="#">here</a></p>
     <p>Need to Register? Click <a class="ClickRegister" href="#">here</a></p>
  </form>

我知道这是很多代码所以我想指出一件事。此时,提交表单时,在#navigation div中重新加载menu.php,并在#mainContent div中重新加载login.php。两者都执行完全相同的代码:

     //following is for debugging, you will see later where this is useful
 if (is_logged_in())
 {   
    echo "logged in value: " . is_logged_in() . "<br />"; 
    print_r($_SESSION);
 }
 else
    echo "not sure what is going on</br >";

当登录表单提交后页面显示时,菜单上方显示“不确定发生了什么”,然后是“Array()”,但是在mainContent中,它显示“登录值:1”并打印输出所有会话值。

为了完整起见,is_logged_in()函数如下所示:

//return true if the current session is logged in
function is_logged_in()
{
   if (array_key_exists('is_logged_in', $_SESSION))
      return $_SESSION['is_logged_in'];
}

我在menu.php和login.php中都有session_start(),如果我没有,它在尝试访问$ _SESSION数组时会给我各种其他错误。

我真的希望有人能发现我的错误。请帮忙!

提前致谢。

1 个答案:

答案 0 :(得分:0)

正式回答这个问题。正如上面的评论中所提到的,我在将会话设置为登录的一部分之前就破坏了会话。我仍然不确定为什么它会产生效果,因为两个div都在破坏/重新创建后加载但可能有一些ajax竞争条件。

我很高兴它已经解决了,我可以继续前进。感谢大家的投入。