cookie+session+登录+注册+验证码

时间:2022-08-31 11:38:51
---------------cookie--------------------------------
<?php
/*
* 1、在test1.php中保存cookie 名称为 name,值为tom
在test2.php中读取保存的cookie内容
2、要先执行test1.php这样保存后。test2.php才能获取到
*/

setcookie("name","tom",time()+100);
<?php
//读取cookie
echo $_COOKIE['name'];
----login.php用户登录页面(cookie保存)---------------
<?php
//1、连接数据库
mysql_connect("localhost","root","root");
//2、选择默认数据库
mysql_select_db("cms");
//判断用户是否登录
if(isset($_COOKIE['username'])&&$_COOKIE['username']!=""){
//说明用户已经登录
header("location:index.php");
}
//当用户提交信息后,对用户输入的信息进行判断
if($_POST){
//收录用户提交的信息
$username = $_POST['username'];
if($username==""){
echo "用户名不能为空";
exit;
}

$password = $_POST['password'];
if($password==""){
echo "密码不能为空";
exit;
}
//去数据库比对
$query = "select id from cms_admin where
aname='"
.$username."' and
pwd='"
.$password."'";
$result = mysql_query($query);
if($row = mysql_fetch_assoc($result)){
//登录成功,将用户信息保存到cookie中
setcookie("username",$username,time()+24*3600);
setcookie("id",$row['id'],time()+24*3600);
//登录成功,跳转到index.php
header("location:index.php");
}else{
echo "登录失败";
}
}
?>

<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=utf-8" />
<title>登录系统</title>
</head>

