douqinlu4217 2013-10-25 00: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-25 01: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.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题