php - "Cannot modify header information - headers already sent by"验证、提交然后重定向时出现问题

标签 php html mysql validation

首先,我对 PHP 真的很陌生,而且真的很累,所以如果解决方案很简单,我深表歉意。 基本上我在大学学习 PHP,我的导师给了我一些代码粘贴到文档中进行测试,这些代码应该验证登录表单,然后将用户带到另一个页面并回显“welcome $username”

我已经有一个工作功能数据库(phpMYADMIN),我可以使用我作为包含插入的代码成功链接到该数据库。

<?php //Connect to phpMYADMIN database`enter code here`
    $db_hostname = 'localhost'; 
    $db_database = 'CollegeUsername_DB'; 
    $db_username = 'CollegeUsername'; 
    $db_password = 'password';  
    $db_status = 'not initialised'; 
    $str_result = ''; 

    $db_server = mysqli_connect($db_hostname, $db_username, $db_password); 
    $db_status = "connected"; 

    if (!$db_server){ 
        die("Unable to connect to the database: " . mysqli_connect_error()); 
    }
?>

无论如何,我的问题是登录验证代码,更具体地说是 header 函数。这是带有登录验证和登录表单的代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <?php 

     require_once("functions.php"); 

 $username = trim($_POST['username']); 

 $password = trim($_POST['password']); 

 if ($username&&$password){ 

 session_start(); 

 require_once("db_connect.php"); 

mysqli_select_db($db_server, $db_database) or 

die("Couldn't find db"); 

 $username = clean_string($db_server, $username); 

 $password = clean_string($db_server, $password); 

$query = "SELECT * FROM users WHERE username='$username'"; 

$result = mysqli_query($db_server, $query); 

if($row = mysqli_fetch_array($result)){ 

 $db_username = $row['username']; 

 $db_password = $row['password']; 

 if($username==$db_username&&salt($password)==$db_password){ 

 $_SESSION['username']=$username; 

 $_SESSION['logged']="logged"; 

 header('Location: home.php');

 }else{

 $message = "<h1>Incorrect password!</h1>"; 

 } 

 }else{ 

 $message = "<h1>That user does not exist!</h1>" . 

 "Please <a href='index.php'>try again</a>"; 

 } 

 mysqli_free_result($result); 

 require_once("db_close.php"); 

 }else{ 

 $message = "<h1>Please enter a valid username/password</h1>"; 

 }  

?> 

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
 <?php echo $message; //Place within HTML body ?>

<h1>Login</h1> 

<form action='login.php' method='POST'> 

 Username: <input type='text' name='username'><br /> 

Password: <input type='password' name='password'><br /> 

<input type='submit' name='submit' value='Login'> 

 <input name='reset' type='reset' value='Reset'> 

</form> <h4><a href='register.php'>Register</a></h4> 


</body>
</html>

当我单击登录按钮时,这是我收到的错误消息:

Warning: Cannot modify header information - headers already sent by 
(output started at /home/cs12k2s/public_html/Dynamic Website/functions.php:28) 
in /home/cs12k2s/public_html/Dynamic Website/login.php on line 31

我仍然不完全理解 header 函数是如何工作的,所以任何帮助都会很棒。

最佳答案

你已经得到了输出

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

header() 调用之前

header('Location: home.php');

header() 调用之前不得有输出。

manual page 的一部分:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

关于php - "Cannot modify header information - headers already sent by"验证、提交然后重定向时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21156517/

相关文章:

javascript - 获取谷歌索引的动态文本

mysql - 查找每个月的最小值和最大值 'Rainfall' 以及相应的月份和日期

MySQL 5.7.10 简单查询比 5.6.35 慢

php - PDO:查找值并返回相似值的数组

php - 按列值对 mySQL 结果进行排序

javascript - 无法包含 xajax Javascript 组件

html - 表体列与标题列的宽度不同

php - 无法将 facebook 包含在我的 codeigniter 项目中

javascript - jquery内容 slider ,如何在没有冗余代码的情况下进行复制

PHP mcrypt 和 SQL 加密信息在哪里?