您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“php實(shí)現(xiàn)數(shù)據(jù)庫驗(yàn)證跳轉(zhuǎn)的方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“php實(shí)現(xiàn)數(shù)據(jù)庫驗(yàn)證跳轉(zhuǎn)的方法”吧!
php實(shí)現(xiàn)數(shù)據(jù)庫驗(yàn)證跳轉(zhuǎn)的方法:首先創(chuàng)建login.html文件;然后創(chuàng)建doLogin.php文件;接著進(jìn)行判空操作,通過后進(jìn)行驗(yàn)證碼驗(yàn)證;最后再進(jìn)行數(shù)據(jù)庫驗(yàn)證即可。
本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦
php怎么實(shí)現(xiàn)數(shù)據(jù)庫驗(yàn)證跳轉(zhuǎn)?
PHP登錄驗(yàn)證功能示例【用戶名、密碼、驗(yàn)證碼、數(shù)據(jù)庫、已登陸驗(yàn)證、自動登錄和注銷登錄等】
本文實(shí)例講述了PHP登錄驗(yàn)證功能。分享給大家供大家參考,具體如下:
登錄界面
具體實(shí)現(xiàn)方法如下:
login.html
<!DOCTYPE html <html <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" <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"/ 自動登錄</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ù)器端驗(yàn)證碼 $username=$_POST['username']; $password=$_POST['password']; $autologin=isset($_POST['autologin'])?1:0; //獲取是否選擇了自動登錄 $verifycode=$_POST['verifycode']; $code=$_SESSION['code']; //獲取服務(wù)器生成的驗(yàn)證碼 /* * 首先進(jìn)行判空操作,通過后進(jìn)行驗(yàn)證碼驗(yàn)證,通過后再進(jìn)行數(shù)據(jù)庫驗(yàn)證。 * 手機(jī)號碼和郵箱驗(yàn)證可根據(jù)需要自行添加 * */ if(checkEmpty($username,$password,$verifycode)){ if(checkVerifycode($verifycode,$code)){ if(checkUser($username,$password)){ $_SESSION['username']=$username; //保存此時(shí)登錄成功的用戶名 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 "); //全部驗(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ī)號碼格式有誤");</Script </head </html ' . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\" ";//手機(jī)號碼格式不對 } } //方法:郵箱格式驗(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' 請登錄</a "; } else echo "登錄成功,歡迎您:".$_COOKIE['username']."<a href='logout.php' 退出登錄</a ";
驗(yàn)證碼和數(shù)據(jù)庫的實(shí)現(xiàn)方法前面寫過,這里不再贅述。
到此,相信大家對“php實(shí)現(xiàn)數(shù)據(jù)庫驗(yàn)證跳轉(zhuǎn)的方法”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。