前面三篇文章我们已经成功将用户激活的相关数据写进了数据库里,并以邮件的方式给用户发送了一条带有激活数据的get链接。形如:http://xxxxx.com/jihuo.php?id=xx&token=xxxxxxxxxxx
当用户点击后,就会将数据提交到jihuo.php页面。所以,我们需要在jihuo.php页面中接收数据,并判断是否予以激活。
- 创建jihuo.php文件,位与网站根目录下。
- 接收传递过来的get数据。代码如下:
复制
if(isset($_GET[ 'token'])&&isset($_GET[ 'user_id'])) { $token=$_GET[ 'token']; $user_id=$_GET[ 'user_id']; }else{ echo "数据接收失败"; } - 获取服务器时间与数据库中对应用户的激活时间
- 计算时间是否超过指定时间差。详情见:代码狗——计算时间差
- 链接数据库,更新数据。
jihuo.php文件代码如下:
复制
<?php
if(isset($_GET[ 'token'])&&isset($_GET[ 'user_id']))
{
$token=$_GET[ 'token'];
$user_id=$_GET[ 'user_id'];
$con=mysql_connect("localhost", "root", "800820");
if (!$con) {
die("数据库连接失败: " . mysql_error());
}else{
mysql_select_db("wptest",$con);
$sql="select * from wp_users where id='".$user_id."'";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
$cxtoken=$row['token'];
$cxtoken_time=$row['token_time'];
}
if($token==$cxtoken){
if(isouttime($cxtoken_time,gettime())<1){
$sql="UPDATE wp_users set status='1' where id='".$user_id."'";
mysql_query($sql);
echo "激活成功!";
}else{
echo "激活码已失效!";
}
}else{
echo "激活失败,激活码校验失败!";
}
}
}else{
echo "接收数据失败";
}
mysql_close($con);
function isouttime($firtime,$lastime){
$one = strtotime($firtime);//开始时间 时间戳
$tow = strtotime($lastime);//结束时间 时间戳
$cle = $tow - $one; //得出时间戳差值
$d = floor($cle/3600/24);
$h = floor(($cle%(3600*24))/3600); //%取余
$m = floor(($cle%(3600*24))%3600/60);
$s = floor(($cle%(3600*24))%60);
return $d;
}
function gettime(){
date_default_timezone_set("Asia/Hong_Kong");
$time=Date("Y-m-d H:i:s");
return $time;
}
?>实测效果图:








评论 (0)