问题描述:

I have a session variable that's an array and is supposed to store different usernames. Upon a user trying to log in, the username is checked against the array to see if the name exists within the array. If it's not found within the array the user is re-directed to a registration page, where the user can enter in a username and password.

This page, upon accepting the username and password, is supposed to update the session array, so that the next time the user tries logging in he/she is redirected to a different page.

I am able to register, but think that each time I go back to my main page the usernames array is refreshed to contain 0 entries.

Any way I can make my array more persistent?

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>

网友答案:

You might consider rethinking the logic behind storing the list of users/usernames and their properties in the session. With time, sessions will get bigger and bigger and you're going to have more problems down the line.

Instead, store that information in a database and consult it when needed.

Relative to your issue, the problem you're having with the session array being reset after the data is submitted is caused by this:

#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"];
...

Hope it helps. Good luck

相关阅读:
Top