zoukankan      html  css  js  c++  java
  • “js操作cookie” 与 “php操作cookie”,两种实现方法

    一、js操作cookie:
    function SetCookie(name,value,days)//两个参数,一个是cookie的名子,一个是值
    {
        var Days = 30;
        if(typeof(days)=="undefined"||isNaN(days))
            Days=parseInt(days.toString());
         //此 cookie 将被保存 30 天 -1为浏览器关闭  
        if(Days!=-1){
            var exp = new Date();    //new Date("December 31, 9998");
            exp.setTime(exp.getTime() + Days*24*60*60*1000);
            document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
        }else{
            document.cookie = name + "="+ escape (value) + ";expires=-1";
        }
    }

      

    function getCookie(name)//取cookies函数
    {
        var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
        if(arr != nullreturn unescape(arr[2]); return null;}
    function delCookie(name)//删除cookie
    {   
        var exp = new Date();
        exp.setTime(exp.getTime() - (86400 * 1000 * 1));
        var cval=getCookie(name);
        if(cval!=null)
            document.cookie = name + "="+ escape (cval) + ";expires="+exp.toGMTString();}

    调用实例: 登陆时“记住我”设置cookie

    //头部登陆框的验证函数
    function checkform(objF){    
    var cookieName_username="LOGIN_USER_NAME";
            var cookieName_password="LOGIN_PASSWORD";
                delCookie(cookieName_username);//删除用户名Cookie
                delCookie(cookieName_password);//删除密码       
                if(objF.autologin.checked)        
                   { 
                      SetCookie(cookieName_username,objF.uname.value.trim(),7);//保存到Cookie中  保存7天      
                      SetCookie(cookieName_password,objF.password.value.trim(),7);   
                    }

    调用实例: 获取cookie自动填充登陆框

      function getUserInfoByCookie(){
              var uname=getCookie(cookieName_username);           
                var upass=getCookie(cookieName_password);        
          
              if(uname!=null&&!uname.toString().isnullorempty())
                   if(upass!=null&&!upass.toString().isnullorempty()){
                       document.getElementById("uname").value=uname;
                       document.getElementById("password").value=upass; }}       

    二、php操作cookie

    实例:登陆时设置cookie

    if($autologin=='1')
            {
                setcookie("account", $accounttime()+1000,"/path/");
                setcookie("password", $passwordtime()+1000,"/path/");}

    实例:退出时删除cookie

    setcookie("account", "", time()-3600,"/path/");
    setcookie("password", "", time()-3600,"/path/");

    实例:index.php获取cookie

     if (isset($_COOKIE["account"])&&isset($_COOKIE["password"])) {

            $account=$_COOKIE["account"];
            $account=$_COOKIE["password"];
    }

         

  • 相关阅读:
    laravel执行migrate出现1071 字符串类型过长 异常
    Laravel关闭CSRF功能的两种方法
    webstome10破解及汉化
    spring-retry简单demo(附完整代码)
    git提交报错SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
    tomcat8的session共享实现方案
    idea下载安装指南
    《Maven实战》(许晓斌)导读(读书笔记&第二次读后感)
    spring cloud config搭建说明例子(四)-补充配置文件
    spring cloud config搭建说明例子(三)-添加actuator
  • 原文地址:https://www.cnblogs.com/luowei/p/2712851.html
Copyright © 2011-2022 走看看