PHP登錄驗證功能示例【用戶名、密碼、驗證碼、數據庫、已登陸驗證、自動登錄和注銷登錄等】
本文實例講述了PHP登錄驗證功能。分享給大家供大家參考,具體如下:
登錄界面




具體實現方法如下:
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="驗證碼" name="verifycode" class="captcha"><br><br>
<img id="captcha_img" src="captcha.php?r=<?php echo rand();?>" alt="驗證碼">
<label><a href="javascript:void(0)" rel="external nofollow" onclick="document.getElementById('captcha_img').src='captcha.php?r='+Math.random()">換一個</a> </label><br>
<label><input type="checkbox" name="autologin[]" value="1"/>自動登錄</label><br>
<button type="submit">登錄</button>
</form>
</body>
</html>
doLogin.php
<?php
header("Content-type:text/html;charset=UTF-8");
require "mysql.php"; //導入mysql.php訪問數據庫
session_start(); //開啟會話一獲取到服務器端驗證碼
$username=$_POST['username'];
$password=$_POST['password'];
$autologin=isset($_POST['autologin'])?1:0; //獲取是否選擇了自動登錄
$verifycode=$_POST['verifycode'];
$code=$_SESSION['code']; //獲取服務器生成的驗證碼
/*
* 首先進行判空操作,通過后進行驗證碼驗證,通過后再進行數據庫驗證。
* 手機號碼和郵箱驗證可根據需要自行添加
* */
if(checkEmpty($username,$password,$verifycode)){
if(checkVerifycode($verifycode,$code)){
if(checkUser($username,$password)){
$_SESSION['username']=$username; //保存此時登錄成功的用戶名
if($autologin==1){ //如果用戶勾選了自動登錄就把用戶名和加了密的密碼放到cookie里面
setcookie("username",$username,time()+3600*24*3); //有效期設置為3天
setcookie("password",md5($password),time()+3600*24*3);
}
else{
setcookie("username","",time()-1); //如果沒有選擇自動登錄就清空cookie
setcookie("password","",time()-1);
}
header("location: index.php "); //全部驗證都通過之后跳轉到首頁
}
}
}
//方法:判斷是否為空
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("驗證碼為空");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\">";
}
else{
return true;
}
}
}
//方法:檢查驗證碼是否正確
function checkVerifycode($verifycode,$code){
if($verifycode==$code){
return true;
}
else{
echo '<html><head><Script Language="JavaScript">alert("驗證碼錯誤");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\">";
}
}
//方法:查詢用戶是否在數據庫中
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();
}
//方法:手機格式驗證
function checkPhoneNum($phonenumber){
$preg="/^1[34578]{1}\d{9}$/";
if(preg_match($preg,$phonenumber)){
return ture; //驗證通過
}else{
echo '<html><head><Script Language="JavaScript">alert("手機號碼格式有誤");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\">";//手機號碼格式不對
}
}
//方法:郵箱格式驗證
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
//退出登錄并跳轉到登錄頁面
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'>請登錄</a>";
}
else
echo "登錄成功,歡迎您:".$_COOKIE['username']."<a href='logout.php'>退出登錄</a>";
驗證碼和數據庫的實現方法前面寫過,這里不再贅述。
驗證碼制作://www.dbjr.com.cn/article/156850.htm
數據庫連接://www.dbjr.com.cn/article/156875.htm
更多關于PHP相關內容感興趣的讀者可查看本站專題:《php+mysql數據庫操作入門教程》、《php+mysqli數據庫程序設計技巧總結》、《php面向對象程序設計入門教程》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《PHP網絡編程技巧總結》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
PHP5.0 TIDY_PARSE_FILE緩沖區(qū)溢出漏洞的解決方案
這篇文章主要給大家介紹了關于PHP5.0 TIDY_PARSE_FILE緩沖區(qū)溢出漏洞的解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-10-10
php中sprintf與printf函數用法區(qū)別解析
這篇文章主要介紹了php中sprintf與printf函數用法區(qū)別解析,需要的朋友可以參考下2014-02-02
php 備份數據庫代碼(生成word,excel,json,xml,sql)
本篇文章是對php備份數據庫代碼(生成word,excel,json,xml,sql)進行了詳細的分析介紹,需要的朋友參考下2013-06-06

