用Php編寫注冊后Email激活驗(yàn)證的實(shí)例代碼
總共需兩個(gè)頁面,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. 創(chuàng)建用戶數(shù)據(jù)表格 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. 創(chuàng)建驗(yàn)證碼 用戶注冊信息存入數(shù)據(jù)表
我們使用狀態(tài)‘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. 發(fā)送驗(yàn)證碼
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. 驗(yàn)證激活代碼 verify.php
如果驗(yàn)證碼相同,則激活用戶。
$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());
}
// 到這里,用戶已經(jīng)完全激活了賬號,你可以將頁面跳轉(zhuǎn)到登陸后的界面了
}
} // end of while
- PHP+Ajax異步通訊實(shí)現(xiàn)用戶名郵箱驗(yàn)證是否已注冊( 2種方法實(shí)現(xiàn))
- php發(fā)送短信驗(yàn)證碼完成注冊功能
- 注冊頁面之前先驗(yàn)證用戶名是否存在的php代碼
- php用戶注冊頁面利用js進(jìn)行表單驗(yàn)證具體實(shí)例
- php用戶注冊信息驗(yàn)證正則表達(dá)式
- php自動(dòng)注冊登錄驗(yàn)證機(jī)制實(shí)現(xiàn)代碼
- PHP學(xué)習(xí)筆記 用戶注冊模塊用戶類以及驗(yàn)證碼類
- Thinkphp實(shí)現(xiàn)短信驗(yàn)證注冊功能
- php 注冊時(shí)輸入信息驗(yàn)證器的實(shí)現(xiàn)詳解
- php+ajax注冊實(shí)時(shí)驗(yàn)證功能
- js和php郵箱地址驗(yàn)證的實(shí)現(xiàn)方法
- PHP實(shí)現(xiàn)的激活用戶注冊驗(yàn)證郵箱功能示例
相關(guān)文章
PHPMAILER實(shí)現(xiàn)PHP發(fā)郵件功能
這篇文章主要為大家詳細(xì)介紹了PHPMAILER實(shí)現(xiàn)PHP發(fā)郵件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04thinkphp配置文件路徑的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猼hinkphp配置文件路徑的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08laravel框架關(guān)于搜索功能的實(shí)現(xiàn)
本文是作者整理的關(guān)于laravel框架搜索功能的實(shí)現(xiàn)原理,并附上了詳細(xì)代碼,有需要的小伙伴請持續(xù)關(guān)注!2018-03-03Yii框架實(shí)現(xiàn)多數(shù)據(jù)庫配置和操作的方法
這篇文章主要介紹了Yii框架實(shí)現(xiàn)多數(shù)據(jù)庫配置和操作的方法,結(jié)合實(shí)例形式分析了Yii框架多數(shù)據(jù)庫的配置技巧與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2017-05-05PHP 設(shè)計(jì)模式系列之 specification規(guī)格模式
規(guī)格模式是組合模式的一種擴(kuò)展,在框架性開發(fā)中使用較多(項(xiàng)目級開發(fā)很少使用),通過本文給大家介紹PHP 設(shè)計(jì)模式系列之 specification規(guī)格模式,對specification模式相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-01-01