用戶註冊E-mail驗證

註冊表單頁signup.php:
 
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title >會員註冊 </title>
< style type ="text/css" >
body
{
background:url("imgs/bg.gif");
}
</style>
</head>

< body >
   < table width ="350" border ="0" align ="center" cellpadding ="0" cellspacing ="0" >
   < tr >
   < td > < form name ="form1" method ="post" action ="signup_ac.php" >
   < table width ="100%" border ="0" cellspacing ="4" cellpadding ="0" >
   < tr >
   < td colspan ="3" > < strong >註冊 </strong> </td>
   </tr>
   < tr >
   < td width ="76" >用戶名 </td>
   < td width ="3" >: </td>
   < td width ="305" > < input name ="name" type ="text" id ="name" size ="30" > </td>
   </tr>
   < tr >
   < td >E-mail </td>
   < td >: </td>
   < td > < input name ="email" type ="text" id ="email" size ="30" > </td>
   </tr>
   < tr >
   < td >密碼 </td>
   < td >: </td>
   < td > < input name ="password" type ="password" id ="password" size ="30" > </td>
   </tr>
   < tr >
   < td >國家 </td>
   < td >: </td>
   < td > < input name ="country" type ="text" id ="country" size ="30" > </td>
   </tr>
   < tr >
   < td >  </td>
   < td >  </td>
   < td > < input type ="submit" name ="Submit" value ="提交" >  
   < input type ="reset" name ="Reset" value ="重置" > </td>
   </tr>
   </table>
   </form> </td>
   </tr>
   </table>    
</body>
</html>
 
表單處理頁面signup_ac.php:
 
<?php
include('config.php');

// 表名
$tbl_name="tmp_members";//臨時表

//隨機確認碼
$confirm_code=md5(uniqid(rand()));

//表達傳送的值
$name=$_POST['name'];
$email=$_POST['email'];
$country=$_POST['country'];

//插入數據
$sql="INSERT INTO $tbl_name(confirm_code, name, email, password, country)VALUES('$confirm_code', '$name', '$email', '$password', '$country')";
$result=mysql_query($sql);

// 若是數據插入成功則發送驗證碼給指定E-mail
if($result){

// ---------------- 發送郵箱----------------

//發送給 ...
$to=$email;

// 主題
$subject="您的確認連接";

// 來自
$header="from:admin < HOHO@localhost >";

//你的消息
$message="你的確認連接 \r\n";
$message.="單擊此鏈接激活你的帳戶 \r\n";
$message.="http://localhost/ex/confirm.php?passkey=$confirm_code";

//發送郵件
$sentmail = mail($to,$subject,$message,$header);

}else {
// 若是沒找到
echo "數據庫中沒有此郵箱";
}

// 若是郵箱發送成功
if($sentmail){
echo "您的確認連接地址已經發送到您的E-mail中.";
}else {
echo "發送郵箱失敗";
}
?>
 
數據庫配置config.php:
 
<?php
$host="localhost"; // 主機名
$username="root"; // Mysql 用戶名
$password="123456"; // Mysql密碼
$db_name="verifyemail"; // 數據庫名

//鏈接服務器並選擇數據庫
mysql_connect("$host", "$username", "$password")or die("不能鏈接數據庫服務器");
mysql_select_db("$db_name")or die("不能選擇數據庫");
?>
 
 
確認confirm.php
 
<?php
include('config.php');

// 從鏈接地址獲得通行密鑰
$passkey=$_GET['passkey'];

$tbl_name1="tmp_members";

// 檢索數據尋找匹配密鑰的那條記錄
$sql1="SELECT * FROM $tbl_name1 WHERE confirm_code ='$passkey'";
$result1=mysql_query($sql1);

// 查詢成功
if($result1){

// 統計下多少行含有此密鑰
$count=mysql_num_rows($result1);

//若是找到則獲取數據
if($count==1){

$rows=mysql_fetch_array($result1);
$name=$rows['name'];
$email=$rows['email'];
$password=$rows['password'];
$country=$rows['country'];

$tbl_name2="reg_members";

// 把數據插入到註冊會員表中
$sql2="INSERT INTO $tbl_name2(name, email, password, country)VALUES('$name', '$email', '$password', '$country')";
$result2=mysql_query($sql2);
}
// 若是沒有找到則顯示確認碼不正確
else {
echo "錯誤的驗證碼";
}

// 插入成功的話提示用戶已經激活
if($result2){

echo "您的帳戶已經激活";

// 從臨時表中刪除記錄
$sql3="DELETE FROM $tbl_name1 WHERE confirm_code = '$passkey'";
$result3=mysql_query($sql3);
}
}
?>
 
 
 
 
相關文章
相關標籤/搜索