报错:Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at D:\AppServ\www\basic1\login.php:2) in D:\AppServ\www\basic1\login.php on line 7
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at D:\AppServ\www\basic1\login.php:2) in D:\AppServ\www\basic1\login.php on line 7
Warning: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\basic1\login.php:2) in D:\AppServ\www\basic1\login.php on line 12
下面是我写的三段程序:
login.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户登录</title>
</head>
<body>
欢迎登录网站后台。<br/>
<form action="login.php" method="post" name="form">
用户名:<input name="name" type="text" width="20"/><br/>
密码:<input name="pass" type="password" width="20"/><br/>
<input type="submit" name="ok" value="登录">
</form>
</body>
</html>
login.php:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
$user="admin";
$pass="password";
if(trim($_POST['name'])==$user&&trim($_POST['pass'])==$pass){
session_start();
session_register('admin');
session_register('user');
$_SESSION['admin']=1;
$_SESSION['user']=$user;
header("Location:manage.php");
}
else
{
echo "错误的用户名和密码";
}
?>
manage.php:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>网站后台
管理</title>
</head>
<body>
<?php
session_start();
if($_SESSION['admin']==1)
{
echo"欢迎您,管理员".$_SESSION['user']."<br/>";
?>
1、会员管理<br>
2、新闻管理<br>
3、公告管理<br>
4、广告管理<br>
<?php
}
else
{
echo "sorry,您未被授权访问此页面";
}
?>
</body>
</html>
使用session和cookie的时候,在它们之前不能对浏览器发送任何数据,否则会有警告和错误产生