Posted in PHP onFebruary 25, 2019
本文实例讲述了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
<?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='logout.php'>退出登录</a>";
else
echo "你还没有登录,<a href='login.html'>请登录</a>";
}
else
echo "登录成功,欢迎您:".$_COOKIE['username']."<a href='logout.php'>退出登录</a>";
希望本文所述对大家PHP程序设计有所帮助。
PHP登录验证功能示例【用户名、密码、验证码、数据库、已登陆验证、自动登录和注销登录等】
- Author -
不能吃的坚果声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@