您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么用Php編寫注冊后Email激活驗證,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
總共需兩個頁面,register.php 和 verify.php
1. 用戶注冊表格 register.php
復制代碼 代碼如下:
<html>
<body>
<form action="register.php" method="post" name="register">
用戶名:<input type="text" name="username" />
密碼:<input type="password" name="password" />
電子郵件:<input type="text" name="email" />
<input type="submit" value="注冊" />
</form>
</body>
</html>
2. 創建用戶數據表格 Users
復制代碼 代碼如下:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL auto_increment,
`status` varchar(20) NOT NULL,
`username` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
`email` varchar(20) NOT NULL,
`activationkey` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `activationkey` (`activationkey`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
3. 創建驗證碼 用戶注冊信息存入數據表
我們使用狀態‘verify' 來表示尚未激活的用戶。
復制代碼 代碼如下:
$activationKey = mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
$username = mysql_real_escape_string($_POST[username]);
$password = mysql_real_escape_string($_POST[password]);
$email = mysql_real_escape_string($_POST[email]);
$sql="INSERT INTO users (username, password, email, activationkey, status) VALUES ('$username', '$password', '$email', '$activationKey', 'verify')";
4. 發送驗證碼
復制代碼 代碼如下:
echo "An email has been sent to $_POST[email] with an activation key. Please check your mail to complete registration.";
##Send activation Email
$to = $_POST[email];
$subject = " YOURWEBSITE.com Registration";
$message = "Welcome to our website!\r\rYou, or someone using your email address, has completed registration at YOURWEBSITE.com. You can complete registration by clicking the following link:\rhttp://www.YOURWEBSITE.com/verify.php?$activationKey\r\rIf this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards,\ YOURWEBSITE.com Team";
$headers = 'From: noreply@ YOURWEBSITE.com' . "\r\n" .
'Reply-To: noreply@ YOURWEBSITE.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
5. 驗證激活代碼 verify.php
如果驗證碼相同,則激活用戶。
復制代碼 代碼如下:
$queryString = $_SERVER['QUERY_STRING'];
$query = "SELECT * FROM users";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
if ($queryString == $row["activationkey"]){
echo "Congratulations!" . $row["username"] . " is now the proud new owner of a YOURWEBSITE.com account.";
$sql="UPDATE users SET activationkey = '', status='activated' WHERE (id = $row[id])";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
// 到這里,用戶已經完全激活了賬號,你可以將頁面跳轉到登陸后的界面了
}
} // end of while
關于“怎么用Php編寫注冊后Email激活驗證”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。