---------------cookie--------------------------------
<?php
setcookie("name","tom",time()+100);
<?php
echo $_COOKIE['name'];
----login.php用户登录页面(cookie保存)---------------
<?php
mysql_connect("localhost","root","root");
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)){
setcookie("username",$username,time()+24*3600);
setcookie("id",$row['id'],time()+24*3600);
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/>
密 码:<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
setcookie("username","",time()-100);
setcookie("id","",time()-100);
header("location:index.php");
---------------------------设置session(demo1.php)-------
<?php
session_start();
$_SESSION['username']='tom';
---------------------------读取session(demo2.php)------
<?php
session_start();
echo $_SESSION['username'];
---------删除session(demo3.php)-----------------
<?php
session_start();
unset($_SESSION["username"]);
$_SESSION=array();
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>
<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>
<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>
<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 © 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);
}
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;
$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['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['username']=$username;
$_SESSION['id']=mysql_insert_id();
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" />
<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['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"/> 下次自动登录</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
include "./common/config.inc.php";
$_SESSION=array();
session_destroy();
header("location:index.php");