<?php $name_error = ""; $passwd_error = ""; define('HOST','localhost'); define('USER','root'); define('PASSWD',''); function input_filter($input){ $input = trim($input); $input = addslashes($input); return $input; } if($_SERVER['REQUEST_METHOD']=='POST'){ if(empty($_POST['name'])){ $name_error = "*用户名不能为空"; }elseif(empty($_POST['passwd'])){ $passwd_error = "*密码不能为空"; }else{ $name = input_filter($_POST['name']); $passwd = input_filter($_POST['passwd']); $passwd = md5($passwd); $conn = new mysqli(HOST,USER,PASSWD); if($conn->connect_error){ die('error:'.$conn->connect_error); } $conn->select_db('php_practice'); $stmt = $conn->prepare('select * from user where name=? and passwd=?'); $stmt->bind_param('ss',$name,$passwd); $stmt->execute(); $stmt->store_result(); if($stmt->num_rows){ echo '<script>alert("登陆成功!")</script>'; }else{ echo '<script>alert("登陆失败!")</script>'; } } } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Please login</title> </head> <body> <form action="login.php" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="name"></td> </tr> <tr><td style="color:red"><?php echo $name_error; ?></td></tr> <tr> <td>密码:</td> <td><input type="password" name="passwd"></td> </tr> <tr><td style="color:red"><?php echo $passwd_error; ?></td></tr> </table> <input type="submit"> </form> </body> </html>
打赏我,让我更有动力~
© 2016 - 2024 掌控者 All Rights Reserved.
Track-方丈
发表于 2018-3-28
通过,积分金币已发放。
tips: 既然都使用对象和预处理了,那索性在研究一下PDO吧。
评论列表
加载数据中...