简体   繁体   English

PHP - 维护页面之间的会话数组

[英]PHP - Maintain Session Array Between Pages

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.我可以注册,但认为每次返回主页时,用户名数组都会刷新为包含 0 个条目。

Any way I can make my array more persistent?有什么办法可以让我的阵列更持久?

products.php产品.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

<?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祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM