dsdapobp26141 2014-03-24 14:43
浏览 12
已采纳

登录后的PHP空白页面

I'm building a CMS for a website. The problem is that after the login a blank page appears and it stays until I hit refresh. Then it loads to the correct menu page and everything else is working correctly except this little detail. Any tips to solve this? Thanks, my code is below:

    <?php

session_start();

include_once('../includes/connection.php');
if(isset($_SESSION['logged_in'])) {
    //display index
    ?>

    <html>
    <head>
        <meta charset="UTF-8">
        <title>AdminENG</title>
        <link rel ="stylesheet" href="../assets/style.css"/>
    </head>

        <body>
            <div class="container">
                CMS - ENG
                <ol>

                    <li><a href ="add.php">Add Article</a></li>
                    <li><a href ="delete.php">Delete Article</a></li>
                    <li><a href ="logout.php">Logout</a></li>
                </ol>
            </div>
        </body>
    </html>

    <?php
}
else {
    //display login
    if(isset($_POST['username'], $_POST['password'])) {
        $username = $_POST['username'];
        $password = md5($_POST['password']);

        if (empty($username) || 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 the correct details
                $_SESSION['logged_in'] = true;

                header('Location: index.php');
                exit();
            }
            else {
                //user entered false details
                $error = "Incorrect details!";
            }
        }
    }

    ?>

    <html>
    <head>
        <title>AdminENG</title>
        <meta charset="UTF-8">
        <link rel ="stylesheet" href="../assets/style.css"/>
    </head>

        <body>
            <div class="container">
                CMS
                <br><br>

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

                <?php } ?>

                <br><br>

                <form action="index.php" method="post">
                    <input type ="text" name="username" placeholder="Username"/>
                    <input type="password" name="password" placeholder="Password"/>
                    <input type="submit" value="Login"/>
                </form>
            </div>
        </body>
    </html>

    <?php
}
?>
  • 写回答

2条回答 默认 最新

  • doujiunai2169 2014-03-24 14:48
    关注

    You can't use Header after you execute html to the browser. Try replace this: header('Location: index.php');

    With this:

    <script>window.location="index.php";</script>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了