2015-07-04 52 views
-2

我有一个会话变量,它是一个数组,应该存储不同的用户名。当用户尝试登录时,会根据数组检查用户名是否存在于数组中。如果未在阵列中找到该用户,则将用户重定向到注册页面,用户可在其中输入用户名和密码。PHP - 维护页面之间的会话数组

此页面在接受用户名和密码后应更新会话数组,以便下次用户尝试登录时将其重定向到其他页面。

我能够注册,但认为每次我回到我的主页时,用户名数组被刷新为包含0个条目。

任何方式,我可以使我的数组更持久?

products.php

<?php 
    session_start(); 
?> 

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title>Studen Project #6 - M.M.</title> 
     <link rel="stylesheet" href="mystyles.css" /> 
    </head> 
    <body> 
     <h1>Product Listings</h1> 

     <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"> 
      Username: <input type="text" name="username" /><br> 
      Password: <input type="password" name="password" /><br><br> 
      Enter a Quantity for Each Product<br><br> 
      Pencils: <input type="number" name="pencils" /><br> 
      Notebooks: <input type="number" name="notebooks" /><br> 
      Folders: <input type="number" name="folders" /><br><br> 
      <input type="submit" /> 
     </form> 

     <h2>Dixon Ticonderoga Wood-Cased Pencils</h2> 
     <h3>$2.88</h3> 
     <img src="http://ecx.images-amazon.com/images/I/41OAcvBFqXL.jpg" alt="pencil" /> 
     <p>The World's Best Pencil with an exclusive #2 HB graphite core formula provides extra smooth performance</p> 

     <h2>Five Star Stay-Put Pocket Folder</h2> 
     <h3>$5.49</h3> 
     <img src="http://ecx.images-amazon.com/images/I/71HaaqlhilL._SL1280_.jpg" alt="folder" /> 
     <p>Durable plastic folder helps keep sheets protected and in one place; great for reports, projects, as a take-home folder and for storage</p> 

     <h2>Five Star Wirebound Notebook</h2> 
     <h3>$18.98</h3> 
     <img src="http://ecx.images-amazon.com/images/I/61NgdQwSjIL._SL1000_.jpg" alt="notebook" /> 
     <p>Five-subject plastic cover notebook has 200 college-ruled, 11 x 8.5 inch, 3-hole punched sheets</p> 

     <?php 
      $usernames = array(); 
      $_SESSION["usernames"]; 
      $_SESSION["quantity_total"]; 
      $_SESSION["username"]; 
      $_SESSION["pencils"]; 
      $_SESSION["folders"]; 
      $_SESSION["notebooks"]; 

      if($_SERVER["REQUEST_METHOD"] === "POST") { 
       $_SESSION["usernames"] = $usernames; 
       $_SESSION["username"] = $_POST["username"]; 
       $_SESSION["pencils"] = $_POST["pencils"]; 
       $_SESSION["folders"] = $_POST["folders"]; 
       $_SESSION["notebooks"] = $_POST["notebooks"]; 

       if(!in_array($_SESSION["username"], $_SESSION["usernames"])) { 
        header("Location:registration.php"); 
        exit(); 
       } else { 
        $_SESSION["quantity_total"] = $_SESSION["pencils"] * 2.88 + 
         $_SESSION["folders"] * 5.49 + $_SESSION["notebooks"] * 18.98; 
        header("Location:preview.php"); 
        exit(); 
       } 
      } 
     ?> 
    </body> 
</html> 

registration.php的

<?php 
    session_start(); 
?> 

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title>Student Project #6 - M.M.</title> 
     <style> 
      body { 
       background-color: lightgreen; 
       margin: auto; 
       width: 75%; 
       text-align: center; 
      } 
      h1 { 
       color: blue; 
       text-decoration: underline; 
      } 
      img { 
       width: 100px; 
       height: 100px; 
      } 
      form { 
       padding: 5px; 
       background-color: lightblue; 
       font-weight: bold; 
       font-family: Arial; 
      } 
     </style> 
    </head> 
    <body> 
     <h1>Register Here!</h1> 
     <img src="http://0.media.dorkly.cvcdn.com/36/35/6603dc5a9292104b44c349b85b5aaf7a-5-crazy-fan-theories-that-make-total-sense.jpg" 
      alt="thumbsup"><br> 
     <form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>"> 
      Username: <input type="text" name="username"><br> 
      Password: <input type="password" name="password"><br> 
      <input type="submit" /> 
     </form> 

     <?php 
      if($_SERVER["REQUEST_METHOD"] === "POST") { 
       array_push($_SESSION["usernames"], $_POST["username"]); 
       header("Location: products.php"); 
      } 
     ?> 
    </body> 
</html> 
+0

你自己做了什么调试吗?预计访问者所做的不仅仅是将一层代码粘贴到问题中。如果你看到一个特定的,可重现的问题,你可以减少到一个以前没有问过的最小例子,那就是提问的时候了。堆栈溢出绝不应该成为您尝试的第一个地方。 – Anonymous

+0

感谢您提供所有有用的信息。是的,我已经完成了调试,但我是PHP新手,并不确定发生了什么。我张贴到这个论坛期待指导,而不是只是被告知“尽力而为”。 – Delfino

+0

您可能想阅读[如何问问指南](http://stackoverflow.com/help/how-to-ask)了解更多信息。 – Anonymous

回答

2

你可能会考虑重新思考背后存储用户/用户名及其在会话属性列表的逻辑。 随着时间的推移,会议将变得越来越大,你将会遇到更多问题。

取而代之,将该信息存储在数据库中,并在需要时咨询。

相对于你的问题,你用会话阵列被重置为提交的数据是由这引起了之后的问题:

#line 41 $usernames = array(); <--- variable set to an empty array 
... 
      if($_SERVER["REQUEST_METHOD"] === "POST") { 
#line 50  $_SESSION["usernames"] = $usernames; <---- session variable affected with an empty array 
       $_SESSION["username"] = $_POST["username"]; 
... 

希望它能帮助。祝你好运