PHP中cookie、localStorage、session、的用法
2016-07-20 16:34
513 查看
1、cookie用来存储用户相关数据,存储的位置在用户本地:
用php实现:
设置cookie:setcookie("key","value","time");
读取cookie:$_COOKIE("key") //key就是setcookie中的key值
删除cookie:setcookie让时间过期;
用JS(javascript)实现:
设置cookie(js中存放位置document.cookie):
document.cookie 将以字符串的方式返回所有的 cookies,类型格式: cookie1=value; cookie2=value; cookie3=value;
JS中读取出来的cookie是:document.cookie="username=admin; expires='GMT时间'; ..;..";
设置cookie :
function setCookie(ckey,cvalue,exdays)
{
var d = new Date();//new一个时间戳
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString(); //设置cookie保存时间
document.cookie = ckey + "=" + cvalue + "; " + expires;//cookie的类型
格式
}
//runoob写的内容://http://www.runoob.com/
function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++)
{
var c = ca[i].trim();
if (c.indexOf(name)==0)
return c.substring(name.length,c.length);
}
return "";
}
//从str1中截取defg:
var str1="abcdefg";
str1.length=7;
str1.substring(3,7) == "defg";
读取cookie document.cookie 将以字符串的方式返回所有的 cookies,类型格式: cookie1=value;
cookie2=value; cookie3=value;所以就对cookie解析
//我们写的:
funciton getCookieByKey(key){
key = key + "=";
var cookie = document.cookie.split(";");
for(var i=0;i<cookie.length;i++){
var value = cookie[i].trim();//.trim()去空格
if(value.indexOf(key)==0){
var val=value.split("=");
return val[1];
}
}
return "";
}
删除cookie:将cookie的有效期设置为过期的时间就可以。
删除 cookie 非常简单。您只需要设置 expires 参数为以前的时间即可,如下所示,设置为 Thu, 01 Jan 1970 00:00:00 GMT:
2、session:session是用来在服务器端存储用户信息,当浏览器关闭的时候,会自动销毁;
session_start();
超级数组$_SESSION可以实现session的设置与读取;
3、JS端的localStorage:HTML5提供的本地存储方式(可以称为“键值对”数据库);
设置数据:localStorage.setItem("key","value");
读取数据:localStorage.getItem("key");读取的结果是一个字符串;
删除数据:localStorage.removeItem("key");
用php实现:
设置cookie:setcookie("key","value","time");
读取cookie:$_COOKIE("key") //key就是setcookie中的key值
删除cookie:setcookie让时间过期;
用JS(javascript)实现:
设置cookie(js中存放位置document.cookie):
document.cookie 将以字符串的方式返回所有的 cookies,类型格式: cookie1=value; cookie2=value; cookie3=value;
JS中读取出来的cookie是:document.cookie="username=admin; expires='GMT时间'; ..;..";
设置cookie :
function setCookie(ckey,cvalue,exdays)
{
var d = new Date();//new一个时间戳
d.setTime(d.getTime()+(exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString(); //设置cookie保存时间
document.cookie = ckey + "=" + cvalue + "; " + expires;//cookie的类型
格式
}
//runoob写的内容://http://www.runoob.com/
function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++)
{
var c = ca[i].trim();
if (c.indexOf(name)==0)
return c.substring(name.length,c.length);
}
return "";
}
//从str1中截取defg:
var str1="abcdefg";
str1.length=7;
str1.substring(3,7) == "defg";
读取cookie document.cookie 将以字符串的方式返回所有的 cookies,类型格式: cookie1=value;
cookie2=value; cookie3=value;所以就对cookie解析
//我们写的:
funciton getCookieByKey(key){
key = key + "=";
var cookie = document.cookie.split(";");
for(var i=0;i<cookie.length;i++){
var value = cookie[i].trim();//.trim()去空格
if(value.indexOf(key)==0){
var val=value.split("=");
return val[1];
}
}
return "";
}
删除cookie:将cookie的有效期设置为过期的时间就可以。
删除 cookie 非常简单。您只需要设置 expires 参数为以前的时间即可,如下所示,设置为 Thu, 01 Jan 1970 00:00:00 GMT:
2、session:session是用来在服务器端存储用户信息,当浏览器关闭的时候,会自动销毁;
session_start();
超级数组$_SESSION可以实现session的设置与读取;
3、JS端的localStorage:HTML5提供的本地存储方式(可以称为“键值对”数据库);
设置数据:localStorage.setItem("key","value");
读取数据:localStorage.getItem("key");读取的结果是一个字符串;
删除数据:localStorage.removeItem("key");
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- Angularjs 跨域请求
- PHP数据库长连接mysql_pconnect的细节
- kindeditor 批量上传 上传失败 thinkphp swfupload session
- Php Installing An Expansion
- rem : web app适配的秘密武器
- jquery高级应用之Deferred对象
- 关于浮动与清除浮动,你应该知道的
- 数组reduce方法的高级技巧
- php7 读取php.ini[4]
- 杰奇登录后的东西都是在session里面的