大家好日子,我是PHP新手。我查了w3c并得到了一些很好的建议。但是我的登录代码遇到了一个小小的挑战。它显示了PHP警告:无法修改标头信息 - 第8行/home/perspect/public_html/jst/index.php中已经发送的标头(在/home/perspect/public_html/jst/dbcon.php:2处开始输出) 下面是我的索引页面;
<?php
session_start();
include_once 'includes/dbcon.php';
if(isset($_SESSION['user'])!="")
{
header("Location: candidate_list.php");
}
if(isset($_POST['btn-login']))
{
$username = mysqli_real_escape_string($con, $_POST['username']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$stat = 'notvoted';
$res=mysqli_query($con,"SELECT * FROM students WHERE username='$username' AND status='$stat'");
$row=mysqli_fetch_array($res);
if($row['password']==md5($password))
{
$_SESSION['user'] = $row['user_id'];
header("Location: candidate_list.php");
}
else
{
?>
<script>alert('wrong details');</script>
<?php
}
}
?>
<!-- Login Box -->
<div class="col-md-6 col-md-offset-3 col-sm-offset-3">
<form class="login-page" action="" method="post">
<div class="login-header margin-bottom-30">
<h2>Login</h2>
</div>
<div class="input-group margin-bottom-20">
<span class="input-group-addon">
<i class="fa fa-user"></i>
</span>
<input placeholder="VIN" name="username" class="form-control" type="text">
</div>
<div class="input-group margin-bottom-20">
<span class="input-group-addon">
<i class="fa fa-lock"></i>
</span>
<input placeholder="Password" name="password" class="form-control" type="password">
</div>
<div class="row">
<div class="col-md-6">
<button class="btn btn-primary pull-right" type="submit" name="btn-login">Login</button>
</div>
</div>
<p>
</form>
</div>
<!-- End Login Box -->
我的candidate_list.php代码是;
<?php
session_start();
include_once 'includes/dbcon.php';
if(!isset($_SESSION['user']))
{
header("Location: index.php");
}
$res=mysqli_query($con, "SELECT * FROM students WHERE user_id=".$_SESSION['user']);
$userRow=mysqli_fetch_array($res);
?>
我的dbcon是
<?php
$con=mysqli_connect("xxx","xxx","xxxx","xxx");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
感谢您的帮助。
答案 0 :(得分:1)
一旦你在标题重定向之前回显一些东西,就会出现这种类型的php警告。
如果您仍想重定向,可以使用ob_start,也可以使用javascript
window.location.href =路径