0

好的,我正在为当地部队制作童子军网站。我制作了登录系统,出于某种原因,它让我在显示登录页面之前登录了两次。请帮我弄清楚它为什么这样做。当我登录时,我必须重新加载,然后它将显示信息

<!doctype html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css" />
<script src="jquery.js"></script>
<script src="script.js"></script>
  </head>
<body>
<div id="wrap">
     <h1 id="title">Welcome to Boy Scout Troop 416, ----------- -------</h1>

    <div id="left">
        <section class="part">
            <h1>About Us</h1>
            <p>Hello, we are Boy Scout Troop 416. Our Scout Master is       Mr. Kelly Barrett. We have camp-outs about once a mouth and our meetings are every Tuesday      at 7:00.</p>
        </section>

        <section class="part">
            <h1>FAQ</h1>
            <h2>When and Where are the Meetings?</h2>
            <p>The meetings are every Tuesday at The Williamsburg American Legion, Post 288 on Main Street from 7:00 to 8:30.</p>
            <h2>How do I sign up?</h2>
            <p>To sign up you can go to "Contact Information", and contact the Scout Master and tell him you want to join then, he will help you with the rest.</p>
            <h2>How much Does it cost?</h2>
            <p>To sign up it is about____. Every camp-out is about $3.00 for food. The uniforms are about____.</p>
            <h2>What will you learn?</h2>
            <p>You will learn how to use a:</p>
            <ul>
                <li>Knife</li>
                <li>Compass</li>
                <li>Bow and Arrow</li>
                <li>Axe</li>
                <li>Hatchet</li>
                <li>And much more!</li>
            </ul>
            <p>You will learn how to:</p>
            <ul>
                <li>Tie different kinds of knots</li>
                <li>Put up a tent</li>
                <li>Cook</li>
                <li>Treat a hurt person</li>
                <li>And a lot more!</li>
            </ul>
        </section>
    </div>

    <div id="middle">
        <?php
        require 'connect.inc.php';
        session_start();

        function getuserfield($field) {
            $query = "SELECT `$field` FROM `users` WHERE      `id`='".$_SESSION['user_id']."'";
            if ($query_run = mysql_query($query)) {
                if ($query_result = mysql_result($query_run, 0,   $field)) {
                    return $query_result;
                }       
            }
        }
        ?>

        <section class="part">
            <h1>Announcements</h1>
            <p>bla f gg keggnfm gw lorem isput g fgrh egyhe tryher t.   </p>
            <div class="div"></div>
            <p>Need help with merit badges, try <a href="http://meritbadge.org/wiki/index.php?title=Main_Page" target="_blank">Meritbadge.org</a>.</p>
        </section>

        <section class="part">
            <h1>Upcoming Events</h1>
            <table class="table" cellpadding="10px" cellspacing="0px">
                <tr>
                    <th>Events</th><th>Location</th>  <th>Date</th><th>Time</th>
                </tr>
                <tr>
                    <td>Camp-out</td><td>A Place</td><td>11/11/11</td><td>5:30pm</td>
                </tr>
                <tr>
                    <td>Camp-out</td><td>A Place</td><td>11/11/11</td><td>5:30pm</td>
                </tr>
                <tr>
                    <td>Camp-out</td><td>A Place</td><td>11/11/11</td><td>5:30pm</td>
                </tr>
            </table>
        </section>

        <section class="part">
            <h1>Contact Info.</h1>
            <table>
                <tr>
                    <td><img class="img_contact" src="pictures/person.png" /></td><td>Rick Shoe</td><td>email@example.com</td>
                </tr>
                <tr>
                    <td><img class="img_contact" src="pictures/person2.png" /></td><td>Raymond Kneipp jr.</td><td>email@example.com</td>
                </tr>
                <tr>
                    <td><img class="img_contact" src="pictures/person3.png" /></td><td>Bobby Star</td><td>email@example.com</td>
                </tr>
            </table>
        </section>
    </div>

    <div id="right">

        <?php
        if (isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])) {
        echo '
            <section class="part">
                <h1>Account</h1>
                <p>Account Balance: '.getuserfield('money').'</p>
                <p>Rank: '.getuserfield('rank').'</p>
                <p><a href="logout.php"><button>Logout</button></a></p>
            </section>
            ';

        echo '
            <section class="part">
                <form action="index.php" method="POST">
                    <h1>Edit Account</h1>
                    <p>Password: <input type="password" name="" maxlength="50" /></p>
                    <p>New Password: <input type="newPassword2" name="" maxlength="50" /></p>
                    <p>New Password: <input type="newPassword" name="" maxlength="50" /></p>
                    <p><a href="logout.php"><button>Logout</button></a></p>
                </form>
            </section>
            ';

        } else {
        ?>
        <section class="part">
            <h1>Login</h1>
            <form action="index.php" method="POST">
                <table>
                    <tr>
                        <td>Username:</td><td><input type="email" name="username" value="<?php if(isset($_COOKIE['remember'])) { echo $_COOKIE['remember']; } ?>" maxlength="50"/></td>
                    </tr>
                    <tr>
                        <td>Password:</td><td><input type="password" name="password" maxlength="50"/></td>
                    </tr>
                    <tr>
                        <td>Remember Me<input type="checkbox" value="remember" /></td><td><input type="submit" value="Login" /></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <?php

                            if (isset($_POST['username']) && isset($_POST['password'])) {
                                $username = $_POST['username'];
                                $password = $_POST['password'];
                                $password_hash = md5($password);
                                if (!empty($password) && !empty($username)) {
                                    if (strlen($username) <= 50 && strlen($username) >= 5 && strlen($password) <= 50 && strlen($password) >= 5) {
                                        $query = "SELECT `id` FROM `users` WHERE `email` = '$username' AND `password` = '$password_hash'";
                                        $query_run = mysql_query($query);
                                        if (mysql_num_rows($query_run) == 0) {
                                            echo 'Username or Password is incorrect.';
                                        } else if (mysql_num_rows($query_run) == 1) {
                                            $user_id = mysql_result($query_run, 0, 'id');
                                            $_SESSION['user_id'] = $user_id;
                                            if (isset($_POST['remember'])) {
                                                setcookie('remember', $username, time()+86400);
                                            }
                                        } else {
                                            echo 'Username or Password is incorrect.';
                                        }
                                    } else {
                                        echo 'Password or username is too long or too short.';
                                    }
                                } else {
                                    echo 'All fields required';
                                }
                            }

                            ?>
                        </td>
                    </tr>
                </table>
            </form>
        </section>
        <?php
        }
        ?>

        <section class="part">
            <h1>Photos</h1>
            <a href="gallery.php"><img class="slide_img" src="pictures/fall.jpg" /></a>
            <div id="play_menu">
                <button id="back">◄&lt;/button>
                <button id="go">►&lt;/button>
            </div>
            <h2><a href="gallery.php">Click to Browse Gallery</a></h2>
        </section>

        <section class="part">
            <h1>Share</h1>
            <p>
                <a href="https://www.facebook.com/DBCTroop416" target="_blank"><img class="share" src="pictures/fb.png" /></a>
                <a href="https://twitter.com/" target="_blank"><img class="share" src="pictures/t.png" /></a>
                <a href="http://instagram.com/" target="_blank"><img class="share" src="pictures/ig.jpg" /></a>
                <a href="https://www.pinterest.com/" target="_blank"><img class="share" src="pictures/p.png" /></a>
            </p>
        </section>

        <section class="part">
            <h1>Scout Rules</h1>
            <div class="center">
            <h2>Pledge of Allegiance</h2>
            <p>"I pledge allegiance to the Flag of the United States of America, and to the Republic for which it stands, one Nation under God, indivisible, with liberty and justice for all."</p>
            <h2>Scout Law</h2>
            <p>"A scout is trustworthy, loyal, helpful, friendly, courteous, kind, obedient, cheerful, thrifty, brave, clean, and reverent."</p>
            <h2>Scout Oath</h2>
            <p>"On my honor I will do my best To do my duty to God and my country and to obey the Scout Law; To help other people at all times; To keep myself Physically strong, mentally awake, and morally straight."</p>
            <h2>Scout Motto</h2>
            <p>"Be Prepared."</p>
            <h2>Scout Slogan</h2>
            <p>"Do a Good Turn daily."</p>
            </div>
        </section>
    </div>
</div>

<footer id="foot">
    <p>Version: 3.0.0</p>
    <p>This website is hosted, managed, and was created by:</p>
    <h1><a href="#">Biz Name</a></h1>
</footer>
</body>
</html>
4

2 回答 2

0

我很惊讶它最终会登录 - 您在评估回发结果之前输出了大量数据,这意味着setcookie调用应该失败,因为标头已经发送。由于隐式输出缓冲,它可能会意外工作。

真正的问题当然是在检查回发数据之前和输出条件数据之后输出整个登录表单。您的代码是按顺序执行的,如果您认为很明显您的代码无法工作。

在生成任何数据之前,您应该始终在代码顶部评估回发,并且如果在 POST 上更改状态,最佳做法是向自己发出 303 重定向以消除 F5'able 错误,例如:

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
  // Check login stuffs, and if succesful redirect
  if($succesfully_logged_in)
  {
    header('HTTP/1.1 303 See other');
    header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    die;
  }
}

?><!doctype html> ...and the rest of your code...
于 2013-10-25T01:46:45.603 回答
0

session_start 需要在您的 doctype 声明下方。

PHP:必须登录两次,直到设置会话变量

你也可以看看这个讨论。

于 2013-10-25T01:12:27.980 回答