美文网首页
PDO实例-发送邮件激活账户

PDO实例-发送邮件激活账户

作者: EwanRenton | 来源:发表于2018-07-26 16:30 被阅读0次

    layout: post
    title: "PDO实例-发送邮件激活账户"
    date: 2016-05-22 16:07:33 +0800
    comments: true
    categories: [php]


    实现注册发送邮件激活账户

    使用swiftmailer-master发送邮件

    核心代码:

    <?php 
    header('content-type:text/html;charset=utf-8');
    //1.包含所需文件
    require_once 'swiftmailer-master/lib/swift_required.php';
    require_once 'PdoMySQL.class.php';
    require_once 'config.php';
    require_once 'pwd.php';
    //2.接收信息
    $act=$_GET['act'];
    $username=addslashes($_POST['username']);
    $password=md5($_POST['password']);
    $email=$_POST['email'];
    $table='user';
    //3.得到连接对象
    $PdoMySQL=new PdoMySQL();
    if($act==='reg'){
        $regtime=time();
        //完成注册的功能
        $token=md5($username.$password.$regtime);
        $token_exptime=$regtime+24*3600;//过期时间
        $data=compact('username','password','email','token','token_exptime','regtime');
        $res=$PdoMySQL->add($data, $table);
        $lastInsertId=$PdoMySQL->getLastInsertId();
        if($res){
            //发送邮件,以QQ邮箱为例
            //配置邮件服务器,得到传输对象
            $transport=Swift_SmtpTransport::newInstance('smtp.qq.com',25);
            //设置登陆帐号和密码
            $transport->setUsername('xxxxxxxxx@qq.com');
            $transport->setPassword($emailPassword);
            //得到发送邮件对象Swift_Mailer对象
            $mailer=Swift_Mailer::newInstance($transport);
            //得到邮件信息对象
            $message=Swift_Message::newInstance();
            //设置管理员的信息
            $message->setFrom(array('xxxxxxxxx@qq.com'=>'Reton'));
            //将邮件发给谁
            $message->setTo(array($email=>'reton'));
            //设置邮件主题
            $message->setSubject('激活邮件');
            $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?act=active&token={$token}";
            $urlencode=urlencode($url);
            $str=<<<EOF
            亲爱的{$username}您好~!感谢您注册我们网站<br/>
            请点击此链接激活帐号即可登陆!<br/>
            <a href="{$url}">{$urlencode}</a>
            <br/>
            如果点此链接无反映,可以将其复制到浏览器中来执行,链接的有效时间为24小时。      
    EOF;
            $message->setBody("{$str}",'text/html','utf-8');
            try{
                if($mailer->send($message)){
                    echo "恭喜您{$username}注册成功,请到邮箱激活之后登陆<br/>";
                    echo '3秒钟后跳转到登陆页面';
                    echo '<meta http-equiv="refresh" content="3;url=index.php#tologin"/>';
                }else{
                    $PdoMySQL->delete($table,'id='.$lastInsertId);
                    echo '注册失败,请重新注册';
                    echo '3秒钟后跳转到注册页面';
                    echo '<meta http-equiv="refresh" content="3;url=index.php#toregister"/>';
                }
            }catch(Swift_ConnectionException $e){
                echo '邮件发送错误'.$e->getMessage();
            }
        }else{
            echo '用户注册失败,3秒钟后跳转到注册页面';
            echo '<meta http-equiv="refresh" content="3;url=index.php#toregister"/>';
        }
    }elseif($act==='login'){
        //完成登陆的功能
        $row=$PdoMySQL->find($table,"username='{$username}' AND password='{$password}'",'status');
        if($row['status']==0){
            echo '请先激活在登陆';
            echo '<meta http-equiv="refresh" content="3;url=index.php#tologin"/>';
        }else{
            echo '登陆成功,3秒钟后跳转到首页';
            echo '<meta http-equiv="refresh" content="3;url=http://www.imooc.com"/>';
        }
        
    }elseif($act==='active'){
        $token=addslashes($_GET['token']);
        $row=$PdoMySQL->find($table,"token='{$token}' AND status=0",array('id','token_exptime'));
        $now=time();
        if($now>$row['token_exptime']){
            echo '激活时间过期,请重新登陆激活';
        }else{
            $res=$PdoMySQL->update(array('status'=>1),$table,'id='.$row['id']);
            if($res){
                echo '激活成功,3秒钟后跳转到登陆页面';
                echo '<meta http-equiv="refresh" content="3;url=index.php#tologin"/>';
            }else{
                echo '激活失败,请重新激活';
                echo '<meta http-equiv="refresh" content="3;url=index.php"/>';
            }
        }
        
    }
    
    <!DOCTYPE html>
    <!--[if lt IE 7 ]> <html lang="en" class="no-js ie6 lt8"> <![endif]-->
    <!--[if IE 7 ]>    <html lang="en" class="no-js ie7 lt8"> <![endif]-->
    <!--[if IE 8 ]>    <html lang="en" class="no-js ie8 lt8"> <![endif]-->
    <!--[if IE 9 ]>    <html lang="en" class="no-js ie9"> <![endif]-->
    <!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->
        <head>
            <meta charset="UTF-8" />
            <!-- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">  -->
            <title>Login and Registration Form with HTML5 and CSS3</title>
            <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
            <meta name="description" content="Login and Registration Form with HTML5 and CSS3" />
            <meta name="keywords" content="html5, css3, form, switch, animation, :target, pseudo-class" />
            <meta name="author" content="Codrops" />
            <link rel="shortcut icon" href="../favicon.ico"> 
            <link rel="stylesheet" type="text/css" href="css/demo.css" />
            <link rel="stylesheet" type="text/css" href="css/style.css" />
            <link rel="stylesheet" type="text/css" href="css/animate-custom.css" />
        </head>
        <body>
            <div class="container">
                <header>
                    <h1>It's Reton<span>This Is Reton</span></h1>
                    <nav class="codrops-demos">
                        <span>Click <strong>"Join us"</strong> to Register the reton</span>
                    </nav>
                </header>
                <section>               
                    <div id="container_demo" >
                        <a class="hiddenanchor" id="toregister"></a>
                        <a class="hiddenanchor" id="tologin"></a>
                        <div id="wrapper">
                            <div id="login" class="animate form">
                                <form  action="doAction.php?act=login" autocomplete="on" method="post"> 
                                    <h1>Log in</h1> 
                                    <p> 
                                        <label for="username" class="uname" data-icon="u" > Your username </label>
                                        <input id="username" name="username" required="required" type="text" placeholder="My Username"/>
                                    </p>
                                    <p> 
                                        <label for="password" class="youpasswd" data-icon="p"> Your password </label>
                                        <input id="password" name="password" required="required" type="password" placeholder="*****" /> 
                                    </p>
                                    <p class="keeplogin"> 
                                        <input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" /> 
                                        <label for="loginkeeping">Keep me logged in</label>
                                    </p>
                                    <p class="login button"> 
                                        <input type="submit" value="Login" /> 
                                    </p>
                                    <p class="change_link">
                                        Not a member yet ?
                                        <a href="#toregister" class="to_register">Join us</a>
                                    </p>
                                </form>
                            </div>
    
                            <div id="register" class="animate form">
                                <form  action="doAction.php?act=reg" autocomplete="on" method="post"> 
                                    <h1> Sign up </h1> 
                                    <p> 
                                        <label for="usernamesignup" class="uname" data-icon="u">Your username</label>
                                        <input id="usernamesignup" name="username" required="required" type="text" placeholder="Mr.King" />
                                    </p>
                                    <p> 
                                        <label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
                                        <input id="emailsignup" name="email" required="required" type="email" placeholder="xxxx@mail.com"/> 
                                    </p>
                                    <p> 
                                        <label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label>
                                        <input id="passwordsignup" name="password" required="required" type="password" placeholder="******"/>
                                    </p>
                                    <p class="signin button"> 
                                        <input type="submit" value="Sign up"/> 
                                    </p>
                                    <p class="change_link">  
                                        Already a member ?
                                        <a href="#tologin" class="to_register"> Go and log in </a>
                                    </p>
                                </form>
                            </div>
                            
                        </div>
                    </div>  
                </section>
            </div>
        </body>
    </html>
    

    相关文章

      网友评论

          本文标题:PDO实例-发送邮件激活账户

          本文链接:https://www.haomeiwen.com/subject/efibmftx.html