PHP登錄驗(yàn)證功能示例【用戶(hù)名、密碼、驗(yàn)證碼、數(shù)據(jù)庫(kù)、已登陸驗(yàn)證、自動(dòng)登錄和注銷(xiāo)登錄等】
本文實(shí)例講述了PHP登錄驗(yàn)證功能。分享給大家供大家參考,具體如下:
登錄界面




具體實(shí)現(xiàn)方法如下:
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="doLogin.php">
<input type="text" placeholder="用戶(hù)名" name="username"><br><br>
<input type="password" placeholder="密碼" name="password"><br><br>
<input type="text" placeholder="驗(yàn)證碼" name="verifycode" class="captcha"><br><br>
<img id="captcha_img" src="captcha.php?r=<?php echo rand();?>" alt="驗(yàn)證碼">
<label><a href="javascript:void(0)" rel="external nofollow" onclick="document.getElementById('captcha_img').src='captcha.php?r='+Math.random()">換一個(gè)</a> </label><br>
<label><input type="checkbox" name="autologin[]" value="1"/>自動(dòng)登錄</label><br>
<button type="submit">登錄</button>
</form>
</body>
</html>
doLogin.php
<?php
header("Content-type:text/html;charset=UTF-8");
require "mysql.php"; //導(dǎo)入mysql.php訪問(wèn)數(shù)據(jù)庫(kù)
session_start(); //開(kāi)啟會(huì)話(huà)一獲取到服務(wù)器端驗(yàn)證碼
$username=$_POST['username'];
$password=$_POST['password'];
$autologin=isset($_POST['autologin'])?1:0; //獲取是否選擇了自動(dòng)登錄
$verifycode=$_POST['verifycode'];
$code=$_SESSION['code']; //獲取服務(wù)器生成的驗(yàn)證碼
/*
* 首先進(jìn)行判空操作,通過(guò)后進(jìn)行驗(yàn)證碼驗(yàn)證,通過(guò)后再進(jìn)行數(shù)據(jù)庫(kù)驗(yàn)證。
* 手機(jī)號(hào)碼和郵箱驗(yàn)證可根據(jù)需要自行添加
* */
if(checkEmpty($username,$password,$verifycode)){
if(checkVerifycode($verifycode,$code)){
if(checkUser($username,$password)){
$_SESSION['username']=$username; //保存此時(shí)登錄成功的用戶(hù)名
if($autologin==1){ //如果用戶(hù)勾選了自動(dòng)登錄就把用戶(hù)名和加了密的密碼放到cookie里面
setcookie("username",$username,time()+3600*24*3); //有效期設(shè)置為3天
setcookie("password",md5($password),time()+3600*24*3);
}
else{
setcookie("username","",time()-1); //如果沒(méi)有選擇自動(dòng)登錄就清空cookie
setcookie("password","",time()-1);
}
header("location: index.php "); //全部驗(yàn)證都通過(guò)之后跳轉(zhuǎn)到首頁(yè)
}
}
}
//方法:判斷是否為空
function checkEmpty($username,$password,$verifycode){
if($username==null||$password==null){
echo '<html><head><Script Language="JavaScript">alert("用戶(hù)名或密碼為空");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\">";
}
else{
if($verifycode==null){
echo '<html><head><Script Language="JavaScript">alert("驗(yàn)證碼為空");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\">";
}
else{
return true;
}
}
}
//方法:檢查驗(yàn)證碼是否正確
function checkVerifycode($verifycode,$code){
if($verifycode==$code){
return true;
}
else{
echo '<html><head><Script Language="JavaScript">alert("驗(yàn)證碼錯(cuò)誤");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\">";
}
}
//方法:查詢(xún)用戶(hù)是否在數(shù)據(jù)庫(kù)中
function checkUser($username,$password){
$conn=new Mysql();
$sql="select * from user where name='{$username}' and password='{$password}';";
$result=$conn->sql($sql);
if($result){
return true;
}
else{
echo '<html><head><Script Language="JavaScript">alert("用戶(hù)不存在");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\">";
}
$conn->close();
}
//方法:手機(jī)格式驗(yàn)證
function checkPhoneNum($phonenumber){
$preg="/^1[34578]{1}\d{9}$/";
if(preg_match($preg,$phonenumber)){
return ture; //驗(yàn)證通過(guò)
}else{
echo '<html><head><Script Language="JavaScript">alert("手機(jī)號(hào)碼格式有誤");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\">";//手機(jī)號(hào)碼格式不對(duì)
}
}
//方法:郵箱格式驗(yàn)證
function checkEmail($email){
$preg = '/^(\w{1,25})@(\w{1,16})(\.(\w{1,4})){1,3}$/';
if(preg_match($preg, $email)){
return true;
}else{
echo '<html><head><Script Language="JavaScript">alert("y郵箱格式有誤");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\">";
}
}
logout.php
<?php
//退出登錄并跳轉(zhuǎn)到登錄頁(yè)面
unset($_SESSION['username']);
setcookie("username","",time()-1); //清空cookie
setcookie("password","",time()-1);
header("location: login.html ");
index.php
<?php
session_start();
if(empty($_COOKIE['username'])&&empty($_COOKIE['password'])){
if(isset($_SESSION['username']))
echo "登錄成功,歡迎您".$_SESSION['username']."<a href='logout.php'>退出登錄</a>";
else
echo "你還沒(méi)有登錄,<a href='login.html'>請(qǐng)登錄</a>";
}
else
echo "登錄成功,歡迎您:".$_COOKIE['username']."<a href='logout.php'>退出登錄</a>";
驗(yàn)證碼和數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法前面寫(xiě)過(guò),這里不再贅述。
驗(yàn)證碼制作://chabaoo.cn/article/156850.htm
數(shù)據(jù)庫(kù)連接://chabaoo.cn/article/156875.htm
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》、《php+mysqli數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP5.0 TIDY_PARSE_FILE緩沖區(qū)溢出漏洞的解決方案
這篇文章主要給大家介紹了關(guān)于PHP5.0 TIDY_PARSE_FILE緩沖區(qū)溢出漏洞的解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
PHP連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)注冊(cè)頁(yè)面的增刪改查操作
這篇文章主要介紹了PHP連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)注冊(cè)頁(yè)面的增刪改查操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03
PHP中如何使用Redis接管文件存儲(chǔ)Session詳解
這篇文章主要給大家介紹了關(guān)于在PHP中如何使用Redis接管文件存儲(chǔ)Session的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
php中sprintf與printf函數(shù)用法區(qū)別解析
這篇文章主要介紹了php中sprintf與printf函數(shù)用法區(qū)別解析,需要的朋友可以參考下2014-02-02
php 備份數(shù)據(jù)庫(kù)代碼(生成word,excel,json,xml,sql)
本篇文章是對(duì)php備份數(shù)據(jù)庫(kù)代碼(生成word,excel,json,xml,sql)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

