2013-08-31 27 views
1

我是Laravel的新手,试图制作一个非常简单的登录表单。使用Laravel实现“记住我”功能4

此表单有一个“记住我”复选框。我试图用Cookie::make()来实现它的功能,但事实证明我需要返回一个Response以保存它。

当我在我的浏览器中检查从localhost存储的Cookie时,我没有找到名为username的cookie。我做了一些调查,结果发现我必须将cookie放入Response然后返回。

问题是,我不想返回一个Response

我在学习过程中尚未达到Auth班。所以没有使用这个类的解决方案会更合适。

这里是我的代码:

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); 
    } 
} 

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

Route::get('signup', '[email protected]'); 

Route::post('signup', '[email protected]'); 

Route::get('result', '[email protected]'); 

回答

5

您应该检查Laravel 4的文档上的认证用户,可以在这里找到:

http://laravel.com/docs/security#authenticating-users

基本上,您可以通过将$ data传递给Auth :: attempt()来验证用户身份。传递true作为第二个参数来验证::尝试(),以记住用户为以后登录:

$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); 
} 

您应该使用Laravel的方法进行验证,因为它需要照顾密码提醒和更多的。