1

这是我的 index.php 的内容。当我在我的本地主机上测试它时,它按预期工作。一旦用户提交了用户名和密码,页面就会刷新,并显示一个新页面。当我通过登录时出于某种原因上传到我的服务器时,一个页面只加载了标题。如果按下后退按钮或单击刷新按钮,则应该在登录后显示的 html 确实显示出来。知道为什么在登录成功并且用户被重定向到同一页面后它不能立即工作吗?这是我的代码:

<?php

session_start();

include_once('../includes/connection.php');
include_once('header.php');

if (isset($_SESSION['logged_in'])) {
//display index
?>
<div id="inner-wrap">
    <div class="page_title" onclick="window.location = 'index.php';">Admin</div>

    <div class="admin">
        <div class="admin_sec">
        <u>Manage Works</u><br>
        <a href="img_upload.php">upload image</a><br>
        <a href="img_delete.php">delete image</a>
        </div>

        <div class="admin_sec">
        <u>Manage Bio</u><br>
        <a href="bio_edit.php">edit bio</a>
        </div>

        <div class="admin_sec">
        <u>Manage Blog</u><br>
        <a href="../blog/wp-login.php">login to blog</a>
        </div>

        <div class="admin_sec">
        <u>Manage Links</u><br>
        <a href="link_add.php">add link</a><br>
        <a href="link_delete.php">delete link</a>
        </div>
    </div>

    <div class="logout">
        <div class="page_title" onclick="window.location='logout.php';">Logout</div>    
    </div>
 </div> 
  </body>
 </html>
 <?php
} else {
//display login
if (isset($_POST['username'], $_POST['password'])) {
    $username = $_POST['username'];
    $password = md5($_POST['password']);

    if (empty($username) or empty($password)) {
        $error = 'All fields are required.';        
    } else {
        $query = $pdo->prepare("SELECT * FROM users WHERE user_name = ? AND user_password = ?");
        $query->bindValue(1, $username);
        $query->bindValue(2, $password);

        $query->execute();

        $num = $query->rowCount();

        if ($num == 1) {
            //user entered correct details
            $_SESSION['logged_in'] = true;
            header('Location: index.php');
            exit();
        } else {
            //user entered incorrect username and/or password
            $error = 'The username and/or password are incorrect.';
        }
    }
}
?>

<div id="inner-wrap">
<div class="page_title" onclick="window.location = 'index.php';">Login</div>
<div class="admin_sec"> 
    <form action="index.php" method="post" autocomplete="off">
        <input type="text" name="username" placeholder="Username">
        <input type="password" name="password" placeholder="Password">
        <input type="submit" class="button" value="Login">
    </form>

    <?php if (isset($error)) { ?>
        <small style="color:#aa0000"><?php echo $error; ?></small>
    <?php } ?>
</div>
</div>  

</body>
</html>

<?php } ?>
4

2 回答 2

1

最简单的解决方法是首先放置登录逻辑。

session_start();

include_once('../includes/connection.php');
include_once('header.php');

if (!isset($_SESSION['logged_in'])) {
//display login
if (isset($_POST['username'], $_POST['password'])) {
        $username = $_POST['username'];
        $password = md5($_POST['password']);

        if (empty($username) or empty($password)) {
                $error = 'All fields are required.';        
        } else {
                $query = $pdo->prepare("SELECT * FROM users WHERE user_name = ? AND user_password = ?");
                $query->bindValue(1, $username);
                $query->bindValue(2, $password);

                $query->execute();

                $num = $query->rowCount();

                if ($num == 1) {
                        //user entered correct details
                        $_SESSION['logged_in'] = true;
                } else {
                        //user entered incorrect username and/or password
                        $error = 'The username and/or password are incorrect.';
                }
        }
}
}
if (isset($_SESSION['logged_in'])) {
//display index
?>
<div id="inner-wrap">
        <div class="page_title" onclick="window.location = 'index.php';">Admin</div>

        <div class="admin">
                <div class="admin_sec">
                <u>Manage Works</u><br>
                <a href="img_upload.php">upload image</a><br>
                <a href="img_delete.php">delete image</a>
                </div>

                <div class="admin_sec">
                <u>Manage Bio</u><br>
                <a href="bio_edit.php">edit bio</a>
                </div>

                <div class="admin_sec">
                <u>Manage Blog</u><br>
                <a href="../blog/wp-login.php">login to blog</a>
                </div>

                <div class="admin_sec">
                <u>Manage Links</u><br>
                <a href="link_add.php">add link</a><br>
                <a href="link_delete.php">delete link</a>
                </div>
        </div>

        <div class="logout">
                <div class="page_title" onclick="window.location='logout.php';">Logout</div>    
        </div>
 </div> 
    </body>
 </html>
 <?php
}else{?>
<div id="inner-wrap">
<div class="page_title" onclick="window.location = 'index.php';">Login</div>
<div class="admin_sec"> 
        <form action="index.php" method="post" autocomplete="off">
                <input type="text" name="username" placeholder="Username">
                <input type="password" name="password" placeholder="Password">
                <input type="submit" class="button" value="Login">
        </form>

        <?php if (isset($error)) { ?>
                <small style="color:#aa0000"><?php echo $error; ?></small>
        <?php } ?>
</div>
</div>  

</body>
</html>
<?php
}
于 2013-05-16T22:02:41.533 回答
0

尝试更改header(Location: index.php)为此header(Location: ./index.php)应该可以解决您的问题

于 2013-05-16T21:42:27.140 回答