PHP登錄驗(yàn)證功能示例【用戶名、密碼、驗(yàn)證碼、數(shù)據(jù)庫、已登陸驗(yàn)證、自動(dòng)登錄和注銷登錄等】
本文實(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="用戶名" 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訪問數(shù)據(jù)庫 session_start(); //開啟會(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)行判空操作,通過后進(jìn)行驗(yàn)證碼驗(yàn)證,通過后再進(jìn)行數(shù)據(jù)庫驗(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í)登錄成功的用戶名 if($autologin==1){ //如果用戶勾選了自動(dòng)登錄就把用戶名和加了密的密碼放到cookie里面 setcookie("username",$username,time()+3600*24*3); //有效期設(shè)置為3天 setcookie("password",md5($password),time()+3600*24*3); } else{ setcookie("username","",time()-1); //如果沒有選擇自動(dòng)登錄就清空cookie setcookie("password","",time()-1); } header("location: index.php "); //全部驗(yàn)證都通過之后跳轉(zhuǎn)到首頁 } } } //方法:判斷是否為空 function checkEmpty($username,$password,$verifycode){ if($username==null||$password==null){ echo '<html><head><Script Language="JavaScript">alert("用戶名或密碼為空");</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\">"; } } //方法:查詢用戶是否在數(shù)據(jù)庫中 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("用戶不存在");</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)證通過 }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)到登錄頁面 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 "你還沒有登錄,<a href='login.html'>請(qǐng)登錄</a>"; } else echo "登錄成功,歡迎您:".$_COOKIE['username']."<a href='logout.php'>退出登錄</a>";
驗(yàn)證碼和數(shù)據(jù)庫的實(shí)現(xiàn)方法前面寫過,這里不再贅述。
驗(yàn)證碼制作://www.dbjr.com.cn/article/156850.htm
數(shù)據(jù)庫連接://www.dbjr.com.cn/article/156875.htm
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫操作入門教程》、《php+mysqli數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP5.0 TIDY_PARSE_FILE緩沖區(qū)溢出漏洞的解決方案
這篇文章主要給大家介紹了關(guān)于PHP5.0 TIDY_PARSE_FILE緩沖區(qū)溢出漏洞的解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10PHP連接數(shù)據(jù)庫實(shí)現(xiàn)注冊(cè)頁面的增刪改查操作
這篇文章主要介紹了PHP連接數(shù)據(jù)庫實(shí)現(xiàn)注冊(cè)頁面的增刪改查操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03PHP中如何使用Redis接管文件存儲(chǔ)Session詳解
這篇文章主要給大家介紹了關(guān)于在PHP中如何使用Redis接管文件存儲(chǔ)Session的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11php中sprintf與printf函數(shù)用法區(qū)別解析
這篇文章主要介紹了php中sprintf與printf函數(shù)用法區(qū)別解析,需要的朋友可以參考下2014-02-02php 備份數(shù)據(jù)庫代碼(生成word,excel,json,xml,sql)
本篇文章是對(duì)php備份數(shù)據(jù)庫代碼(生成word,excel,json,xml,sql)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06