首先,我对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());
}
?>
无论如何,我的问题在于登录验证代码,更具体地说是标头函数。这是带有登录验证和登录表单的代码。
<!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
我仍然不完全了解标题函数的工作原理,因此任何帮助都会很棒。
你有输出
<!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()
调用之前不得有输出。
手册页的一部分:
请记住,在任何实际输出之前必须调用 header() 通过普通 HTML 标记、文件中的空行或从 PHP 发送。