登录  /  注册

PHP如何发送邮件来进行用户验证

*文
发布: 2017-12-23 13:32:17
原创
4651人浏览过

网站有时会需要用到邮箱验证来防止用户恶意注册、验证身份等操作。可是如何使用php后端来发送验证邮件呢?本文就以一套注册实例来讲解php是如何去发送邮件的。

在用户注册中*常见的安全验证之一就是邮箱验证。根据行业的一般做法,进行邮箱验证是避免潜在的安全隐患一种非常重要的做法,现在就让我们来讨论一下这些*佳实践,来看看如何在PHP中创建一个邮箱验证。

让我们先从一个注册表单开始:

<form method="post" action="http://mydomain.com/registration/">
    <fieldset>
        <label for="fname">First Name:</label>
        <input type="text" name="fname" required />
    </fieldset>
    <fieldset>
        <label for="lname">Last Name:</label>
        <input type="text" name="lname" required />
    </fieldset>
    <fieldset>
        <label for="email">Last name:</label>
        <input type="email" name="email" required />
    </fieldset>
    <fieldset>
        <label for="password">Password:</label>
        <input type="password" name="password" required />
    </fieldset>
    <fieldset>
        <label for="cpassword">Confirm Password:</label>
        <input type="password" name="cpassword" required />
    </fieldset>
    <fieldset>
        <button type="submit">Register</button>
    </fieldset>
</form>
登录后复制

接下来是数据库的表结构:

CREATE TABLE IF NOT EXISTS `user` (
    `id` INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `fname` VARCHAR(255) ,
    `lname` VARCHAR(255) ,
    `email` VARCHAR(50) ,
    `password` VARCHAR(50) ,
    `is_active` INT(1) DEFAULT &#39;0&#39;,
    `verify_token` VARCHAR(255) ,
    `created_at` TIMESTAMP,
    `updated_at` TIMESTAMP,
);
登录后复制

一旦这个表单被提交了,我们就需要验证用户的输入并且创建一个新用户:

// Validation rules
$rules = array(
    &#39;fname&#39; => &#39;required|max:255&#39;,
    &#39;lname&#39; => &#39;required|max:255&#39;,
    &#39;email&#39; => &#39;required&#39;,
    &#39;password&#39; => &#39;required|min:6|max:20&#39;,
    &#39;cpassword&#39; => &#39;same:password&#39;
);
$validator = Validator::make(Input::all(), $rules);
// If input not valid, go back to registration page
if($validator->fails()) {
    return Redirect::to(&#39;registration&#39;)->with(&#39;error&#39;, $validator->messages()->first())->withInput();
}
$user = new User();
$user->fname = Input::get(&#39;fname&#39;);
$user->lname = Input::get(&#39;lname&#39;);
$user->password = Input::get(&#39;password&#39;);
// You will generate the verification code here and save it to the database
// Save user to the database
if(!$user->save()) {
    // If unable to write to database for any reason, show the error
    return Redirect::to(&#39;registration&#39;)->with(&#39;error&#39;, &#39;Unable to write to database at this time. Please try again later.&#39;)->withInput();
}
// User is created and saved to database
// Verification e-mail will be sent here
// Go back to registration page and show the success message
return Redirect::to(&#39;registration&#39;)->with(&#39;success&#39;, &#39;You have successfully created an account. The verification link has been sent to e-mail address you have provided. Please click on that link to activate your account.&#39;);
登录后复制

注册之后,用户的账户仍然是无效的直到用户的邮箱被验证。此功能确认用户是输入电子邮件地址的所有者,并有助于防止垃圾邮件以及未经授权的电子邮件使用和信息泄露。

整个流程是非常简单的——当一个新用户被创建时,在注册过过程中,一封包含验证链接的邮件便会被发送到用户填写的邮箱地址中。在用户点击邮箱验证链接和确认邮箱地址之前,用户是不能进行登录和使用网站应用的。

关于验证的链接有几件事情是需要注意的。验证的链接需要包含一个随机生成的token,这个token应该足够长并且只在一段时间段内是有效的,这样做的方法是为了防止网络攻击。同时,邮箱验证中也需要包含用户的唯一标识,这样就可以避免那些攻击多用户的潜在危险。

现在让我们来看看在实践中如何生成一个验证链接:

// We will generate a random 32 alphanumeric string
// It is almost impossible to brute-force this key space
$code = str_random(32);
$user->confirmation_code = $code;
登录后复制

一旦这个验证被创建就把他存储到数据库中,发送给用户:

Mail::send(&#39;emails.email-confirmation&#39;, array(&#39;code&#39; => $code, &#39;id&#39; => $user->id), function($message)
{
$message->from(&#39;my@domain.com&#39;, &#39;Mydomain.com&#39;)->to($user->email, $user->fname . &#39; &#39; . $user->lname)->subject(&#39;Mydomain.com: E-mail confirmation&#39;);
});
登录后复制

邮箱验证的内容:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
    </head>
    <body>
        <p style="margin:0">
            Please confirm your e-mail address by clicking the following link:
            <a href="http://mydomain.com/verify?code=<?php echo $code; ?>&user=<?php echo $id; ?>"></a>
        </p>
    </body>
</html>
登录后复制

现在让我们来验证一下它是否可行:

$user = User::where(&#39;id&#39;, &#39;=&#39;, Input::get(&#39;user&#39;))
            ->where(&#39;is_active&#39;, &#39;=&#39;, 0)
            ->where(&#39;verify_token&#39;, &#39;=&#39;, Input::get(&#39;code&#39;))
            ->where(&#39;created_at&#39;, &#39;>=&#39;, time() - (86400 * 2))
            ->first();
if($user) {
    $user->verify_token = null;
    $user->is_active = 1;
    if(!$user->save()) {
        // If unable to write to database for any reason, show the error
        return Redirect::to(&#39;verify&#39;)->with(&#39;error&#39;, &#39;Unable to connect to database at this time. Please try again later.&#39;);
    }
    // Show the success message
    return Redirect::to(&#39;verify&#39;)->with(&#39;success&#39;, &#39;You account is now active. Thank you.&#39;);
}
// Code not valid, show error message
return Redirect::to(&#39;verify&#39;)->with(&#39;error&#39;, &#39;Verification code not valid.&#39;);
登录后复制

结论:

上面展示的代码只是一个教程示例,并且没有通过足够的测试。在你的web应用中使用的时候请先测试一下。上面的代码是在Laravel框架中完成的,但是你可以很轻松的把它迁移到其他的PHP框架中。同时,验证链接的有效时间为48小时,之后就过期。引入一个工作队列就可以很好的及时处理那些已经过期的验证链接。

相关推荐:

php完整验证码代码 php 生成验证码 php 短信验证码 php验证码代

php 短信接口的示例代码(入门)

php 短信网关短信内容不能有空格,短信网关短信内容_PHP教程

以上就是PHP如何发送邮件来进行用户验证的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号