douqinlu4217 2013-10-24 16:58
浏览 60

PHP登录系统很奇怪

Okay, im am making a boy scout website for a local troop. I made the login system and for some reason it is making me login in twice before displaying the login page. Please help me figure out why it is doing this. when i login i have to reload, then it will display the information

<!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"></button>
                <button id="go"></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>

展开全部

  • 写回答

2条回答 默认 最新

  • dongshen4129 2013-10-24 17:12
    关注

    session_start needs to be up top just under your doctype declaration.

    PHP : Must login twice until session variables are set

    You might look into this discussion also.

    评论
  • dongsui5464 2013-10-24 17:46
    关注

    I'm surprised it logs in eventually at all - you're outputting a ton of data before evaluating the results of the postback, meaning the setcookie call should fail since headers have already been sent. It could accidentally work because of implicit output buffering.

    The true problem is of course that you're outputting the entire login form before even checking postback data, and after outputting conditional data. Your code is executed sequentially, and if you consider that it's obvious that your code can't work.

    You should always evaluate postbacks at the top of your code, before any data is generated, and if state is changed on a POST best practice is to issue a 303 redirect to yourself to get rid of the F5'able errors, for example:

    <?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...
    
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部