laravel怎么加入验证码功能完整版
时间:2022年03月28日
/来源:网络
/编辑:佚名
78模板网亲测可以用的laravel怎么加入验证码功能
1.我们在项目的根目录可以找到composer.json这个文件,加上下边的代码
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*",
"gregwar/captcha": "1.*"
},
2.进入项目根目录进行composer update
3.设置路由
// 验证码的路由
Route::get('captcha','Index\RegisterController@code');
4.在控制器输出验证码,引用类
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
use Illuminate\Support\Facades\Session;
/*
author:78模板网
web:www.78moban.com
*/
// 生成验证码
public function code()
{
$builder = new CaptchaBuilder();
$builder->build(150,32);
$phrase = $builder->getPhrase();
//把内容存入session
Session::flash('milkcaptcha', $phrase); //存储验证码
ob_clean();
return response($builder->output())->header('Content-type','image/jpeg');
}
// 登陆验证验证码
public function login()
{
$username = Input::get('username', '');
$password = Input::get('password', '');
$captcha = Input::get('captcha', '');
$scaptcha = session()->get('milkcaptcha');
if (empty($username)) {
return $this->error('用户名必须填写');
}
if (empty($password)) {
return $this->error('密码必须填写');
}
if (empty($captcha)) {
return $this->error('验证码必须填写');
}
if (strtolower($scaptcha) !== strtolower($captcha)) {
return $this->error('验证码不正确');
}
$password = Users::MakePassword($password);
$admin = Admin::where('username', $username)->where('password', $password)->first();
if (empty($admin)) {
return $this->error('用户名密码错误');
} else {
$role = AdminRole::find($admin->role_id);
if (empty($role)) {
return $this->error('账号异常');
} else {
session()->put('admin_username', $admin->username);
session()->put('admin_id', $admin->id);
session()->put('admin_role_id', $admin->role_id);
session()->put('admin_is_super', $role->is_super);
session()->put('captcha', $captcha);
return $this->success('登陆成功');
}
}
}
5.在页面输出验证码
<img src="{
{url('captcha')}}" style="width: 130px;height: 60px;margin-left: 410px;position: relative;top: -74px;" onclick="this.src=this.src+'?'+Math.random()" class="code" >
6.点击验证码进行刷新,就是onclick事件
1.我们在项目的根目录可以找到composer.json这个文件,加上下边的代码
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*",
"gregwar/captcha": "1.*"
},
2.进入项目根目录进行composer update
3.设置路由
// 验证码的路由
Route::get('captcha','Index\RegisterController@code');
4.在控制器输出验证码,引用类
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
use Illuminate\Support\Facades\Session;
/*
author:78模板网
web:www.78moban.com
*/
// 生成验证码
public function code()
{
$builder = new CaptchaBuilder();
$builder->build(150,32);
$phrase = $builder->getPhrase();
//把内容存入session
Session::flash('milkcaptcha', $phrase); //存储验证码
ob_clean();
return response($builder->output())->header('Content-type','image/jpeg');
}
// 登陆验证验证码
public function login()
{
$username = Input::get('username', '');
$password = Input::get('password', '');
$captcha = Input::get('captcha', '');
$scaptcha = session()->get('milkcaptcha');
if (empty($username)) {
return $this->error('用户名必须填写');
}
if (empty($password)) {
return $this->error('密码必须填写');
}
if (empty($captcha)) {
return $this->error('验证码必须填写');
}
if (strtolower($scaptcha) !== strtolower($captcha)) {
return $this->error('验证码不正确');
}
$password = Users::MakePassword($password);
$admin = Admin::where('username', $username)->where('password', $password)->first();
if (empty($admin)) {
return $this->error('用户名密码错误');
} else {
$role = AdminRole::find($admin->role_id);
if (empty($role)) {
return $this->error('账号异常');
} else {
session()->put('admin_username', $admin->username);
session()->put('admin_id', $admin->id);
session()->put('admin_role_id', $admin->role_id);
session()->put('admin_is_super', $role->is_super);
session()->put('captcha', $captcha);
return $this->success('登陆成功');
}
}
}
5.在页面输出验证码
<img src="{
{url('captcha')}}" style="width: 130px;height: 60px;margin-left: 410px;position: relative;top: -74px;" onclick="this.src=this.src+'?'+Math.random()" class="code" >
6.点击验证码进行刷新,就是onclick事件
新闻资讯 更多