php注册、登录界面的制作

当初我觉得一个网站上注册和登录这两个功能很神奇,后来自己研究一下发现其实道理很简单,接下来看一下怎么实现的吧。。。。

我实在我的电脑上建了几个文件:

login.html (登录页面)

register.html(注册页面)

success.html(登录成功跳转页面)

return.html(注册成功页面)

login.php

register.php

登录界面和注册界面以及success.html并没有

什么都是些html标记如下:

 1 <html>
 2 <head>
 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 4 <title>登录界面</title>
 5 </head>
 6 
 7 <body>
 8 <form method="post" action="login.php">
 9 账号:
10 <input type="text" name="usernamel"><br/><br/>
11 密码:
12 <input type="password" name="passwordl">
13 <input type="submit" value="登录" name="subl">
14 <a href="http://127.0.0.1:8080/register.html">没有账号,注册</a>
15 </form>
16 </body>
17 </html>
View Code
 1 <html>
 2 <head>
 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 4 <title>会员注册</title>
 5 </head>
 6 
 7 <body>
 8 <form method="post" action="register.php">
 9&nbsp;&nbsp;户:
10 <input type="text" name="username"><br/><br/>
11&nbsp;&nbsp;码:
12 <input type="password" name="password"><br/><br/>
13 密码确认:
14 <input type="password" name="password2">
15 <input type="submit" value="注册" name="sub">
16 </form>
17 </body>
18 </html>
View Code
View Code

return.html是注册成功之后呈现的页面,里面有一段js代码是用来定时返回登录界面的

 1 <html>
 2 <head>
 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 4 <title>无标题文档</title>
 5 </head>
 6 
 7 <body>
 8 注册成功!<br/>
 9 5秒后返回登录界面<br/>
10 你也可以直接点击回到<a href="http://127.0.0.1:8080/login.html">登录页面</a>
11 <script type="text/javascript">
12 setTimeout("ren()",5000);
13 function ren()
14 {
15     window.location="http://127.0.0.1:8080/login.html";
16 }
17 
18 </script>
19 
20 </body>
21 </html>
View Code

register.php这是与注册页面相对应后台页面

 1 <?php
 2 $link=mysql_connect("localhost","root","207207");//链接数据库
 3 header("Content-type:text/html;charset=utf-8");
 4 if($link)
 5     {    
 6         //echo"链接数据库成功";
 7         $select=mysql_select_db("login",$link);//选择数据库
 8         if($select)
 9         {
10             //echo"选择数据库成功!";
11             if(isset($_POST["sub"]))
12             {
13                 $name=$_POST["username"];
14                 $password1=$_POST["password"];//获取表单数据
15                 $password2=$_POST["password2"];
16                 if($name==""||$password1=="")//判断是否填写
17                 {
18                     echo"<script type="."""."text/javascript".""".">"."window.alert"."("."""."请填写完成!".""".")".";"."</script>";
19                     echo"<script type="."""."text/javascript".""".">"."window.location="."""."http://127.0.0.1:8080/register.html"."""."</script>";        
20                     exit;
21                 }
22                 if($password1==$password2)//确认密码是否正确
23                 {
24                 $str="select count(*) from register where username="."'"."$name"."'";
25                 $result=mysql_query($str,$link);
26                 $pass=mysql_fetch_row($result);
27                 $pa=$pass[0];
28                 if($pa==1)//判断数据库表中是否已存在该用户名
29                 {
30                 
31                 echo"<script type="."""."text/javascript".""".">"."window.alert"."("."""."该用户名已被注册".""".")".";"."</script>";
32                 echo"<script type="."""."text/javascript".""".">"."window.location="."""."http://127.0.0.1:8080/register.html"."""."</script>";      
33                 exit;  
34                 }
35                 
36                 
37                 $sql="insert into register values("."""."$name".""".","."""."$password1".""".")";//将注册信息插入数据库表中
38                 //echo"$sql";
39                 mysql_query($sql,$link);
40                 mysql_query('SET NAMES UTF8');
41                 $close=mysql_close($link);
42                 if($close)
43                 {
44                     //echo"数据库关闭";
45                     //echo"注册成功!";
46                     echo"<script type="."""."text/javascript".""".">"."window.location="."""."http://127.0.0.1:8080/return.html"."""."</script>";        
47                 }
48                 }
49                 else
50                 {
51                     echo"<script type="."""."text/javascript".""".">"."window.alert"."("."""."密码不一致!".""".")".";"."</script>";
52                     echo"<script type="."""."text/javascript".""".">"."window.location="."""."http://127.0.0.1:8080/register.html"."""."</script>";        
53                 }
54             }
55         }
56     }
57 ?>

login.php登录界面对应后台文件

 1 <?php
  header("Content-type:text/html;charset=utf-8");
2 $link=mysql_connect("localhost","root","207207"); 3 if($link) 4 { 5 $select=mysql_select_db("login",$link); 6 if($select) 7 { 8 if(isset($_POST["subl"])) 9 { 10 $name=$_POST["usernamel"]; 11 $password=$_POST["passwordl"]; 12 if($name==""||$password=="")//判断是否为空 13 { 14 echo"<script type="."""."text/javascript".""".">"."window.alert"."("."""."请填写正确的信息!".""".")".";"."</script>"; 15 echo"<script type="."""."text/javascript".""".">"."window.location="."""."http://127.0.0.1:8080/login.html"."""."</script>"; 16 exit; 17 } 18 $str="select password from register where username="."'"."$name"."'"; 19 mysql_query('SET NAMES UTF8');20 $result=mysql_query($str,$link); 21 $pass=mysql_fetch_row($result); 22 $pa=$pass[0]; 23 if($pa==$password)//判断密码与注册时密码是否一致 24 { 25 echo"登录成功!"; 26 echo"<script type="."""."text/javascript".""".">"."window.location="."""."http://127.0.0.1:8080/success.html"."""."</script>"; 27 } 28 { 29 echo"<script type="."""."text/javascript".""".">"."window.alert"."("."""."登录失败!".""".")".";"."</script>"; 30 echo"<script type="."""."text/javascript".""".">"."window.location="."""."http://127.0.0.1:8080/login.html"."""."</script>"; 31 } 32 } 33 34 } 35 } 36 ?>

自己闲来无事做的还有许多要完善的地方,欢迎大家提问讨论,提供更简便的方法!

原文地址:https://www.cnblogs.com/itsuibi/p/5430980.html