當初我覺得一個網站上注冊和登錄這兩個功能很神奇,后來自己研究一下發現其實道理很簡單,接下來看一下怎么實現的吧。。。。
我實在我的電腦上建了幾個文件:
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>

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 賬 戶: 10 <input type="text" name="username"><br/><br/> 11 密 碼: 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>

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>
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 ?>
自己閑來無事做的還有許多要完善的地方,歡迎大家提問討論,提供更簡便的方法!