Home > Backend Development > PHP Problem > How to implement database verification jump in php

How to implement database verification jump in php

藏色散人
Release: 2023-03-11 06:40:02
Original
2025 people have browsed it

php method to implement database verification jump: first create the login.html file; then create the doLogin.php file; then perform a null operation, and then perform verification code verification after passing it; and finally perform database verification.

How to implement database verification jump in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How does php achieve database verification jump?

Examples of PHP login verification function [user name, password, verification code, database, logged in verification, automatic login and logout, etc.]

The example in this article describes the PHP login verification function. Share it with everyone for your reference, the details are as follows:

Login interface

How to implement database verification jump in php

How to implement database verification jump in php

##The specific implementation method is as follows:

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="验证码" name="verifycode" <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(&#39;captcha_img&#39;).src=&#39;captcha.php?r=&#39;+Math.random()" 换一个</a  </label <br 
  <label <input type="checkbox" name="autologin[]" value="1"/ 自动登录</label <br 
  <button type="submit" 登录</button 
</form 
</body 
</html
Copy after login

doLogin.php

<?php
header("Content-type:text/html;charset=UTF-8");
require "mysql.php";      //导入mysql.php访问数据库
session_start();        //开启会话一获取到服务器端验证码
$username=$_POST[&#39;username&#39;];
$password=$_POST[&#39;password&#39;];
$autologin=isset($_POST[&#39;autologin&#39;])?1:0;   //获取是否选择了自动登录
$verifycode=$_POST[&#39;verifycode&#39;];
$code=$_SESSION[&#39;code&#39;];    //获取服务器生成的验证码
/*
* 首先进行判空操作,通过后进行验证码验证,通过后再进行数据库验证。
* 手机号码和邮箱验证可根据需要自行添加
* */
if(checkEmpty($username,$password,$verifycode)){
if(checkVerifycode($verifycode,$code)){
if(checkUser($username,$password)){
$_SESSION[&#39;username&#39;]=$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 &#39;<html <head <Script Language="JavaScript" alert("用户名或密码为空");</Script </head </html &#39; . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\" ";
}
else{
if($verifycode==null){
echo &#39;<html <head <Script Language="JavaScript" alert("验证码为空");</Script </head </html &#39; . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\" ";
}
else{
return true;
}
}
}
//方法:检查验证码是否正确
function checkVerifycode($verifycode,$code){
if($verifycode==$code){
return true;
}
else{
echo &#39;<html <head <Script Language="JavaScript" alert("验证码错误");</Script </head </html &#39; . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\" ";
}
}
//方法:查询用户是否在数据库中
function checkUser($username,$password){
$conn=new Mysql();
$sql="select * from user where name=&#39;{$username}&#39; and password=&#39;{$password}&#39;;";
$result=$conn- sql($sql);
if($result){
return true;
}
else{
echo &#39;<html <head <Script Language="JavaScript" alert("用户不存在");</Script </head </html &#39; . "<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 &#39;<html <head <Script Language="JavaScript" alert("手机号码格式有误");</Script </head </html &#39; . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\" ";//手机号码格式不对
}
}
//方法:邮箱格式验证
function checkEmail($email){
$preg = &#39;/^(\w{1,25})@(\w{1,16})(\.(\w{1,4})){1,3}$/&#39;;
if(preg_match($preg, $email)){
return true;
}else{
echo &#39;<html <head <Script Language="JavaScript" alert("y邮箱格式有误");</Script </head </html &#39; . "<meta http-equiv=\"refresh\" content=\"0;url=login.html\" ";
}
}
Copy after login

logout.php

<?php
//退出登录并跳转到登录页面
unset($_SESSION[&#39;username&#39;]);
setcookie("username","",time()-1);  //清空cookie
setcookie("password","",time()-1);
header("location: login.html ");
index.php
<?php
session_start();
if(empty($_COOKIE[&#39;username&#39;])&&empty($_COOKIE[&#39;password&#39;])){
if(isset($_SESSION[&#39;username&#39;]))
echo "登录成功,欢迎您".$_SESSION[&#39;username&#39;]."<a href=&#39;logout.php&#39; 退出登录</a ";
else
echo "你还没有登录,<a href=&#39;login.html&#39; 请登录</a ";
}
else
echo "登录成功,欢迎您:".$_COOKIE[&#39;username&#39;]."<a href=&#39;logout.php&#39; 退出登录</a ";
Copy after login

The implementation methods of verification code and database have been written before and will not be repeated here.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to implement database verification jump in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template