2015-07-04 19 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"]; 
... 

希望它能幫助。祝你好運