COOKIE实现用户自动登录

Posted 2016

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了COOKIE实现用户自动登录相关的知识,希望对你有一定的参考价值。

<?php
    header(‘content-type:text/html;charset=utf-8‘);
    if(!empty($_POST)) {
        $name = $_POST[‘name‘];
        $psw  = $_POST[‘psw‘];
        if($name==‘jack‘&&$psw==‘rose‘) {
            echo 1;
            exit;
        } else {
            echo 0;
            exit;
        }
    }
 ?>

 <html>
 <head>
     <title>testCookie</title>
 </head>
 <body>
    <form action="/testtest.php">
        name:<input class="name" name="name" type="text" /><br>
        psw :<input class="psw" name="psw" type="password" /><br>
        sub:<button class="btn" onclick="return sub()">登录</button>
    </form>
    
 </body>
 <script src="https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/jquery/jquery-1.10.2.min_f2fb5194.js" type="text/javascript"></script>
 <script type="text/javascript">
    window.onload = function(){
        var is_save_login = getCookie(‘is_save_login‘);
        if(is_save_login==‘yes‘) {
            var name = getCookie(‘name_login‘);
            var psw  = getCookie(‘psw_login‘);
            if(name && psw) {
                $(‘.name‘).val(name);
                $(‘.psw‘).val(psw);
                $(‘.btn‘).html(‘自动登录‘);
                $.post(‘/testCookie.php‘, {‘name‘:name,‘psw‘:psw}, function(res){
                    if(res==‘1‘) {
                        addCookie(‘is_save_login‘, ‘yes‘, 7*24);
                        addCookie(‘name_login‘, name, 7*24);
                        addCookie(‘psw_login‘, psw, 7*24);
                        alert(‘自动登陆成功!‘);
                        window.location.href = ‘http://www.mljun.com‘;
                    } else {
                        alert(‘自动登陆失败!‘);
                        $(‘.psw‘).val(‘‘);
                    }
                })
            } else {
                deleteCookie(‘is_save_login‘);
                deleteCookie(‘name_login‘);
                deleteCookie(‘psw_login‘);
            }
        }

        //删除cookie
        function deleteCookie(name){
           var date=new Date();
           date.setTime(date.getTime()-10000);
           document.cookie=name+"=v; expire="+date.toGMTString();
        }

        //获取cookie
        function getCookie(name){
              var strCookie=document.cookie;
              var arrCookie=strCookie.split("; ");
              for(var i=0;i<arrCookie.length;i++){
                    var arr=arrCookie[i].split("=");
                    if(arr[0]==name)return arr[1];
              }
              return "";
        }
    }

    //提交
    function sub() {
        var name = $(‘.name‘).val();
        var psw  = $(‘.psw‘).val();

        $.post(‘/testCookie.php‘, {‘name‘:name,‘psw‘:psw}, function(res){
            if(res===‘1‘) {
                addCookie(‘is_save_login‘, ‘yes‘, 7*24);
                addCookie(‘name_login‘, name, 7*24);
                addCookie(‘psw_login‘, psw, 7*24);
                alert(‘登陆成功!‘);
                window.location.href = ‘http://www.mljun.com‘;
            } else {
                alert(‘登陆失败!‘);
                $(‘.psw‘).val(‘‘);
            }
        })

        return false;
    }

    //设置cookie
    function addCookie(name,value,expireHours){
        var cookieString=name+"="+escape(value);
        //判断是否设置过期时间
        if(expireHours>0){
            var date=new Date();
            date.setTime(date.getTime+expireHours*3600*1000);
            cookieString=cookieString+"; expire="+date.toGMTString();
        }
        document.cookie=cookieString;
    }

 </script>
 </html>

以上是关于COOKIE实现用户自动登录的主要内容,如果未能解决你的问题,请参考以下文章

cookie自动登录的实现

Cookie实现免用户名和密码自动登录

cookie技术实现自动登录+cookie中写入中文+记住用户名

COOKIE实现用户自动登录

通过 cookie 信息实现自动登录

thinkphp如何实现记住用户名,密码,自动登录