cookie简介

上例子

1.首先要用php创建cookie发送给客户端,利用setcookie()方法即可

 1 <?php
 2 /*
 3  * 
 4  * @Authors peng--jun 
 5  * @Email   1098325951@qq.com
 6  * @Date    2015-11-04 16:28:37
 7  * @Link    http://www.cnblogs.com/xs-yqz/
 8  * @version $Id$
 9  ==========================================
10  */
11  header("Content-type: text/html; charset=UTF-8"); 
12  setcookie("tmpcookie",'这是个临时的cookie');//不设置过期时间
13  setcookie("username",'小泥巴',time()+60);//设置过期时间为60秒,永久cookie
14  setcookie("age",21,time()+60);
15  setcookie('sex',"",time()+60,'','',false);//设置setcookie的所有参数
16 ?>

2.获取cookie变量

1 <?php
2  header("Content-type: text/html; charset=UTF-8"); 
3  $user = $_COOKIE['username'];
4  $age = $_COOKIE['age'];
5  $sex = $_COOKIE['sex'];
6  echo $user." ".$age."岁,性别".$sex;//输出 小泥巴 21岁,性别女
7 ?>

 

posted @ 2015-11-05 20:12  小军的代码库  阅读(150)  评论(0编辑  收藏  举报