<body>
<center>用户登录</center>
<br/><br/>
<form action="" method="post">
用户名:<input type="text" name="username" /> <br/><br/>
密&nbsp;&nbsp;码:<input type="password" name="password" /><br/><br/>
<input type="submit" value="登录" />
</form>
</body>
</html>
---------------------------index.php(判断用户是否登录)---------
<?php
header("Content-Type:text/html;charset=utf-8");
//判断用户是否登录
//若用户登录,则显示欢迎信息,给出退出链接
//若用户没登录,则给出登录链接
if(isset($_COOKIE["username"])&&$_COOKIE["username"]!=""){
//此时用户就登录了
echo "欢迎".$_COOKIE["username"];
echo "<a href='logout.php'>退出</a>";
}else{
//若当前用户没有登录
echo "<a href='login.php'>登录</a>";
}
-------------------------logout(退出,删除保存的cookie)---
<?php
//退出
//删除保存的cookie
setcookie("username","",time()-100);
setcookie("id","",time()-100);
header("location:index.php");
---------------------------设置session(demo1.php)-------
<?php
//session_start()前面不能有内容
//1、设置session
session_start();//分配给用户一个id
//在浏览器查看的时候,就不会再看见值了,只看到sessionid
$_SESSION['username']='tom';
---------------------------读取session(demo2.php)------
<?php
//前面不能有任何的输出
//建立好连接
session_start();
echo $_SESSION['username'];
---------删除session(demo3.php)-----------------
<?php
//删除session
session_start();
//删除一个session值
unset($_SESSION["username"]);
//将整个session文件清空
$_SESSION=array();
//删除掉session文件
session_destroy();
-----config.inc.php(需要开启session的公共类)-----------
<?php
session_start();
header("Content-Type:text/html;charset=utf-8");
-------头部样式的提取(head.php)----------------------
<!--通用顶部导航开始-->
<div id="top-navi-wrap">
<div class="clearfix" id="top-navi">
<div class="side-left">
<a href="#">巴西世界杯一再爆冷 卫冕冠军西班牙出局</a>
<a href="#">瑞士人为啥最幸福</a> 
<a href="#">中国病人庞麦郎</a>
</div>
<div class="side-right">
<?php
//判断用户是否登录
if(isset($_SESSION['username'])&&$_SESSION['username']!=""){

?>

<span>您好,<a href="#" class="current-user">
<?php echo $_SESSION["username"]; ?>
</a></span>
<a href="logout.php">退出</a>
<?php }else{ ?>
<a href="login.php" target="_blank" class="top-nav-login-title">登录</a>
<div class="top-nav-select-title">
<a href="register.php" target="_blank">免费注册</a>
<?php } ?>
</div>
</div>
</div>
</div>
<!-- 通用顶部导航结束 -->
<!--header start-->
<div class="clear" id="header">
<div id="logo"><a href="#" title="换一个角度看新闻"></a></div>
<div id="search-bar">
<form name="search-form" action="" method="get">
<input type="text" name="keywords" id="keywords" value="党报连发治军铁腕新政"/>
<input type="submit" value="" id="search-submit-button"/>
</form>
</div>
</div>
<!--header end-->
<!--导航开始-->
<div id="navigation">
<ul class="clear">
<li><a href="index.php" target="_blank">首页</a></li>
<li><a href="roll.html" target="_blank">滚动</a></li>
<li><a href="china.html" target="_blank">国内</a></li>
<li><a href="world.html" target="_blank">国际</a></li>
<li><a href="society.html" target="_blank">社会</a></li>
<li><a href="photo.html" target="_blank">图片</a></li>
<li><a href="ent.html" target="_blank">娱乐</a></li>
<li><a href="military.html" target="_blank">军事</a></li>
<li><a href="bbs.html" target="_blank">评论</a></li>
<li><a href="history.html" target="_blank">历史</a></li>
<li><a href="encyclopedia.html" target="_blank">百科</a></li>
<li><a href="commonweal.html" target="_blank">公益</a></li>
</ul>
</div>
<!--导航结束-->
-------------------------底部样式的提取(head.php)-----
<div class="section">
<h4 class="friendlink-hr"><span class="friendlink">友情链接</span></h4>
<div class="friendlink-cont">
<p><a href="http://www.gov.cn/" target="_blank">中国*网</a> | <a href="http://www.china.com.cn" target="_blank">中国网</a> | <a href="http://www.people.com.cn/" target="_blank">人民网</a> | <a href="http://www.xinhuanet.com/" target="_blank">新华网</a> | <a href="http://www.chinanews.com/" target="_blank">中新网</a> | <a href="http://www.cctv.com/default.shtml" target="_blank">央视网</a> | <a href="http://www.cnr.cn/" target="_blank">中广网</a> | <a href="http://www.chinadaily.com.cn/" target="_blank">中国日报</a> | <a href="http://www.cyol.net/node/index.htm" target="_blank">中青在线</a> | <a href="http://www.gmw.cn/" target="_blank">光明网</a> | <a href="http://www.chinamil.com.cn/" target="_blank">解放军报</a> | <a href="http://www.legaldaily.com.cn/" target="_blank">法制日报</a> | <a href="http://www.*.cn/" target="_blank">中国*网</a> | <a href="http://www.ce.cn/" target="_blank">中经网</a> | <a href="http://www.nfcmag.com/" target="_blank">南风窗</a></p>
<p><a href="http://www.chinaweekly.cn/" target="_blank">中国周刊</a> | <a href="http://www.lifeweek.com.cn/" target="_blank">三联生活周刊</a> | <a href="http://www.ynet.com/" target="_blank">北青网</a> | <a href="http://www.dayoo.com/" target="_blank">大洋网</a> | <a href="http://www.nanfangdaily.com.cn" target="_blank">南方报业</a> | <a href="http://www.ycwb.com/" target="_blank">金羊网</a> | <a href="http://www.southcn.com/" target="_blank">南方网</a> | <a href="http://www.eastday.com" target="_blank">东方网</a> | <a href="http://www.21dnn.com/" target="_blank">千龙网</a> | <a href="http://www.thebeijingnews.com/" target="_blank">新京报</a> | <a href="http://epaper.jinghua.cn/" target="_blank">京华时报</a>| <a href="http://www.rednet.com.cn/" target="_blank">红网</a> | <a href="http://workercn.cn/" target="_blank">工人日报</a> | <a href="http://www.inewsweek.cn/" target="_blank">中国新闻周刊</a> | <a href="http://www.cankaoxiaoxi.com/" target="_blank">参考消息</a></p>
<p><a href="http://www.neweekly.com.cn" target="_blank">新周刊</a> | <a href="http://www.dahe.cn/" target="_blank">大河网</a> | <a href="http://www.chinapeace.org.cn/" target="_blank">中国长安网</a> | <a href="http://www.huanqiu.com/" target="_blank">环球网</a> | <a href="http://www.newssc.org" target="_blank">四川新闻网</a> | <a href="http://www.morningpost.com.cn/" target="_blank">北晨网</a> | <a href="http://www.zjol.com.cn/" target="_blank">浙江在线</a> | <a href="http://gb.cri.cn/" target="_blank">国际在线</a> | <a href="http://www.e23.cn/" target="_blank">舜网</a> | <a href="http://www.xkb.com.cn./index.php" target="_blank">新快网</a> | <a href="http://www.jxcn.cn/" target="_blank">中国江西网</a> | <a href="http://www.yunnan.cn" target="_blank">云南网</a> | <a href="http://www.szed.com/" target="_blank">深圳新闻网</a> | <a href="http://www.anhuinews.com/" target="_blank">中安在线</a> | <a href="http://www.xwh.cn/" target="_blank">新文化网</a></p>
<p><a href="http://www.ts.cn/" target="_blank">天山网</a> | <a href="http://www.iyaxin.com/" target="_blank">亚心网</a> | <a href="http://www.dzwww.com/" target="_blank">大众网</a> | <a href="http://www.jxnews.com.cn/" target="_blank">大江网</a> | <a href="http://www.hsw.cn/" target="_blank">华商网</a> | <a href="http://www.chinaxiaokang.com/" target="_blank">中国小康网</a> | <a href="http://www.jxcbw.cn/" target="_blank">江西晨报网</a> | <a href="http://xmzk.xinminweekly.com.cn/" target="_blank">新民周刊</a> | <a href="http://www.fjsen.com/" target="_blank">东南网</a> | <a href="http://www.sdnews.com.cn/" target="_blank">山东新闻网</a> | <a href="http://www.*net.com.cn/" target="_blank">*网</a> | <a href="http://www.cqnews.net/" target="_blank">华龙网</a> | <a href="http://news.cnhubei.com/" target="_blank">荆楚网</a> | <a href="http://www.ahwang.cn/" target="_blank">安徽网</a> | <a href="http://news.sina.com.cn/media.html" target="_blank">媒体大全</a></p>
</div>
</div>
<!--友情链接 end-->
<!--版权区 start-->
<div class="section footer">
<p>24小时客户服务热线:4006900000 010-82623378 <a href="#">常见问题解答</a> <a target="_blank" href="http://www.12377.cn/">互联网违法和不良信息举报</a></p>
<p><a target="_blank" href="#">新闻中心意见反馈留言板</a></p>
<p><a href="http://www.tarena.com.cn/">达内简介</a> | <a href="#">关于达内</a> | <a href="#">广告服务</a> | <a href="#">联系我们</a> | <a href="#">招聘信息</a> | <a href="#">网站律师</a> | <a href="#">通行证注册</a> | <a href="#">产品答疑</a></p>
<p>Copyright &copy; 1996-2015 TARENA Corporation, All Rights Reserved</p>
</div>
------------------------验证码文件code.php引入common下---
<?php
session_start();
//验证码
/*
* - 画布(浅色、随机)
- 干扰(随机出现的线,随机出现的点)
- 输出文字
- 展示
- 释放资源
*/

