首页 > 其他 > 详细

2019/5/26

时间:2019-05-26 19:23:22      阅读:123      评论:0      收藏:0      [点我收藏+]

通过一个月的工作,微信智能机器人项目已经阶段完成,解决了最后一个难题便是关于cookies的设置。

从网上搜索的结果是:

cookies:可以从服务器端进行设计,也可以在前端进行设置

session:必须在服务器端进行设置

因为这个项目暂时只需要判断登录状态,所以,我选择了cookies。

js设置cookies代码:

1 var exp = new Date();
2 exp.setTime(exp.getTime() + 2000000*1000);//+号后面单位是毫秒
3 
4 document.cookie = ‘admin_name‘ + "="+ result + ";expires=" + exp.toGMTString();
5 document.cookie = ‘group_name‘ + "="+ group_name + ";expires=" + exp.toGMTString();

 

  实现效果:

  技术分享图片


 

js清除cookies代码:

1 var exp = new Date();
2 group_name = getCookie(‘group_name‘);
3 exp.setTime(exp.getTime() - 1);
4 document.cookie = ‘admin_name‘ + "="+ result + ";expires=" + exp.toGMTString();
5 document.cookie = ‘group_name‘ + "="+ group_name + ";expires=" + exp.toGMTString();

  实现效果:

  技术分享图片


 

js获取cookies代码:

1 function getCookie(key) {
2         var arr,reg = RegExp(‘(^| )‘+key+‘=([^;]+)(;|$)‘);
3         if (arr = document.cookie.match(reg))
4             return decodeURIComponent(arr[2]);
5         else
6             return null;
7     }

返回的是字符串。可以在js中直接使用。

2019/5/26

原文:https://www.cnblogs.com/miquel/p/10926818.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!