index.php
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include ("conn.php");
if(isset($_COOKIE['username'])){
$sql="select * from 'user' where username ='".$_COOKIE['username']."'";
$rs=mysql_fetch_array(mysql_query($sql));
$code=md5($rs['username'].$rs['pwd']);
if (!empty($rs['username'])&&$_COOKIE['code']==$code) {
echo $rs['username']."欢迎来到登陆首页!<a href='logout.php'>退出登陆</a>";
}
else{
echo "非法操作,<a href='login.php'>请登录</a>";
}
}
else {
echo"尚未登陆,<a href='login.php'>请登录</a>";
}
?>
</head>
<body>
</body>
</html>
2.reg.php
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<?php
include("conn.php");
if(isset($_post['reg'])){
if(!empty($_post['user'])&&!empty($_post['password'])){
$sql="select * from 'user' where 'username'='".$_POST['user']."'";
$query=mysql_query($sql);
$rr=mysql_fetch_array($query);
if($rr[username]!=$_POST['user']){
$sql="INSERT INTO 'user'('id','username','pwd','email')VALUES(NULL,'".$_POST['user']."','".md5($_POST['password'])."','".$_POST['email']."'));";
if($state){
setcookie("username",$_POST['user']);
setcookie("code",md5($_POST['user'].md5($_POST['password'])));
echo"<script>alert('注册成功!');window.location.href='index.php'</script>";
}
}
else{
echo"用户名已存在";
}
}
else{
echo"用户名和密码不能为空";
}
}
?>
</head>
<body>
<a href='index.php'>返回首页</a> <a href='login.php'>登陆</a>
<form>
用户名:<input type="text" name="user"/><br/>
密码: <input type="password" name="password"/><br/>
E-MAIL:<input type="text" name="email"/><br/>
<input type="submit" name="reg" value="提交" />
</form>
</body>
</html>
3.login.php
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<?php
include("conn.php");
if(isset($_POST['submit'])){
if(empty($_POST["username"])){
echo"<script>alert('用户名不能为空');</script>";
}else if(empty($_POST["password"])){
echo"<script>alert('密码不你能为空');</script>";
}else{
$sql="select * from 'user' where 'username'='".$_POST['username']."'limit 1";
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
if(!empty($rs['username'])){
if(($rs['pwd'])==md5($_POST['password'])){
setcookie("username",$rs['username']);
setcookie("code",md5($rs['username'].md5($rs['pwd'])));
echo"<script>alert('登录成功');window.location.href='index.php'</script>";
}else{
echo"<script> alert('密码错误');</script>";
}
}else{
echo"<script>alert('用户名不存在');</script>";
}
}
}
?>
</head>
<body>
<a href='index.php'>返回首页</a> <a href='reg.php'>注册</a>
<form action=" " method="post">
用户名:<input type="text" name="username"/><br/>
密 码:
<input type="password" name="password"/><br/>
<input type="submit" name="submit" value="登录"/>
</form>
</body>
</html>
4.loginout.php
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
setcookie('username',"",time()-1);
setcookie('code',"",time()-1);
echo"<script>alert('退出成功');window.localtion.href='index.php'</script>";
?>
5.conn.php
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<?php
mysql_connect("localhost","root","root")or die(mysql_error());
mysql_select_db("user")or die(mysql_error());
mysql_query("set names 'utf8'");
// echo"数据库连接成功";
?>
</body>
</html>
练习一下cookie的使用。。