使用 Laravel 4 实现“记住我”功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18551725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing "Remember Me" functionality using Laravel 4
提问by Rafael Adel
I'm new to Laravel, Trying to make a very simple login form.
我是 Laravel 的新手,正在尝试制作一个非常简单的登录表单。
This form has a "Remember Me" Check box. I tried to implement its functionality using Cookie::make()
but it turned out that i need to return a Response
in order to save it.
此表单有一个“记住我”复选框。我试图使用实现它的功能,Cookie::make()
但结果我需要返回 aResponse
才能保存它。
When i check cookies stored from localhost
in my browser, I don't find a cookie named username
. I've did some research and it turned out that i must attach the cookie into a Response
and then return it.
当我检查localhost
浏览器中存储的 cookie 时,我没有找到名为username
. 我做了一些研究,结果发现我必须将 cookie 附加到 a 中Response
,然后将其返回。
The problem is, I don't want to return a Response
!!
问题是,我不想退货Response
!!
I haven't reached Auth
class yet in my learning process. So a solution without using this class would be more suitable.
Auth
在我的学习过程中,我还没有上课。所以不使用这个类的解决方案会更合适。
Here's my code :
这是我的代码:
public function processForm(){
$data = Input::all();
if($data['username'] == "rafael" & $data['password'] == "123456"){
if(Input::has('rememberme')){
$cookie = Cookie::make('username', $data['username'], 20);
}
Session::put('username', $data['username']);
return Redirect::to('result');
} else {
$message_arr = array('message' => 'Invalid username or password!');
return View::make('signup', $message_arr);
}
}
My signup.blade.php
:
我的signup.blade.php
:
@extends('layout')
@section('content')
@if(isset($message))
<p>Invalid username or password.</p>
@endif
<form action="{{ URL::current() }}" method="post">
<input type="text" name="username"/>
<br>
<input type="text" name="password"/>
<br>
<input type="checkbox" name="rememberme" value="true"/>
<input type="submit" name="submit" value="Submit" />
</form>
@stop
routes.php
:
routes.php
:
Route::get('signup', 'ActionController@showForm');
Route::post('signup', 'ActionController@processForm');
Route::get('result', 'ActionController@showResult');
回答by ehp
You should check Laravel 4's documentation on authenticating users, which can be found at:
您应该查看 Laravel 4 关于验证用户的文档,可以在以下位置找到:
http://laravel.com/docs/security#authenticating-users
http://laravel.com/docs/security#authenticating-users
Basically you can authenticate a user by passing $data to Auth::attempt(). Pass true as the second parameter to Auth::attempt() to remember the user for future logins:
基本上,您可以通过将 $data 传递给 Auth::attempt() 来对用户进行身份验证。将 true 作为第二个参数传递给 Auth::attempt() 以记住用户以备将来登录:
$data = Input::all();
if (Auth::attempt($data, ($data['rememberme'] == 'on') ? true : false)
return Redirect::to('result');
else
{
$message_arr = array('message' => 'Invalid username or password!');
return View::make('signup', $message_arr);
}
You should use Laravel's method for authentication, as it takes care of password reminders and more.
您应该使用 Laravel 的方法进行身份验证,因为它负责密码提醒等。