我正在学习php和MySQL来做一个课程项目,但我在这里遇到了一些问题。
这是我在index.php中的代码(引自https://htmlcssphptutorial.wordpress.com/2015/07/07/simple-user-registration-login-script-in-php-and-mysql/):
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
require('db.php');
// If form submitted, insert values into the database.
if (isset($_POST['username'])){
$username = $_POST['username'];
$password = $_POST['password'];
$username = stripslashes($username);
$username = mysql_real_escape_string($username);
$password = stripslashes($password);
$password = mysql_real_escape_string($password);
//Checking is user existing in the database or not
$query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'";
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result);
if($rows==1){
$_SESSION['username'] = $username;
header("Location: index.php"); // Redirect user to index.php
}else{
echo "<div class='form'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
<div class="form">
<h1>Log In</h1>
<form action="" method="post" name="login">
<input type="text" name="username" placeholder="Username" required />
<input type="password" name="password" placeholder="Password" required />
<input name="submit" type="submit" value="Login" />
</form>
<p>Not registered yet? <a href='registration.php'>Register Here</a></p>
</div>
<?php } ?>
</body>
</html>
它给了我错误信息:
警告:无法修改标头信息 - 已发送的标头 (输出从/Library/WebServer/Documents/login.php:11开始) /Library/WebServer/Documents/login.php第27行
我用谷歌搜索它,它可能是两个phps之间的html。但是,如果我把html放在第一位,而不是大量的PHP代码,它会给我错误
警告:session_start():无法发送会话缓存限制器 - 标头 已发送(输出从/website/login.php:15开始) 第17行/website/login.php
知道如何解决这个问题吗?提前谢谢!