Codeigniter4基础教程(11)--注册登陆案例(1)0.创建users的数据表
CREATE TABLE`users`(
`u_id`int(11)NOT NULL AUTO_INCREMENT,
`u_name`varchar(255)DEFAULT NULL,
`u_email`varchar(255)DEFAULT NULL,
`u_password`varchar(255)DEFAULT NULL,
`u_link`varchar(255)DEFAULT NULL,
`u_date`datetime NOT NULL,
`u_updated`datetime NOT NULL,
PRIMARY KEY(`u_id`)
)ENGINE=MyISAM AUTO_INCREMENT=2DEFAULT CHARSET=utf8
1.新建model,操作数据
app/Models/ModUsers.php
<?php
namespace App\Models;
use CodeIgniter\Model;
class ModUsers extends Model
{
protected$DBGroup='default';
protected$table='users';
protected$primaryKey='u_id';
protected$returnType='array';
protected$useTimestamps=true;
protected$allowedFields=['u_name','u_email','u_password','u_link'];
protected$createdField='u_date';
protected$updatedField='u_updated';
}
2.修改controller
app/Controllers/User.php
<?php namespace App\Controllers;
use App\Models\ModUsers;
class User extends BaseController{
public function index(){
echo'I am fine';
}
public function register(){
// echo '注册开始';
helper('form');
return view('signup');
怎么注册gmail邮箱}
public function newuser(){
$myvalues=$this->validate([
'name'=>'required',
'email'=>'required',
'password'=>'required',
]);
if(!$myvalues){//没有通过条件检验
return$this->register();
}else{//通过条件检验后,取值
$myrequest= \Config\Services::request();
$users=new ModUsers();
// echo $myrequest->getVar('name');
// echo $myrequest->getVar('email');
// echo $myrequest->getVar('password');
helper('text');
$data['u_name']=$myrequest->getVar('name');
$data['u_email']=$myrequest->getVar('email');
$data['u_password']=$myrequest->getVar('password');
$data['u_password']=hash('md5',$data['u_password']);
$data['u_link']=random_string('alnum',20);
$myNewuser=$users->insert($data);
if($myNewuser){
echo'make it to insert';
}else{
echo'insertion fail';
}
}
}
}
3. 发邮件测试
PHP的邮件相关的内容与服务器有所联系,⽐如,SMTP的设置在云平台上和⼀般服务器上有所差异。鉴于操作上有点⿇烦,⽽且不同的邮件服务器对于权限有不同的规定,PHP的邮件功能要么是测试⽤,要么是内部⽤,对外开放⽤的其实不多。⽐如,gmail的有需要对账号的安全等级进⾏设置,有个“Less secure app access”需要激活。这⾥就略过⼀些内容。
app/Config/emial.php,等
public$protocol='smtp';
/**
* SMTP Server Address
*
* @var string
*/
public$SMTPHost='ail';
/**
* SMTP Username
*
* @var string
*/
public$SMTPUser='我的gmail邮箱';
public$SMTPPass='我的gmail邮箱密码';
这⼀块,我测试过,可以发到我的126邮箱⾥。app/controllers/user.php
<?php namespace App\Controllers;
use App\Models\ModUsers;
class User extends BaseController{
public function index(){
echo'I am fine';
}
public function register(){
// echo '注册开始';
helper('form');
return view('signup');
}
public function newuser(){
$myvalues=$this->validate([
'name'=>'required',
'email'=>'required',
'password'=>'required',
]);
if(!$myvalues){//没有通过条件检验
return$this->register();
}else{//通过条件检验后,取值
$myrequest= \Config\Services::request();
$users=new ModUsers();
// echo $myrequest->getVar('name');
// echo $myrequest->getVar('email');
// echo $myrequest->getVar('password');
helper('text');
$data['u_name']=$myrequest->getVar('name');
$data['u_email']=$myrequest->getVar('email');
$data['u_password']=$myrequest->getVar('password');
$data['u_password']=hash('md5',$data['u_password']);
$data['u_link']=random_string('alnum',20);
$myNewuser=$users->insert($data);
if($myNewuser){
echo'make it to insert';
}else{
echo'insertion fail';
}
$email= \Config\Services::email();
$email->setFrom('我的gmail邮箱','Activate the account');
$email->setTo($data['u_email']);
$email->setSubject('Activiate your account');
$email->setMessage($message);
if($email->send())
{
echo'Email sent successfully';
}else{
echo'Fail to send email '.$email->printDebugger(['headers']);
}
}
}
}
测试结果如下
4.进⼀步,随机⽣成注册链接
常见的注册过程中,服务器往往会发⼀封带有链接的地址到⽤户提供邮箱⾥,这⾥就⽣成⼀个随机地址,插⼊到邮件⾥,进⾏演⽰。先添加⼀列,⽤来标注是否允许发送邮件。
ALTER TABLE`users`ADD`u_status`INT(5)NOT NULL DEFAULT'0'AFTER`u_updated`;
app/Controllers/User.php
<?php namespace App\Controllers;
use App\Models\ModUsers;
class User extends BaseController{
public function index(){
echo'I am fine';
}
public function register(){
// echo '注册开始';
helper('form');
return view('signup');
}
public function newuser(){
$myvalues=$this->validate([
'name'=>'required',
'email'=>'required',
'password'=>'required',
]);
if(!$myvalues){//没有通过条件检验
return$this->register();
}else{//通过条件检验后,取值
$myrequest= \Config\Services::request();
$users=new ModUsers();
// echo $myrequest->getVar('name');
// echo $myrequest->getVar('email');
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论