注冊頁面
reg.html 負責收集用戶填寫的注冊信息。教程里只列出關鍵的代碼片段,完整的代碼附在本節最后。
注冊表單
1 <fieldset> 2 <legend>用戶注冊</legend> 3 <form name="RegForm" method="post" action="reg.php" onSubmit="return InputCheck(this)"> 4 <p> 5 <label for="username" class="label">用戶名:</label> 6 <input id="username" name="username" type="text" class="input" /> 7 <span>(必填,3-15字符長度,支持漢字、字母、數字及_)</span> 8 <p/> 9 <p> 10 <label for="password" class="label">密 碼:</label> 11 <input id="password" name="password" type="password" class="input" /> 12 <span>(必填,不得少於6位)</span> 13 <p/> 14 <p> 15 <label for="repass" class="label">重復密碼:</label> 16 <input id="repass" name="repass" type="password" class="input" /> 17 <p/> 18 <p> 19 <label for="email" class="label">電子郵箱:</label> 20 <input id="email" name="email" type="text" class="input" /> 21 <span>(必填)</span> 22 <p/> 23 <p> 24 <input type="submit" name="submit" value=" 提交注冊 " class="left" /> 25 </p> 26 </form> 27 </fieldset>
javascript 檢測代碼
1 <script language=JavaScript> 2 <!-- 3 4 function InputCheck(RegForm) 5 { 6 if (RegForm.username.value == "") 7 { 8 alert("用戶名不可為空!"); 9 RegForm.username.focus(); 10 return (false); 11 } 12 if (RegForm.password.value == "") 13 { 14 alert("必須設定登錄密碼!"); 15 RegForm.password.focus(); 16 return (false); 17 } 18 if (RegForm.repass.value != RegForm.password.value) 19 { 20 alert("兩次密碼不一致!"); 21 RegForm.repass.focus(); 22 return (false); 23 } 24 if (RegForm.email.value == "") 25 { 26 alert("電子郵箱不可為空!"); 27 RegForm.email.focus(); 28 return (false); 29 } 30 } 31 32 //--> 33 </script>
CSS 樣式
1 <style type="text/css"> 2 html{font-size:12px;} 3 fieldset{width:520px; margin: 0 auto;} 4 legend{font-weight:bold; font-size:14px;} 5 label{float:left; width:70px; margin-left:10px;} 6 .left{margin-left:80px;} 7 .input{width:150px;} 8 span{color: #666666;} 9 </style>
注冊表單效果圖:
數據庫連接
1 <?php 2 $conn = @mysql_connect("localhost","root","root123"); 3 if (!$conn){ 4 die("連接數據庫失敗:" . mysql_error()); 5 } 6 mysql_select_db("test", $conn); 7 //字符轉換,讀庫 8 mysql_query("set character set 'gbk'"); 9 //寫庫 10 mysql_query("set names 'gbk'"); 11 ?>
注冊處理
reg.php 負責處理用戶注冊信息。
注冊檢測
1 if(!isset($_POST['submit'])){ 2 exit('非法訪問!'); 3 } 4 $username = $_POST['username']; 5 $password = $_POST['password']; 6 $email = $_POST['email']; 7 //注冊信息判斷 8 if(!preg_match('/^[\w\x80-\xff]{3,15}$/', $username)){ 9 exit('錯誤:用戶名不符合規定。<a href="javascript:history.back(-1);">返回</a>'); 10 } 11 if(strlen($password) < 6){ 12 exit('錯誤:密碼長度不符合規定。<a href="javascript:history.back(-1);">返回</a>'); 13 } 14 if(!preg_match('/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/', $email)){ 15 exit('錯誤:電子郵箱格式錯誤。<a href="javascript:history.back(-1);">返回</a>'); 16 }
本段代碼首先檢測是否 POST 提交訪問該頁,接下來根據注冊要求(用戶名 3-15 字符長度,支持漢字、字母、數字及_;密碼不得少於 6 位)對用戶提交的注冊信息進行檢測。在檢測用戶名和電子郵箱時采用了正則檢測。
數據庫交互
1 //包含數據庫連接文件 2 include('conn.php'); 3 //檢測用戶名是否已經存在 4 $check_query = mysql_query("select uid from user where username='$username' limit 1"); 5 if(mysql_fetch_array($check_query)){ 6 echo '錯誤:用戶名 ',$username,' 已存在。<a href="javascript:history.back(-1);">返回</a>'; 7 exit; 8 } 9 //寫入數據 10 $password = MD5($password); 11 $regdate = time(); 12 $sql = "INSERT INTO user(username,password,email,regdate)VALUES('$username','$password','$email', 13 $regdate)"; 14 if(mysql_query($sql,$conn)){ 15 exit('用戶注冊成功!點擊此處 <a href="login.html">登錄</a>'); 16 } else { 17 echo '抱歉!添加數據失敗:',mysql_error(),'<br />'; 18 echo '點擊此處 <a href="javascript:history.back(-1);">返回</a> 重試'; 19 }
該段代碼首先檢測用戶名是否已經存在,如果存在則輸出提示信息並立即終止程序執行。如果用戶名不存在則把注冊信息寫入數據庫,並輸出對應提示信息。