本文参考十八哥的视频
数据库:
create table user(uid int primary key auto_increment,uname char(20) not null default ‘ ‘,email char(32) not null default ‘ ‘,pass char(32) not null default ‘‘,status tinyint not null default 0)engine myisam charset utf8;
create table activecode(cid int primary key auto_increment,uname char(20) not null default ‘‘,code char(16) not null default ‘‘,expire int not null default 0)engine myisam charset utf8;
php
conn.php:数据库连接
<?php
$conn=mysql_connect(‘localhost‘,‘root‘,‘root‘);
mysql_query(‘use tempemail‘,$conn);
mysql_query(‘set names utf8‘,$conn);
?>
01.php:模拟注册用户,创建激活码,发送激活码
<?php
require(‘./conn.php‘);
require(‘./PHPMailer/class.phpmailer.php‘);
$str=‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456‘;
$uname=substr(str_shuffle(substr($str, 0,52)),0,8);//生成8位随机用户名
$email=‘[email protected]‘;
//模拟注册
$sql="insert into user(uname,email) values(‘$uname‘,‘$email‘)";
mysql_query($sql,$conn);
//生成激活码
$code=substr(str_shuffle(substr($str, 0,52)),0,8);
$expire=time()+5*24*3600;
$sql="insert into activecode(uname,code,expire) values(‘$uname‘,‘$code‘,‘$expire‘)";
mysql_query($sql,$conn);
//发送激活邮件
$phpmailer= new PHPMailer();
//选用smtp
$phpmailer->IsSMTP();
$phpmailer->Host=‘smtp.163.com‘;
$phpmailer->SMTPAuth=true;
$phpmailer->Username=‘php0620‘;
$phpmailer->Password=‘pwd‘;
//可以发信了
$phpmailer->From=‘[email protected]‘;
$phpmailer->FromName=‘fromname‘;
$phpmailer->Subject=$uname.‘欢迎注册‘;
$phpmailer->Body=‘请点击:http://localhost/02.php?code=‘.$code.‘ 进行激活‘;
//设置收件人
$phpmailer->AddAddress(‘[email protected]‘,‘first‘);
$phpmailer->AddCC(‘[email protected]‘,‘second‘);
//发信
echo $phpmailer->send()?‘ok‘:‘no‘;
?>
02.php
<?php
require(‘./conn.php‘);
$code=$_GET[‘code‘];
if(strlen($code)!=8){
exit(‘激活码错误‘);
}
$sql="select * from activecode where code=‘$code‘";
$rs=mysql_query($sql,$conn);
$row=mysql_fetch_assoc($rs);
if(empty($row)){
exit(‘激活码错误‘);
}
if(time()>$row[‘expire‘]){
exit(‘激活码过期‘);
}
//激活用户update
$sql="update user set status=1 where uname=‘$row[uname]‘";
mysql_query($sql,$conn);
//作废激活码
$sql="update activecode set expire=0 where code=‘$code‘";
mysql_query($sql,$conn);
?>