-
09-06
-
12-27
-
01-08
-
12-31
-
12-31
PHP实现验证登录
发布时间:2021-01-08 12:01:48
文章来源:原创
访问次数:11
<?php //初始化值 $name = isset($_POST['name'])?$_POST['name']:''; $pass = isset($_POST['passwd'])?$_POST['passwd']:''; //去除首尾空 $name = trim($name); $pass = trim($pass); //正则表达式 $res_name = preg_match('/^[0-9a-zA-Z]{1,}$/',$name); $res_pass = preg_match('/^[0-9a-zA-Z_]{1,}$/',$pass); if (!$res_name) { echo "用户名存在非法字符串"; die(); } if (!$res_pass) { echo "密码存在非法字符串"; die(); } //链接数据库 $conn = mysqli_connect('127.0.0.1','root','root','test'); //设置字符集 mysqli_set_charset($conn,'utf-8'); //数据库执行语句 //$sql = "SELECT * FROM lhm_user WHERE u_name = '".$name."' AND u_pass = '".$pass."'"; $sql = "SELECT * FROM lhm_user WHERE u_name = '".$name."'"; //执行语句 $res = mysqli_query($conn, $sql); //处理结果集 $row = mysqli_fetch_assoc($res); //拿用户输入账号密码与数据库相比较 if ($name!=''&&$pass!='') { if ($_POST['name']!=$row['u_name']) { echo '<script>alert("账号错误"); location="./login.php"</script>'; }else if ($row['u_pass']!=$_POST['passwd']) { echo '<script>alert("密码错误"); location="./login.php"</script>'; }else{ echo "账号密码正确"; } }
本文链接:https://www.928118.com/php/65.html
文章评论
共 0 条评论,查看全部
- 这篇文章还没有收到评论,赶紧来抢沙发吧~
最新资讯