如何在php中几秒后禁用电子邮件确认链接

时间:2016-03-08 05:30:28

标签: php mysql

您好我在注册后发送确认电子邮件。几秒后链接如何过期,任何人都可以建议我。因为如果我在几天后点击链接也会被激活。这不应该发生这是我的代码:

<?php
session_start();
$sessionCaptcha = $_SESSION['vercode'];
$inputStream = file_get_contents("php://input");
$data = json_decode($inputStream);
$connection = mysql_connect("localhost", "enjoytax_account", "account") or die(mysql_error());
$db = mysql_select_db("enjoytax_accounting", $connection);
if($db)
{
$confirm_code=md5(uniqid(rand()));
$username = $data->username;
$email = $data->email;
$password = md5($data->password);
$confirmpassword = md5($data->confirmpassword);
$mobileno = $data->mobileno;
$captcha=$data->captcha;    
$check=mysql_query("select email from register where email = '$email'");
$num_rows = mysql_num_rows($check);
if ($num_rows == 0) 
{      
    if($captcha == $sessionCaptcha)
    {   
        $query = mysql_query("insert into register(username,email, password, repassword,mobile,confirm_code) values ('$username','$email', '$password' , '$confirmpassword', '$mobileno','$confirm_code')");

        if ($query)  
          {
              $from .= 'info@mail.com' . "\r\n\r\n";
              $to = $data->email;
              $subject="Your confirmation link here";
              $message.="Click on this link to activate your account \r\n";
              $message.="http://www.example.com/model/confirmation.php?email=$email&passkey=$confirm_code";     
              $success = mail($to, $subject, $message);         
                 $successJson='{"success":"We have sent a verification email ' .
                    'to your email id '.$email.', please check your ' .
                    'Inbox and verify your email in order to proceed further."}';
                print_r($successJson);          
          }else{
            $failureJson='{"error":"We are encountering some issue. Please try after some time."}';
            print_r($failureJson);  
        }
    }else{
    $failureJson='{"error":"Please Enter Correct Captcha."}';       
    print_r($failureJson);  
    }
}else{  
 $failureJson='{"error":"Email-Id already Exists."}';
            print_r($failureJson);
}

}       

?>

1 个答案:

答案 0 :(得分:0)

我没有测试过代码,但这可能对你有帮助。

 session_start();

      if ($query)
        {
            $from .= 'info@mail.com' . "\r\n\r\n";
            $to = $data->email;
            $subject="Your confirmation link here";
            $message.="Click on this link to activate your account \r\n";
            $message.="http://www.example.com/model/confirmation.php?email=$email&passkey=$confirm_code";
            $success = mail($to, $subject, $message);
            $successJson='{"success":"We have sent a verification email ' .
                'to your email id '.$email.', please check your ' .
                'Inbox and verify your email in order to proceed further."}';

            $_SESSION['now'] = date('i:s');

            $now =date('Y-m-d H:i:s');

            $futureDate = $now+(60*5);

            $formatDate = date("Y-m-d H:i:s", $futureDate);

            if($_SESSION['now'] > $formatDate)
            {
                $failureJson='{"error":"We are encountering some issue. Please try after some time."}';
                 print_r($failureJson);       

            }

        }
        else
        {

            echo " Query Not Executed";
        }