Yii开发系列2:实现用户注册验证登录

实现简单的用户注册登录功能,基于Yii-basic,Yii-advance有现成的功能

安装Yii2-User

安装方式详见Yii_user主页,使用composer require dektrium/yii2-user安装。提示:

Package operations: 3 installs, 0 updates, 0 removals      
  - Installing yiisoft/yii2-httpclient (2.0.3): Downloading (100%)         
  - Installing yiisoft/yii2-authclient (2.1.2): Downloading (100%)         
  - Installing dektrium/yii2-user (0.9.12): Downloading (100%)         
Writing lock file
Generating autoload files

修改配置文件

Add following lines to your main configuration file:like this

'components' => [
        // other settings...
    ],
'modules' => [
        'user' => [
            'class' => 'dektrium\user\Module',
        ],
    ],

使用php 升级数据库

在当前网站文件的根目录执行:php yii migrate/up --migrationPath=@vendor/dektrium/yii2-user/migrations
提示:

Yii Migration Tool (based on Yii v2.0.11.2)

Total 12 new migrations to be applied:
    m140209_132017_init
    m140403_174025_create_account_table
    m140504_113157_update_tables
    m140504_130429_create_token_table
    m140830_171933_fix_ip_field
    m140830_172703_change_account_table_name
    m141222_110026_update_ip_field
    m141222_135246_alter_username_length
    m150614_103145_update_social_account_table
    m150623_212711_fix_username_notnull
    m151218_234654_add_timezone_to_profile
    m160929_103127_add_last_login_at_to_user_table

Apply the above migrations? (yes|no) [no]:yes

最后运行提示:

*** applied m160929_103127_add_last_login_at_to_user_table (time: 0.412s)


12 migrations were applied.

Migrated up successfully.

配置SwiftMailer

SwiftMailer:Swift Mailer 和前面介绍的PHPMailer一样,也是一个PHP邮件发送类。它不依赖于 PHP 自带的mail() 函数,因为该函数在发送多个邮件时占用的系统资源很高。Swift 直接与 SMTP 服务器通讯,具有非常高的发送速度和效率。
修改config\web.php中的mail配置文件,实例如下:

        //change by zc for yii-user mail
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@app/mailer',
            'useFileTransport' => false,
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.163.com',
                'username' => 'zhangchuanwork@163.com',
                'password' => '********',//**此处为授权码**
                'port' => '25',
                'encryption' => 'tls',
            ],
        ],

以上配置针对163邮箱,需在邮箱中设置SMTP,在password中设置授权码
此时,进入注册链接进行注册会提示Expected response code 250 but got code “553”, with message “553 Mail from must equal authorized user错误,在config下的params.php文件,把邮箱地址修改成和yii-user配置的邮箱一致即可。
经过以上配置注册后,提示:

Your account has been created and a message with further instructions has been sent to your email

并在注册邮箱中收到一封注册验证邮件。

测试注册成功

在浏览器中输入邮件中的链接:

Thank you, registration is now complete.

并在数据库中增加了一个user。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值