PHP学习记录之会话处理

1.cookie

<?php
//创建一个cookie
setcookie('username','jimtang');

//判断是否存在
if(isset($_COOKIE['username']))
{
 echo $_COOKIE['username'];
}
else
{
 echo 'cookie不存在';
}
?>
//cookie的删除
//方法1.
<?php
setcookie('username','');//赋空值
?>
//方法2.
<?php
setcookie('username','jimtang',time()-1);//使用过期时间删除
?>

cookie的验证传递小案例

1.demo6.html

<!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>
<form method="post" action="demo7.php">
用户名:<input type="text" name="username" /><br />
<input type="submit" value="Login" />
</form>
</body>
</html>

2.demo7.php

<?php

if(isset($_POST['username'])||$_POST['username']=='jimtangyh')
{
	setcookie('name','唐寅浩');
	//echo "<script>alert('我进来了');</script>";
	header('Location:demo8.php');
	}
else
{
	header('Location:demo6.html');
	}
?>

3.demo8.php

<?php
if(isset($_COOKIE['name']))
{
	echo '欢迎光临'.$_COOKIE['name'];
	}
else
{
	echo '非法登录';
	}
?>

  

  

  

  

posted @ 2016-07-30 00:18  jimtang  阅读(116)  评论(0)    收藏  举报