<?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>