本文实例讲述了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="https://www.atool.online/article/captcha.php?r=<?php echo rand();?>" alt="验证码"> <label><a href="javascript:void(0)" rel="external nofollow" onclick="document.getElementById('captcha_img').src='https://www.atool.online/article/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='https://www.atool.online/article/logout.php'>退出登录</a>"; else echo "你还没有登录,<a href='https://www.atool.online/article/login.html'>请登录</a>"; } else echo "登录成功,欢迎您:".$_COOKIE['username']."<a href='https://www.atool.online/article/logout.php'>退出登录</a>";
验证码和数据库的实现方法前面写过,这里不再赘述。
验证码制作://www.atool.online/article/156850.htm
数据库连接://www.atool.online/article/156875.htm
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP网络编程技巧总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。