Cookies enable users to log in automatically

Source: Internet
Author: User

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

<title>testCookie</title>
<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 ()" > Login </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 (' automatic login ');
$.post ('/testcookie.php ', {' name ': Name, ' PSW ':p SW}, Function (res) {
if (res== ' 1 ') {
Addcookie (' Is_save_login ', ' yes ', 7x24);
Addcookie (' Name_login ', name, 7x24);
Addcookie (' Psw_login ', PSW, 7x24);
Alert (' Automatic login successful! ‘);
window.location.href = ' http://www.mljun.com ';
} else {
Alert (' Automatic login failed! ‘);
$ ('. PSW '). Val (');
}
})
} else {
Deletecookie (' Is_save_login ');
Deletecookie (' Name_login ');
Deletecookie (' Psw_login ');
}
}

Delete Cookies
function Deletecookie (name) {
var date=new date ();
Date.settime (Date.gettime ()-10000);
Document.cookie=name+ "=V; Expire= "+date.togmtstring ();
}

Get cookies
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 "";
}
}

Submit
function sub () {
var name = $ ('. Name '). Val ();
var PSW = $ ('. PSW '). Val ();

$.post ('/testcookie.php ', {' name ': Name, ' PSW ':p SW}, Function (res) {
if (res=== ' 1 ') {
Addcookie (' Is_save_login ', ' yes ', 7x24);
Addcookie (' Name_login ', name, 7x24);
Addcookie (' Psw_login ', PSW, 7x24);
Alert (' Login successful! ‘);
window.location.href = ' http://www.mljun.com ';
} else {
Alert (' Login failed! ‘);
$ ('. PSW '). Val (');
}
})

return false;
}

Set cookies
function Addcookie (name,value,expirehours) {
var cookiestring=name+ "=" +escape (value);
Determine if the expiration time is set
if (expirehours>0) {
var date=new date ();
Date.settime (date.gettime+expirehours*3600*1000);
Cookiestring=cookiestring+ "; Expire= "+date.togmtstring ();
}
document.cookie=cookiestring;
}

</script>

Cookies enable users to log in automatically

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.