php利用cookie實現(xiàn)自動登錄的方法
更新時間:2014年12月10日 09:50:51 投稿:shichen2014
這篇文章主要介紹了php利用cookie實現(xiàn)自動登錄的方法,涉及針對cookie的讀取判斷及登陸操作,是非常實用的技巧,需要的朋友可以參考下
本文實例講述了php利用cookie實現(xiàn)自動登錄的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
html前端頁面代碼如下:
復(fù)制代碼 代碼如下:
<html>
<head>
<title>enter password</title>
</head>
<body>
<form name="forml" method="post" action="cookiebasedpasswordlogin.php">
<table>
<tr>
<td colspan="2" >
<div align="center"><b>please specify the password</b></div>
</td>
</tr>
<tr>>
<td>
<div align="right">customer id</div>
</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td>
<div align="right">password</div>
</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td colspan="2">
<center>
<input type="submit" name="submit" value="login">
</center>
</td>
</tr>
</table>
</form>
</body>
</html>
<head>
<title>enter password</title>
</head>
<body>
<form name="forml" method="post" action="cookiebasedpasswordlogin.php">
<table>
<tr>
<td colspan="2" >
<div align="center"><b>please specify the password</b></div>
</td>
</tr>
<tr>>
<td>
<div align="right">customer id</div>
</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td>
<div align="right">password</div>
</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td colspan="2">
<center>
<input type="submit" name="submit" value="login">
</center>
</td>
</tr>
</table>
</form>
</body>
</html>
后端PHP代碼如下:
復(fù)制代碼 代碼如下:
<?php
$now = getdate();
$storetime= $now["weekday"] . " " . $now["month"] ." " . $now["year"] ;
$storetime.=" time : ";
if ($now["hours"] < 10) {
$storetime.= "0" . $now["hours"];
} else {
$storetime.= $now["hours"];
}
$storetime.= ":";
if ($now["minutes"]<10) {
$storetime.= "0" . $now["minutes"];
} else {
$storetime.= $now["minutes"];
}
$storetime.= ": ";
if ($now["seconds"] <10) {
$storetime.= "0" . $now["seconds"];
} else {
$storetime.= $now["seconds"];
}
if (isset($data)) {
$counter=++$data[l];
setcookie("data[0]",$storetime,time() + (60*60*24));
setcookie("data[l]", $counter,time() + (60*60*24)); setcookie("data[2]",$username,time() + (60*60*24));
echo "<b><center>hi " . $data[2] . " ! !</center></b><br>n";
echo "<b><center>last login time :" .$data[0] . "</center></b><br>n";
echo "<b><center>current date :" .$storetime. "</center></b><br>n";
echo "<b><center>page view count :" . $data[l]. "</center></b><br>n";
echo "<b><center>you have successfully logged in!</center></b>";
echo ("<b><contor>you can access this area without entering a password for the next 24 hours!</center></b>");
} else {
if (isset($username) && isset($password)) {
if ($password=="superpass") {
$counter=0;
setcookie("data[0]",$storetime,time() + (60*60*24));
setcookie("data[l]",$counter,time() + (60*60*24));
setcookie("data[2]",$username,time() + (60*60*24));
$url="location: cookieimp.php";
header($url);
}else{
echo "<hl><center>invalid password!!!</center></hl>";
}
}
}
?>
$now = getdate();
$storetime= $now["weekday"] . " " . $now["month"] ." " . $now["year"] ;
$storetime.=" time : ";
if ($now["hours"] < 10) {
$storetime.= "0" . $now["hours"];
} else {
$storetime.= $now["hours"];
}
$storetime.= ":";
if ($now["minutes"]<10) {
$storetime.= "0" . $now["minutes"];
} else {
$storetime.= $now["minutes"];
}
$storetime.= ": ";
if ($now["seconds"] <10) {
$storetime.= "0" . $now["seconds"];
} else {
$storetime.= $now["seconds"];
}
if (isset($data)) {
$counter=++$data[l];
setcookie("data[0]",$storetime,time() + (60*60*24));
setcookie("data[l]", $counter,time() + (60*60*24)); setcookie("data[2]",$username,time() + (60*60*24));
echo "<b><center>hi " . $data[2] . " ! !</center></b><br>n";
echo "<b><center>last login time :" .$data[0] . "</center></b><br>n";
echo "<b><center>current date :" .$storetime. "</center></b><br>n";
echo "<b><center>page view count :" . $data[l]. "</center></b><br>n";
echo "<b><center>you have successfully logged in!</center></b>";
echo ("<b><contor>you can access this area without entering a password for the next 24 hours!</center></b>");
} else {
if (isset($username) && isset($password)) {
if ($password=="superpass") {
$counter=0;
setcookie("data[0]",$storetime,time() + (60*60*24));
setcookie("data[l]",$counter,time() + (60*60*24));
setcookie("data[2]",$username,time() + (60*60*24));
$url="location: cookieimp.php";
header($url);
}else{
echo "<hl><center>invalid password!!!</center></hl>";
}
}
}
?>
希望本文所述對大家的PHP程序設(shè)計有所幫助。
您可能感興趣的文章:
- 利用php實現(xiàn)一周之內(nèi)自動登錄存儲機制(cookie、session、localStorage)
- php中實現(xiàn)記住密碼下次自動登錄的例子
- php中實現(xiàn)記住密碼自動登錄的代碼
- phpMyAdmin自動登錄和取消自動登錄的配置方法
- php版微信自動登錄并獲取昵稱的方法
- PHP登錄驗證功能示例【用戶名、密碼、驗證碼、數(shù)據(jù)庫、已登陸驗證、自動登錄和注銷登錄等】
- PHP基于cookie與session統(tǒng)計網(wǎng)站訪問量并輸出顯示的方法
- php頁面跳轉(zhuǎn)session cookie丟失導(dǎo)致不能登錄等問題的解決方法
- PHP中cookie和session的區(qū)別實例分析
- PHP根據(jù)session與cookie用戶登錄狀態(tài)操作類的代碼
- php同時使用session和cookie來保存用戶登錄信息的實現(xiàn)代碼
- PHP cookie,session的使用與用戶自動登錄功能實現(xiàn)方法分析
相關(guān)文章
淺析HTTP消息頭網(wǎng)頁緩存控制以及header常用指令介紹
本篇文章是對HTTP消息頭網(wǎng)頁緩存控制以及header常用指令進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP 抽象方法與抽象類abstract關(guān)鍵字介紹及應(yīng)用
抽象方法指沒有方法體的方法,只要一個類里面有一個方法是抽象方法,那么這個類就要定義為抽象類,不了解的朋友可以看看2014-10-10PHP 雜談《重構(gòu)-改善既有代碼的設(shè)計》之三 重新組織數(shù)據(jù)
承接上文的PHP 雜談《重構(gòu)-改善既有代碼的設(shè)計》之 重新組織你的函數(shù)繼續(xù)重構(gòu)方面的內(nèi)容2012-04-04