代码共四个文件
1.loginsign.html
<html>
<head>
<meta charset="utf-8">
<title>登陆注册系统</title>
</head>
<h1>欢迎来到登陆注册系统</h1>
<h2>welcome to login and sign system</h2>
<form action="loginsign.php" method="POST">
帐号 <input type="text" name='username'/><br /><br />
密码 <input type="password" name='password'/> <br /><br />
登陆<input type="submit" value="login"/>
</form>
<form action="sign.html">
注册<input type="submit" value="sign"/>
</form>
</html>
2.sign.html
<html>
<head>
<meta charset="utf-8">
<title>注册系统</title>
</head>
<h1>欢迎新用户</h1>
<form action="sign.php" method="POST">
请输入你的帐号 <input type="text" name='username'/><br /><br />
请输入你的密码 <input type="password" name='password'/> <br /><br />
确认注册<input type="submit" name= 'sign' value="sign"/>
</form>
<form action="loginsign.html">
<input type="submit" value="返回登陆"/>
</form>
</html>
3.loginsign.php
<meta charset="utf-8">
<?php
$username = $_POST['username'];//获取传参
$password = $_POST['password'];
$con = mysqli_connect('localhost','phpwebtest','phpwebtest','phpwebtest');
mysqli_select_db($con,'phpwebtest');//连接数据库
$sql="select*from users where username ='$username' and passwd ='$password'";//执行sql语句
$result=mysqli_query($con,$sql);//得到查询结果 object
if ($username== NULL or $password == NULL) {
echo '帐号或密码为空,请重新登陆';
}
elseif(mysqli_fetch_array($result)['username']==$username){
echo '登陆成功';
}
else{
echo '该帐户不存在,请返回注册完成后再登陆';
}
//转换为数组,判断输入的正确与否
?>
4.sign.php
<?php
$sign = $_POST['sign'];
$username = $_POST['username'];//获取传参
$password = $_POST['password'];
if (($username or $password) != NULL) {
$con = mysqli_connect('localhost','phpwebtest','phpwebtest','phpwebtest');
mysqli_select_db($con,'phpwebtest');//连接数据库
$sql="insert into users value('$username','$password')";//执行sql语句
$result=mysqli_query($con,$sql);//得到结果 object
echo '注册成功,请返回登陆';
}else {
echo '请输入正确的密码完成注册!';
}
?>
功能展示
1.随便输入账号密码
跳转新页面返回如下:
2.不输入或填写不完整登陆
跳转新页面返回如下:
3.注册
留空填写注册会返回
输入正确注册:
点击返回登陆
4.登陆
提示登陆成功