久久久久久AV无码免费看大片,亚洲一区精品人人爽人人躁,国产成人片无码免费爱线观看,亚洲AV成人无码精品网站,为什么晚上搞的时候要盖被子

php實(shí)現(xiàn)qq發(fā)送驗(yàn)證郵箱

時(shí)間:2017-06-02 11:37:15 類(lèi)型:PHP
字號(hào):    

    我們經(jīng)常在一些電商平臺(tái)上注冊(cè)的時(shí)候,都會(huì)需要進(jìn)行郵箱驗(yàn)證,對(duì)于php程序開(kāi)發(fā)工程師需要弄明白其中的實(shí)現(xiàn)原理和流程

1,先去qq郵箱中的 點(diǎn)擊設(shè)置-》點(diǎn)擊帳戶-》將smtp的權(quán)限開(kāi)啟 如圖: 


如果是設(shè)置POP3和SMTP的SSL加密方式,則端口如下: 
POP3服務(wù)器(端口995) 
SMTP服務(wù)器(端口465或587)。 
2、查看你的Openssl和Socketsd是否支持:php -m查看 

利用OpenSSL庫(kù)對(duì)Socket傳輸進(jìn)行安全加密。 
3、表單部分 
<form id="reg" action="register.php" method="post"> 
<p>用戶名:<input type="text" class="input" name="username" id="user"></p> 
<p>密 碼:<input type="password" class="input" name="password" id="pass"></p> 
<p>E-mail:<input type="text" class="input" name="email" id="email"></p> 
<p><input type="submit" class="btn" value="提交注冊(cè)"></p> 
</form> 

4、表單提交部分

 <!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>PHP用戶注冊(cè)郵箱驗(yàn)證激活帳號(hào)</title>
<style type="text/css">
.demo {margin: 20px auto; width: 400px; border: 1px solid #ccc; line-height: 50px;text-align: center;}
.input {width: 150px; height: 25px; border: 1px solid #ccc;}
.btn {padding: 5px 15px; font-size: 16px; font-family: '微軟雅黑'; background:#ff0066; color: #fff; border: none;}
</style>
<script type="text/javascript">
function chk_form(){
var user = document.getElementById("user");
if(user.value==""){
alert("用戶名不能為空!");
return false;
//user.focus();
}
var pass = document.getElementById("pass");
if(pass.value==""){
alert("密碼不能為空!");
return false;
//pass.focus();
}
var email = document.getElementById("email");
if(email.value==""){
alert("Email不能為空!");
return false;
//email.focus();
}
var preg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; //匹配Email
if(!preg.test(email.value)){ 
alert("Email格式錯(cuò)誤!");
return false;
//email.focus();
}
}
</script>
</head>
<body>
<div id="main">
<div class="demo">
<form id="reg" action="register.php" method="post" onsubmit="return chk_form();">
用戶名:<input type="text" class="input" name="username" id="user"><br>
密 碼:<input type="password" class="input" name="password" id="pass"><br>
郵 箱:<input type="text" class="input" name="email" id="email"><br>
<input type="submit" class="btn" value="提交注冊(cè)">
</form>
</div>
</div>
</body>

<html> 

5,如果注冊(cè)成功,發(fā)送郵箱驗(yàn)證碼,這里負(fù)責(zé)發(fā)送。 

register.php部分
<?php
include_once("connect.php");//連接數(shù)據(jù)庫(kù) 
// include_once("smtp.class.php");//郵件發(fā)送類(lèi) 


$username = stripslashes(trim($_POST['username'])); 
$sql ="select id from t_user where username='{$username}'";
// $query = mysqli_query("'");
$res  = mysqli_query($link, $sql); 

    if($res && mysqli_num_rows($res)>0){

            echo '用戶名已存在,請(qǐng)換個(gè)其他的用戶名'; 
            exit; 
    }


//構(gòu)造激活碼
$password = md5(trim($_POST['password'])); //加密密碼 
$email = trim($_POST['email']); //郵箱 
$regtime = time(); 

$token = md5($username.$password.$regtime); //創(chuàng)建用于激活識(shí)別碼 
$token_exptime = time()+60*60*24;//過(guò)期時(shí)間為24小時(shí)后 

$sql = "insert into `t_user` (`username`,`password`,`email`,`token`,`token_exptime`,`regtime`)  
values ('$username','$password','$email','$token','$token_exptime','$regtime')"; 
 // echo $sql;
$res1 = mysqli_query($link, $sql); 


if(mysqli_insert_id($link)){ 
    //郵件發(fā)送
    require 'class.phpmailer.php';
    require 'class.smtp.php';

    $mail = new PHPMailer;
    //$mail->SMTPDebug = 3; // Enable verbose debug output
    $mail->isSMTP(); // Set mailer to use SMTP
    $mail->Host = 'smtp.qq.com'; // Specify main and backup SMTP servers
    $mail->SMTPAuth = true; // Enable SMTP authentication
    $mail->Username = '1391241655@qq.com'; // SMTP username
    $mail->Password = 'dzrxckopdnxuhjhf'; // SMTP password
    $mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465; // TCP port to connect to
    $mail->setFrom('1391241655@qq.com', '發(fā)件人');

    $email = $_POST['email'];
    $mail->addAddress($email, '.'); // Add a recipient
    // Name is optional
    $mail->addReplyTo($email, 'php');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');

    $mail->isHTML(true); // Set email format to HTML
    $mail->Subject = '標(biāo)題';
    $mail->Body = "發(fā)送的內(nèi)容";
    // $mail->AltBody = '發(fā)送的內(nèi)容22';
    if(!$mail->send()) {
    //輸出錯(cuò)誤信息
    echo 'Mailer Error: ' . $mail->ErrorInfo;
    return false;
    } else {
    echo 'Message has been sent'; //成功輸出
    return true;
    }
}
    ?>

6、驗(yàn)證部分

include_once("connect.php");//連接數(shù)據(jù)庫(kù) 

$verify = stripslashes(trim($_GET['verify'])); 
$nowtime = time(); 
 $sql = "select id,token_exptime from t_user where status='0' and  `token`='$verify'";
$res= mysql_query($link, $sql); 
$row = mysql_fetch_array($res); 
if($row){ 
    if($nowtime>$row['token_exptime']){ //24hour 
        $msg = '您的激活有效期已過(guò),請(qǐng)登錄您的帳號(hào)重新發(fā)送激活郵件.'; 
    }else{ 
    $sql1 ="update t_user set status=1 where id=".$row['id']";
    $res1 =  mysqli_query($link, $sql1); 
        if(mysqli_affected_rows($link)!=1) die(0); 
        $msg = '激活成功!'; 
    } 
}else{ 
    $msg = 'error.';     

echo $msg;