知识屋:更实用的电脑技术知识网站
所在位置:首页 > 编程技术 > PHP编程

用Php编写注册后Email激活验证的实例代码

发布时间:2013-04-01 13:22:55作者:知识屋

总共需两个页面,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!rrYou, 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?$activationKeyrrIf this is an error, ignore this email and you will be removed from our mailing list.rrRegards, YOURWEBSITE.com Team";

$headers = 'From: noreply@ YOURWEBSITE.com' . "rn" .

'Reply-To: noreply@ YOURWEBSITE.com' . "rn" .

'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


(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