本文實例講述了PHP登錄驗證功能。分享給大家供大家參考,具體如下:
登錄界面
具體實現(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="驗證碼" 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"; //導(dǎo)入mysql.php訪問數(shù)據(jù)庫
session_start(); //開啟會話一獲取到服務(wù)器端驗證碼
$username=$_POST['username'];
$password=$_POST['password'];
$autologin=isset($_POST['autologin'])?1:0; //獲取是否選擇了自動登錄
$verifycode=$_POST['verifycode'];
$code=$_SESSION['code']; //獲取服務(wù)器生成的驗證碼
/*
* 首先進(jìn)行判空操作,通過后進(jìn)行驗證碼驗證,通過后再進(jìn)行數(shù)據(jù)庫驗證。
* 手機(jī)號碼和郵箱驗證可根據(jù)需要自行添加
* */
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); //有效期設(shè)置為3天
setcookie("password",md5($password),time()+3600*24*3);
}
else{
setcookie("username","",time()-1); //如果沒有選擇自動登錄就清空cookie
setcookie("password","",time()-1);
}
header("location: index.php "); //全部驗證都通過之后跳轉(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("驗證碼為空");/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\">";
}
}
//方法:查詢用戶是否在數(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ī)格式驗證
function checkPhoneNum($phonenumber){
$preg="/^1[34578]{1}\d{9}$/";
if(preg_match($preg,$phonenumber)){
return ture; //驗證通過
}else{
echo 'html>head>Script Language="JavaScript">alert("手機(jī)號碼格式有誤");/Script>/head>/html>' . "meta http-equiv=\"refresh\" content=\"0;url=login.html\">";//手機(jī)號碼格式不對
}
}
//方法:郵箱格式驗證
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'>請登錄/a>";
}
else
echo "登錄成功,歡迎您:".$_COOKIE['username']."a href='logout.php'>退出登錄/a>";
驗證碼和數(shù)據(jù)庫的實現(xiàn)方法前面寫過,這里不再贅述。
驗證碼制作://www.jb51.net/article/156850.htm
數(shù)據(jù)庫連接://www.jb51.net/article/156875.htm
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫操作入門教程》、《php+mysqli數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
您可能感興趣的文章:- asp.net使用ODP即oracle連接方式的的防注入登錄驗證程序
- php調(diào)用百度人臉識別接口查詢數(shù)據(jù)庫人臉信息實現(xiàn)驗證登錄功能
- Android登錄注冊功能 數(shù)據(jù)庫SQLite驗證
- QT連接Oracle數(shù)據(jù)庫并實現(xiàn)登錄驗證的操作步驟