邮件激活注册实例

1.建立一个数据库连接文件conn.php

点击(此处)折叠或打开

  1. <?php
  2. $conn = mysql_connect('localhost','root','123456');
  3. mysql_select_db('email',$conn);
  4. mysql_query('set names utf8',$conn);
  5. ?>
2.建 一个注册账号

点击(此处)折叠或打开

  1. <?php
  2. /*create table user(
  3. uid int primary key auto_increment,
  4. uname char(32) not null default '',
  5. pass char(32) not null default '',
  6. email char(32) not null default '',
  7. status tinyint not null default 0
  8. )engine myisam charset utf8;

  9. create table activecode(
  10. cid int primary key auto_increment,
  11. uname char(32) not null default '',
  12. code char(16) not null default '',
  13. expire int not null default 0
  14. )engine myisam charset utf8;
  15. */
  16. /*模拟注册用户+创建激活码+发送激活码
  17. 思路:
  18. 1.连接数据库
  19. 2.insert用户表
  20. 3.随机生成激活码并insert activecode表
  21. 4.把生成的激活码发送到注册注册邮箱
  22. */
  23. require ('./conn.php');
  24. require ('./PHPMailer/class.phpmailer.php');
  25. $str = '1234567890abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ!@#$%';
  26. $uname = substr(str_shuffle(substr($str,0,52)),0,8);
  27. $email = '[email protected]';
  28. $sql = "insert into user(uname,email) values('$uname','$email');";
  29. mysql_query($sql,$conn);
  30. $code = substr(str_shuffle(substr($str,0,52)),0,8); //循环一个8位账号
  31. $expire = time()+24*3600;
  32. $sql = "insert into activecode(uname,code,expire)value('$uname','$code','$expire')";
  33. mysql_query($sql,$conn);

  34. //发送验证码
  35. $mail = new PHPMailer();
  36. $mail->IsSMTP();                    // 启用SMTP
  37. $mail->Host = "smtp.163.com";            //SMTP服务器
  38. $mail->SMTPAuth = true;                    //开启SMTP认证
  39. $mail->Username = "[email protected]";            // SMTP用户名
  40. $mail->Password = "xxxxx";                // SMTP密码
  41. $mail->From = "[email protected]";            //发件人地址
  42. $mail->FromName = "Mailer";                //发件人
  43. $mail->AddAddress("[email protected]", "Josh Adams");    //添加收件人
  44. $mail->Subject = "$uname,欢迎你,请激活账号";            //邮件主题
  45. $mail->Body = "请点击http://localhost/youjianjihuo/02.php?code=$code 来激活您的账号";        //邮件内容

  46. if(!$mail->Send()){
  47.     echo "error";
  48. }else{
  49.     echo "ok";
  50. }
  51. ?>
3.建立一个判断激活地址的文件

点击(此处)折叠或打开

  1. <?php
  2. /*
  3. 1.从地址栏获取激活码
  4. 2.查询该激活码
  5.     2.1激活码不存在
  6.     2.2激活码过期
  7.     2.3激活码正常,修改user的status状态为1
  8.     2.4把激活码的expire改成-1
  9. */
  10.     require ('./conn.php');
  11.     header("content-type:text/html; charset=utf-8");
  12. //从地址栏获取激活码
  13.     $code = $_GET['code'];
  14. //对激活码做出判断
  15.     if(strlen($code) !=8 ){
  16.         die('激活码错误aaa');
  17.     }
  18.     $sql = "select * from activecode where code = '$code'";
  19.     $rs = mysql_query($sql,$conn);
  20.     $row = mysql_fetch_assoc($rs); //取结果
  21.     if(empty($row)){
  22.         die('激活码错误');
  23.     }
  24.     if(time()>$row['expire']){
  25.         exit('激活码已过期,请重新申请');
  26.     }
  27. //激活用户
  28.     $sql = "update user set status=1 where uname ='$row[uname]' ";
  29.     mysql_query($sql,$conn);
  30.     echo "激活成功,请登录";
  31. //
  32.     $sql = "update activecode set expire=0 where code ='$code' ";
  33.     mysql_query($sql,$conn);
  34. ?>

你可能感兴趣的:(邮件激活注册实例)