0

好的......我有一些没有逻辑意义的东西,我希望有人能指出我犯的愚蠢错误。我有一个页面,我希望菜单更改用户是否登录。我正在使用会话变量 $_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>

好的......此时,当页面加载时,在菜单顶部我看到调试消息“不确定这里发生了什么”,因为我还没有登录。然后我单击登录链接并通过 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 回调应该重新加载菜单,就像 index.php 文档准备好的初始加载一样。通过调试,我确实看到它确实重新加载了它,但它从未在会话变量中看到任何内容,因此即使我已登录,它仍然显示登录链接而不是注销。

现在,把它们放在一起只是为了表明我没有疯,这是 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 数组时给我各种其他错误。

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

提前致谢。

4

1 回答 1

0

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

我很高兴它解决了,我可以继续前进。感谢大家的意见。

于 2012-04-13T13:03:48.050 回答