2

我目前正在学习 PHP,并且正在创建一个包含登录区域的小型 CMS 功能。我使用了下面的代码,其中包含一个包含 doctype/head 信息和开始标记的包含头文件。它还包括标题内容。我还有一个用于连接数据库的连接文件。

我的标题包含代码是:

<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title><?php echo $pagetitle ?></title>
<link rel="stylesheet" href="../stylesheets/foundation.css">
<link rel="stylesheet" href="../stylesheets/app.css">
<style>@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700,800); @import url (http://fonts.googleapis.com/css?family=Kreon:100,200,300,400);</style>

<script src="../javascripts/modernizr.foundation.js"></script>
</head>


<body>
<div class="subHeader">
<div class="row">
    <div class="four columns logo">
    <a href="../index.html"><img src="../images/logo.png" alt="logo" /></a>
    </div>
    <div class="eight columns navigation right">
    <ul class="navigationMain">
    <li class="<?php if($navcurrent == "home"){echo "navigationActive";} ?>"><a href="index.php">Home</a></li>
    <li class="<?php if($navcurrent == "services"){echo "navigationActive";} ?>"><a href="services.php">Services</a></li>
    <li class="<?php if($navcurrent == "work"){echo "navigationActive";} ?>"><a href="gallery.php">Recent Work</a></li>
    <li class="<?php if($navcurrent == "about"){echo "navigationActive";} ?>"><a href="about.php">About</a></li>
    <li class="<?php if($navcurrent == "contact"){echo "navigationActive";} ?>"><a href="contact.php">Contact</a></li>
    </ul>
    </div>

 <div class="twelve columns titlesection">
 <h2><?php echo $headTitle ?></h2>
 <h4><?php echo $headsubTitle ?></h4>
 </div>

 </div><!--End Feature Row-->
 </div><!--End Feature-->

 <div class="underbar">
<div class="bordertriangle"></div>
<div class="row">
        <div class="eight columns">&nbsp;</div>
        <div class="three columns right socialcontainer">
        <ul class="socialicons">
        <li><a><img id="linkedinIcon" src="../images/socialli.png" alt="linkedin icon" /></a></li>
        <li><a><img id="twitterIcon" src="../images/socialtw.png" alt="twitter icon" /></a></li>
        <li><a><img id="facebookIcon" src="../images/socialfb.png" alt="facebook icon" /></a></li>
        </ul>
        </div>
</div>

当我打开管理页面时,用户名密码表单、页眉和页脚会正常显示。如果我测试错误,它们会按原样返回。但是,当我使用有效的用户名和密码成功登录时,除了头文件中包含的内容外,没有其他内容出现。谁能指出我可能做错的方向?任何帮助将非常感激。我是PHP的相对菜鸟......

<?php 
$pagetitle = "Admin";
$navcurrent = "home";
$headTitle = "ADMIN AREA";
$headsubTitle = "SITE ADMINISTRATION AREA";
include_once('../includes/connection.php');
include_once('../includes/headeradmin.php');


if (isset($_SESSION['logged_in'])) {
echo('Successfully Logged In');
} else {
    if (isset($_POST['username'], $_POST['password'])){

        $username = $_POST['username'];
        $password = $_POST['password'];

        if (empty($username) or empty($password)){

            $error = 'An Error Has Occurred - 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) {
                $_SESSION['logged_in'] = true;

                header('location: index.php');
                exit(); 
                }
                else{

                $error = 'The username/password you entered was incorrect - Please try again';
                    }
                }
        }
    ?>

    <div class="row">
    <div class="four columns centered">

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

    <?php   } ?>

    <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>
    </div>

4

1 回答 1

3

如果您在此行中包含标题时已经将内容(即 - html 代码)输出到浏览器,则不能使用header('location: index.php');该行include_once('../includes/headeradmin.php');

阅读header的文档-请记住,header() 必须在发送任何实际输出之前调用,无论是通过普通 HTML 标记、文件中的空白行还是从 PHP

在输出管理页面的头部 html 之前,您需要使用 header() 函数重定向用户

于 2013-09-14T23:50:10.477 回答