0

我创建了一个带有记住我复选框的登录表单。我想知道如何允许用户在关闭浏览器时保持登录或在用户关闭浏览器时注销用户。一个示例代码会很好,谢谢。

这是我的代码

类 HomeController 扩展 BaseController {

public function getIndex()
 {    

     if(Auth::check())
         {
              return Redirect::to('profile');
         }

    return View::make('index');

}

public function postRegister()
 {        
         //gets array of the register form input values
         $value = Input::all();

         // create a new instance of the User model
         $user = new User;

         $validate = $user->userValidate($value);

         //checks if the validation for the field fails 
         if($validate->fails())
           {    
               /* $message = $validation->messages();
               return $message; */ 
              return  Redirect::back()->withInput()->withErrors($validate);

           }



                 //adds the users input to speicific field in the users table
                 $user->user_name = $value['username'];
                 $user->email = $value['email'];
                 $user->password = Hash::make($value['password']);

                 //save the inputs to the users table
                 $user->save();

                 return 'information has been stored';



    }

   public function getRegister()
      {      
              $title = 'Register';
             return View::make('register')->with('title',$title); 
      }



  public function getSignIn()
      {
                 $title = 'Signup';
                 return View::make('signup')->with('title',$title);
      }


   public function postSignIn()
     {  

          //user's information
         $credentials = array('email' => Input::get('email'),'password'=>Input::get('password'));

         //logs this user in and checked if they are registered already in 
         if(Auth::attempt($credentials,false))
           {
             return Redirect::to('profile');
           }

       return Redirect::back()->withInput();

     }

}

4

2 回答 2

1

您只需在登录方法中打开它:

if (Auth::attempt(array('email' => $email, 'password' => $password), true))
{
    // The user will now be logged in and remembered
}
else
{
   // Raise a login error
}

这个“真”参数是为了记住你的用户。

这是 Laravel Auth::attempt() 方法声明:

public function attempt(array $credentials = array(), $remember = false, $login = true)
{
  ...
}
于 2013-07-24T15:10:10.633 回答
0

您可以在用户浏览器上设置一个 cookie(如果您是,请确保告诉他们)以识别他们。但请注意,这可能会被恶意用户修改。 PHP Cookie 文档

于 2013-07-24T15:10:39.450 回答