欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-19 13:29:16
总共需两个页面,register.php 和 verify.php 1. 用户注册表格 register.php 复制代码 代码如下: 

总共需两个页面,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