简单登录注册页面

使用工具:PHPstudy

打开PHPstudy,进入WWW目录,我是建了3个html文件和2个php文件,分别是login.html(登录页面)、enter.php(登录后台判断)、register.html(注册页面)、register.php(注册后台判断)、welcome.html(登陆成功后欢迎界面)。第一次做web页面,很简陋……代码:

login.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>登录页面</title>
<style type="text/css">
form {
text-align: center;
}
</style>
</head>
<body>
<form method="post" action="enter.php">
用户名:<input type="text" name='username' maxlength="20"/><br />
密 码:<input type="password" name='password' maxlength="18"/><br /><br />
<input type="submit" value='登录' name="submit" />
<a href="register.html">
<input type="button" value="注册" name="button" />
</a>
</form>
</body>
</html>

enter.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
header('content-type:text/html;charset=utf-8');
error_reporting(E_ALL ^ E_NOTICE);//屏蔽notice提示错误
//初次访问
if ( ( $_POST['username'] != null ) && ( $_POST['password'] != null ) ) {
$userName = $_POST['username'];
$password = $_POST['password'];
$con = mysqli_connect('localhost','root','root');//连接数据库
if (!$con) {
die('数据库连接失败'.$mysql_error());
}
mysqli_select_db($con,"ymh");//选择数据库

$sql = "select * from user where username = '{$userName}' ";
$res = mysqli_query($con,$sql);
$arr = mysqli_fetch_assoc($res);
if($arr){
if ($arr['password'] == $password)
{
setcookie('username',$userName,time()+3600,'/');
setcookie('password',$password,time()+3600,'/');
header('Location: welcome.html');
}
else
{
echo"<script>alert('用户名或密码错误!');location.href = 'login.html';</script>";
}
}else{
echo"<script>alert('用户名不存在!');location.href = 'login.html';</script>";
}
}else{
echo"<script>alert('用户名和密码不能为空!');location.href = 'login.html';</script>";
}
//再次访问
if ( ($_COOKIE['username']!= null)&&($_COOKIE['password']!= null) ) {
$userName = $_COOKIE['username'];
$password = $_COOKIE['password'];
$con = mysqli_connect('localhost','root','root','ymh');
$res = mysqli_query($con,"select * from ymh where username='{$userName}'");
$row = mysqli_fetch_assoc($res);
if ($row['password'] == $password) {
header('Location: welcome.html' . "?username={$userName}");
}
else{
echo"<script>location.href = 'login.html';</script>";
}
}
?>

register.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>注册页面</title>
<style type="text/css">
form {
text-align: center;
}
</style>
</head>
<body>
<form method="post" action="register.php">
  用户名:<input type="text" name='username' maxlength="20"/><br />
请设置密码:<input type="password" name='password1' maxlength="18"/><br />
请确认密码:<input type="password" name='password2' maxlength="18"/><br /><br />
<input type="submit" value="立即注册" name="submit" style="margin-left:99px; margin-right:0px;padding-left:60px;padding-right:57px;"/>
</form>
</body>
</html>

register.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php 
header('content-type:text/html;charset=utf-8');
error_reporting(E_ALL ^ E_NOTICE);
if ( ( $_POST['username'] != null ) && ( $_POST['password1'] != null ) && ( $_POST['password2'] != null )){
$userName = $_POST['username'];
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];
$con = mysqli_connect("localhost","root","root");
mysqli_select_db($con,"ymh");
$sql1 = "select * from user where username = '{$userName}' ";
$res = mysqli_query($con,$sql1);
$row = mysqli_num_rows($res);
if($row>0){
echo"<script>alert('用户名已存在!');location.href = 'register.html';</script>";
exit;
}else{
if($password1!==$password2){
echo"<script>alert('两次输入的密码不一致!');location.href = 'register.html';</script>";
}else{
$sql2="insert into user(username,password) values ('$userName','$password1')";
if(mysqli_query($con,$sql2)){
echo"<script>alert('注册成功!');location.href = 'login.html';</script>";
}else{
echo"<script>alert('注册失败!');location.href = 'register.html';</script>";
}
}
}
}else{
echo"<script>alert('用户名和密码不能为空!');location.href = 'register.html';</script>";
}
?>

welcome.html

1
2
3
4
5
6
7
8
9
<html>
<head>
<meta charset="UTF-8">
<title>欢迎登陆</title>
</head>
<body>
<p>Hello World</p>
</body>
</html>