//画布(浅色、随机)
$img = imagecreatetruecolor(80,40);
//创建画布的背景色
$bgcolor = imagecolorallocate($img,
rand(200,255),
rand(200,255),
rand(200,255));
//填充画布
imagefill($img,0,0,$bgcolor);

//- 干扰(随机出现的线,随机出现的点)
for($i=0;$i<50;$i++){
$color = imagecolorallocate($img,
rand(100,200),
rand(100,200),
rand(100,200));
imagesetpixel($img,rand(1,79),
rand(1,29),$color);
}

//随机10条线
for($i=0;$i<10;$i++){
$color = imagecolorallocate($img,
rand(100,200),
rand(100,200),
rand(100,200));
imageline($img,rand(1,79),rand(1,29),
rand(1,79),rand(1,29),$color);

}

//- 输出文字
$codes = "0123456789abcdefghijklmnopqrstuvwxyz";
//规定验证码的长度
$length = 4;
//定义一个将存储在session中的字符串变量
$words="";
//从 库字符串 随机截取 每次截取一个字符
for($i=0;$i<$length;$i++){
$color = imagecolorallocate($img,
rand(0,100),
rand(0,100),
rand(0,100));

$str = substr($codes,rand(0,strlen($codes)-1),1);
$words.=$str;
$x = (80/$length)*$i+5;
$y imagestring($img,5,$x,$y,$str,$color);
}
//将验证码上的文字保存到session中
$_SESSION['code']=$words;
//- 展示
header("Content-Type:image/png");
imagepng($img);
//- 释放资源
imagedestroy($img);
-------------------register.php(有用户名+密码+验证码的注册)----
<?php
include './common/config.inc.php';
//引入公用的数据库配置文件
include "./common/db.inc.php";
//判断用户是否提交数据
if($_POST){
//首先收录验证码,验证法错了,后面就不需要浪费资源验证了
$code = $_POST['verify'];
echo $code;
echo "<hr/>";
echo $_SESSION['vcode'];
if($code==""){
echo "验证码不能为空";
exit;
}
if($code!=$_SESSION['vcode']){
echo "验证码错误";
exit;
}
$username = $_POST['username'];
if($username==""){
echo "用户名不能为空";
exit;
}

$password = $_POST['password'];
if($password==""){
echo "密码不能为空";
exit;
}
//收录协议
$protocol = isset($_POST['protocol'])?
$_POST['protocol']:0;
if($protocol==0){
echo "请选择已阅读并同意协议";
exit;
}
//数据库操作
$query = "insert into cms_user
(username,password)
value
('"
.$username."','".md5($password)."') ";
$result = mysql_query($query);
if($result){
//帮助用户登录,或者给出登录链接让用户
//用户信息保存到session中
//用户名 id
//自己去登录
$_SESSION['username']=$username;
$_SESSION['id']=mysql_insert_id();
//跳转回index.php
header("location:index.php");
}else{
echo "注册失败";
}

}
?>

<!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>
<link href="styles/reset.css" rel="stylesheet" type="text/css" media="all" />
<link href="styles/layout.css" rel="stylesheet" type="text/css" media="all" />
<link href="styles/common.css" rel="stylesheet" type="text/css" media="all" />
</head>

<body>
<div id="header-wrapper">
<div class="wrapper">
<div id="header">
<div class="side-left logo">
<a href="#" title="换一个角度看新闻">新闻视界</a>
</div>
<div class="side-center page-title"><span>注册</span></div>
<div class="side-right login-bar"><a href="login.php" target="_blank">快速登录</a><a href="index.html" target="_blank">返回首页</a></div>
</div>
</div>
</div>
<div id="register-wrapper">
<div id="register-main">
<div class="progress-bar-input" id="progress-bar"></div>
<form id="register-form" method="post">
<ul>
<li class="clear">
<span class="register-title side-left">账号:</span>
<input type="text" name="username" class="register-input-username side-left"/>
<span class="prompt side-left"></span>
</li>
<li class="clear">
<span class="register-title side-left">密码:</span>
<input type="password" name="password" class="register-input-password side-left"/>
<span class="prompt side_left"></span>
</li>
<li class="clear">
<span class="register-title side-left">验证码:</span>
<input type="text" name="verify" class="register-input-verify side-left"/>
<img src="common/vcode.php" />
<!--因为验证码返回的是一张随机的图片,所以img可以直接引用这样做好的验证码图片-->
<span class="prompt side-left"></span>
</li>
<li class="register-protocol">
<input type="checkbox" name="protocol" checked="checked" value="1"/>我已阅读并同意《新闻视界用户服务条款与隐私保护政策》
</li>
<li class="register-button"><input type="submit" value="注册新账号" class="register-submit-button"/></li>
</ul>
</form>
</div>
</div>
</body>
</html>
---------------------------较完整的登录页面(login.php)-------
<?php
session_start();
//引入数据库的链接
include 'common/db.inc.php';
//如果之前已经登录过了,就直接跳转到首页
if(isset($_SESSION['username'])&&$_SESSION['username']!=null){
header("location:index.php");
}
if($_POST){
//收录用户名
$username=$_POST['username'];
//收录密码
$password=$_POST['password'];
if($username==""){
echo '用户名不能为空';
exit;
}
if($password==""){
echo '密码不能为空';
exit;
}
$sql="select id from cms_user where username='{$username}' and password=md5('{$password}')";
$result=mysql_query($sql);
if($row=mysql_fetch_assoc($result)){
//将登录成功的用户信息保存到session中
$_SESSION['username']=$username;
header("location:index.php");
}else{
echo '登录失败';
}
}
?>

<!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>
<link href="styles/reset.css" rel="stylesheet" type="text/css" media="all" />
<link href="styles/layout.css" rel="stylesheet" type="text/css" media="all" />
<link href="styles/common.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<div id="header-wrapper">
<div class="wrapper">
<div id="header">
<div class="side-left logo">
<a href="#" title="换一个角度看新闻">新闻视界</a>
</div>
<div class="side-center page-title"><span>登录</span></div>
<div class="side-right login-bar"><a href="register.html" target="_blank">免费注册</a><a href="index.html" target="_blank">返回首页</a></div>
</div>
</div>
</div>
<div id="login-wrapper">
<div id="login-main">
<form action="" method="post" id="login-form">
<h1 class="login-title">用户登录</h1>
<input type="text" class="login-input-username" name="username" value="请输入登录账号"/>
<input type="password" class="login-input-password" name="password"/>
<div class="login-remmber-me"><span><input type="checkbox" name="remember" checked="checked" value="1"/>&nbsp;下次自动登录</span><a href="#">忘记密码</a></div>
<input type="submit" value="登 录" class="login-submit-button"/>
<div class="other-login">
<p>可以使用以下方式登录:</p>
<ul class="other-login-list clear">
<li><a href="#" class="qq" title="QQ登录">QQ登录</a></li>
<li><a href="#" class="weibo" title="微博账号登录">微博账号登录</a></li>
<li><a href="#" class="baidu" title="百度账号登录">百度账号登录</a></li>
<li><a href="#" class="renren" title="人人网账号登录">人人网账号登录</a></li>
<li><a href="#" class="weixin" title="微信账号登录">微信账号登录</a></li>
</ul>
</div>
</form>
</div>
</div>
</body>
</html>
-------------------------退出登录,清除session(logout.php)--
<?php
//引入公共配置文件 session_start();
include "./common/config.inc.php";
//清空session
$_SESSION=array();
//删除session文件
session_destroy();
//跳转回首页
header("location:index.php");