我目前正在学习 PHP,并正在创建一个包含登录区域的小型 CMS 功能。我使用了下面的代码,其中包含一个包含头文件,其中包含 doctype/head 信息和开始标记。它还包括标题内容。我还有一个用于连接数据库的连接文件。
我的 header 包含代码是:
<!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"> </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>
最佳答案
如果您在将 header 包含在其中时已经将内容(即 html 代码)输出到浏览器,则不能使用 header('location: index.php');
行这一行 include_once('../includes/headeradmin.php');
阅读 header 的文档- 请记住,在发送任何实际输出之前,必须调用 header(),无论是通过正常的 HTML 标记、文件中的空行,还是从 PHP
在输出管理页面的 head html 之前,您需要使用 header() 函数重定向用户
关于php - 无法使用登录功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18807460/