首页 > 开发 > PHP > 正文

PHP登录验证功能示例【用户名、密码、验证码、数据库、已登陆

2024-05-04 22:40:57
字体:
来源:转载
供稿:网友

本文实例讲述了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

<?phpheader("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/">";  }}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表