写了一个简单的html表单,提交后通过php处理,连接到mysql数据库,从而完成注册和登录的功能。
html表单页面。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录注册页面</title>
</head>
<body>
<form action="login.php" method="post">
账号:<input type="text" name = 'username'><br>
密码:<input type="password" name="password"><br>
登录:<input type="radio" name="login"><br>
注册:<input type="radio" name="regedit"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
php表单验证
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录注册详情</title>
</head>
<body>
</body>
</html>
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$dl = $_POST['login'];
$zc = $_POST['regedit'];
$conn = mysqli_connect('127.0.0.1','root','root');
mysqli_select_db($conn,'php');
if($dl == 'on'){
$sql = "select * from userinfo where name = '$username'&& pass = '$password'";
$result = mysqli_query($conn,$sql);
$srow = mysqli_fetch_array($result);
if($srow != null){
echo'<h1>登录成功</h1>';
}
else{
echo'<h1>登录失败</h1>';
}
}
if($zc == 'on'){
$sql = "insert into userinfo value('$username','$password')";
$result = mysqli_query($conn,$sql);
$srow = mysqli_fetch_array($result);
echo'<h1>注册成功!</h1>';
}
?>
可以成功地进行登录和注册。




数据库中也可以看到注册的用户。

但这个时候会出现一个问题,输入一些特殊字符,即便数据库中没有该用户的记录也可以成功登录。


解决办法:可以加入正则匹配来限制用户的输入。
$zhengze = preg_match_all('/or|and|\'/',$username);
if($zhengze != 0){
die('<h1>请别输入非法字符</h1>');
}

