Laravel Auth::attempt/login 页面无法正常工作
Laravel Auth::attempt/login page isn't work properly
我的注册页面运行良好。数据已正确插入名为 "registered_users" 的 table。我的登录页面无法正常运行。
routes.php
<?php
Route::get('/', function()
{
return View::make('index');
});
//About page
Route::get('about', function()
{
return View::make('about');
});
//reister route
Route::get('register', function(){
return View::make('register');
});
Route::post('register_action', function()
{
$obj = new RegisterController() ;
return $obj->store();
});
Route::get('login', function(){
return View::make('login');
});
Route::post('logincheck', function(){
// username and password validation rule
$data = Input::except(array('_token')) ;
$rule = array(
'name' => 'required',
'password' => 'required',
) ;
$message = array(
'password.required' => 'The Password field is required.',
'name.required' => 'The Username is required.',
);
$v = Validator::make($data,$rule);
if ($v->fails()) {
// username or password missing
// validation fails
// used to retain input values
Input::flash ();
// return to login page with errors
return Redirect::to('login')
->withInput()
->withErrors ($v->messages ());
} else {
$userdata = array (
'name' => Input::get('name'),
'password' => Input::get('password')
);
//var_dump($userdata);
If (Auth::attempt ($userdata)) {
// authentication success, enters home page
return Redirect::to('home');
} else {
// authentication fail, back to login page with errors
return Redirect::to('login')
->withErrors('Incorrect login details');
}//end if
}//end of v else
});
// Route::get ('home',function(){
// return View::make('home');
// });
Route::get ('test',function(){
return View::make('test');
});
Route::group (array ('before' => 'auth'), function () {
Route::get ('home',function(){
return View::make('home');
});
});
Route::get ('logout',function(){
Auth::logout ();
return Redirect::to('login');
});
我检查了 Route::post('logincheck', function() in routes.php 一步一步......一切都很好......但是在 Route::post('logincheck', function() in routes.php 无法正常工作。无论我在登录表单中输入正确的名称和密码还是错误,它只显示 'Incorrect login details' 消息
If (Auth::attempt ($userdata)) {
// authentication success, enters home page
return Redirect::to('home');
} else {
// authentication fail, back to login page with errors
return Redirect::to('login')
->withErrors('Incorrect login details');
}//end if
login.blade.php
@extends('layout')
@section('main')
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Registrationhhjh</title>
</head>
<body>
<section class="header">
<div class="bannner"> </div>
<div class="container">
<h1>Sign In</h1>
@if ($errors->any())
<ul style="color:red;">
{{ implode('', $errors->all('<li>:message</li>')) }}
</ul>
@endif
{{ Form::open(array('url' => 'logincheck')) }}
{{ Form::text('name', '', array('placeholder'=>'Name')) }}<br><br>
{{ Form::password('password', '', array('placeholder'=>'Password')) }}<br><br>
{{ Form::submit('Sign in', array('class'=>'btn btn-success')) }}
<!-- {{ Form::submit('register', array('class'=>'btn btn-primary')) }} -->
{{ HTML::link('register', 'Register', array('class' => 'btn btn-info'))}}
{{ Form::close() }}
</div>
</section>
</body>
</html>
@stop
User.php(model)。我认为 User.php(model)
中可能存在问题
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'registered_users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
//protected $hidden = array('password');
public function getAuthIdentifier()
{
return $this->getKey();
}
public function getAuthPassword()
{
return $this->password;
}
public function getRememberToken()
{
return $this->remember_token;
}
public function setRememberToken($value)
{
$this->remember_token = $value;
}
public function getRememberTokenName()
{
return "remember_token";
}
public function getReminderEmail()
{
return $this->email;
}
}
auth.php..谁能告诉我什么是 'table' => 'users', in auto.php?....一旦我将其重命名为 'table' => 'registered_users' 因为我没有 'users' 命名为 table.but 问题仍然存在(同样我不能去 'home page' )
<?php
return array(
/*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This driver manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "database", "eloquent"
|
*/
'driver' => 'eloquent',
/*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/
'model' => 'User',
/*
|--------------------------------------------------------------------------
| Authentication Table
|--------------------------------------------------------------------------
|
| When using the "Database" authentication driver, we need to know which
| table should be used to retrieve your users. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/
'table' => 'users',
/*
|--------------------------------------------------------------------------
| Password Reminder Settings
|--------------------------------------------------------------------------
|
| Here you may set the settings for password reminders, including a view
| that should be used as your password reminder e-mail. You will also
| be able to set the name of the table that holds the reset tokens.
|
| The "expire" time is the number of minutes that the reminder should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
我使用 wamp 服务器..我使用 laravel 4.2..我的数据库名称是 mydb...table 名称
registered_users
table 有 id,name,email,password,remember_token,created_at,updated_at 字段
remember_token字段类型是varchar(270)...remember_token[=69=有什么限制吗] 尺寸
请帮助我任何人登录系统有什么问题?
这是我的 RegisterController.php
<?php
class RegisterController extends BaseController {
/*
|--------------------------------------------------------------------------
| Default Home Controller
|--------------------------------------------------------------------------
|
| You may wish to use controllers instead of, or in addition to, Closure
| based routes. That's great! Here is an example controller method to
| get you started. To route to this controller, just add the route:
|
| Route::get('/', 'HomeController@showWelcome');
|
*/
// public function store()
// {
// Register::saveFormData(Input::except(array('_token')));
// }
public function store()
{
$data = Input::except(array('_token')) ;
$rule = array(
'name' => 'required|unique:registered_users',
'email' => 'required|email|unique:registered_users',
'password' => 'required|min:6|same:cpassword',
'cpassword' => 'required|min:6'
) ;
$message = array(
'cpassword.required' => 'The confirm password field is required.',
'cpassword.min' => 'The confirm password must be at least 6 characters',
'password.same' => 'The :attribute and confirm password field must match.',
);
$validator = Validator::make($data,$rule,$message);
// $validator = Validator::make($data,$rule);
if ($validator->fails())
{
return Redirect::to('register')
->withErrors($validator->messages());
}
else
{
Register::saveFormData(Input::except(array('_token','cpassword')));
return Redirect::to('register')
->withMessage('Successfully registered');
}
}
}
这里是model/Register.php
<?php
class Register extends Eloquent {
protected $guarded = array();
protected $table = 'registered_users'; // database table name
public $timestamps = 'false' ; // to disable default timestamp fields
// model function to store form data to database
public static function saveFormData($data)
{
DB::table('registered_users')->insert($data);
}
}
最后我解决了我的 problem.My 问题是我将密码存储为 plaintext.for 解决了我的问题 我刚刚编辑了我的 models\Register.php 文件。
这是我编辑的 models\Register.php
<?php
class Register extends Eloquent {
protected $guarded = array();
protected $table = 'registered_users'; // database table name
public $timestamps = 'false' ; // to disable default timestamp fields
// model function to store form data to database
public static function saveFormData($data)
{
// DB::table('registered_users')->insert($data);
$name = Input::get('name');
$email = Input::get('email');
$password = Hash::make(Input::get('password'));
$user = new User;
$user->name = $name;
$user->email = $email;
$user->password = $password;
$user->save();
}
}
Auth::attempt() 函数会自动散列您的密码。问题是您将密码存储为纯文本。因此,当该方法检查您的密码是否正确时,它会对其进行哈希处理并将其与保存的密码进行比较。该密码未经过哈希处理,因此除非您在注册时对密码进行哈希处理,否则它不会起作用。
我的注册页面运行良好。数据已正确插入名为 "registered_users" 的 table。我的登录页面无法正常运行。
routes.php
<?php
Route::get('/', function()
{
return View::make('index');
});
//About page
Route::get('about', function()
{
return View::make('about');
});
//reister route
Route::get('register', function(){
return View::make('register');
});
Route::post('register_action', function()
{
$obj = new RegisterController() ;
return $obj->store();
});
Route::get('login', function(){
return View::make('login');
});
Route::post('logincheck', function(){
// username and password validation rule
$data = Input::except(array('_token')) ;
$rule = array(
'name' => 'required',
'password' => 'required',
) ;
$message = array(
'password.required' => 'The Password field is required.',
'name.required' => 'The Username is required.',
);
$v = Validator::make($data,$rule);
if ($v->fails()) {
// username or password missing
// validation fails
// used to retain input values
Input::flash ();
// return to login page with errors
return Redirect::to('login')
->withInput()
->withErrors ($v->messages ());
} else {
$userdata = array (
'name' => Input::get('name'),
'password' => Input::get('password')
);
//var_dump($userdata);
If (Auth::attempt ($userdata)) {
// authentication success, enters home page
return Redirect::to('home');
} else {
// authentication fail, back to login page with errors
return Redirect::to('login')
->withErrors('Incorrect login details');
}//end if
}//end of v else
});
// Route::get ('home',function(){
// return View::make('home');
// });
Route::get ('test',function(){
return View::make('test');
});
Route::group (array ('before' => 'auth'), function () {
Route::get ('home',function(){
return View::make('home');
});
});
Route::get ('logout',function(){
Auth::logout ();
return Redirect::to('login');
});
我检查了 Route::post('logincheck', function() in routes.php 一步一步......一切都很好......但是在 Route::post('logincheck', function() in routes.php 无法正常工作。无论我在登录表单中输入正确的名称和密码还是错误,它只显示 'Incorrect login details' 消息
If (Auth::attempt ($userdata)) {
// authentication success, enters home page
return Redirect::to('home');
} else {
// authentication fail, back to login page with errors
return Redirect::to('login')
->withErrors('Incorrect login details');
}//end if
login.blade.php
@extends('layout')
@section('main')
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Registrationhhjh</title>
</head>
<body>
<section class="header">
<div class="bannner"> </div>
<div class="container">
<h1>Sign In</h1>
@if ($errors->any())
<ul style="color:red;">
{{ implode('', $errors->all('<li>:message</li>')) }}
</ul>
@endif
{{ Form::open(array('url' => 'logincheck')) }}
{{ Form::text('name', '', array('placeholder'=>'Name')) }}<br><br>
{{ Form::password('password', '', array('placeholder'=>'Password')) }}<br><br>
{{ Form::submit('Sign in', array('class'=>'btn btn-success')) }}
<!-- {{ Form::submit('register', array('class'=>'btn btn-primary')) }} -->
{{ HTML::link('register', 'Register', array('class' => 'btn btn-info'))}}
{{ Form::close() }}
</div>
</section>
</body>
</html>
@stop
User.php(model)。我认为 User.php(model)
中可能存在问题<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'registered_users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
//protected $hidden = array('password');
public function getAuthIdentifier()
{
return $this->getKey();
}
public function getAuthPassword()
{
return $this->password;
}
public function getRememberToken()
{
return $this->remember_token;
}
public function setRememberToken($value)
{
$this->remember_token = $value;
}
public function getRememberTokenName()
{
return "remember_token";
}
public function getReminderEmail()
{
return $this->email;
}
}
auth.php..谁能告诉我什么是 'table' => 'users', in auto.php?....一旦我将其重命名为 'table' => 'registered_users' 因为我没有 'users' 命名为 table.but 问题仍然存在(同样我不能去 'home page' )
<?php
return array(
/*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This driver manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "database", "eloquent"
|
*/
'driver' => 'eloquent',
/*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/
'model' => 'User',
/*
|--------------------------------------------------------------------------
| Authentication Table
|--------------------------------------------------------------------------
|
| When using the "Database" authentication driver, we need to know which
| table should be used to retrieve your users. We have chosen a basic
| default value but you may easily change it to any table you like.
|
*/
'table' => 'users',
/*
|--------------------------------------------------------------------------
| Password Reminder Settings
|--------------------------------------------------------------------------
|
| Here you may set the settings for password reminders, including a view
| that should be used as your password reminder e-mail. You will also
| be able to set the name of the table that holds the reset tokens.
|
| The "expire" time is the number of minutes that the reminder should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
我使用 wamp 服务器..我使用 laravel 4.2..我的数据库名称是 mydb...table 名称 registered_users table 有 id,name,email,password,remember_token,created_at,updated_at 字段
remember_token字段类型是varchar(270)...remember_token[=69=有什么限制吗] 尺寸
请帮助我任何人登录系统有什么问题?
这是我的 RegisterController.php
<?php
class RegisterController extends BaseController {
/*
|--------------------------------------------------------------------------
| Default Home Controller
|--------------------------------------------------------------------------
|
| You may wish to use controllers instead of, or in addition to, Closure
| based routes. That's great! Here is an example controller method to
| get you started. To route to this controller, just add the route:
|
| Route::get('/', 'HomeController@showWelcome');
|
*/
// public function store()
// {
// Register::saveFormData(Input::except(array('_token')));
// }
public function store()
{
$data = Input::except(array('_token')) ;
$rule = array(
'name' => 'required|unique:registered_users',
'email' => 'required|email|unique:registered_users',
'password' => 'required|min:6|same:cpassword',
'cpassword' => 'required|min:6'
) ;
$message = array(
'cpassword.required' => 'The confirm password field is required.',
'cpassword.min' => 'The confirm password must be at least 6 characters',
'password.same' => 'The :attribute and confirm password field must match.',
);
$validator = Validator::make($data,$rule,$message);
// $validator = Validator::make($data,$rule);
if ($validator->fails())
{
return Redirect::to('register')
->withErrors($validator->messages());
}
else
{
Register::saveFormData(Input::except(array('_token','cpassword')));
return Redirect::to('register')
->withMessage('Successfully registered');
}
}
}
这里是model/Register.php
<?php
class Register extends Eloquent {
protected $guarded = array();
protected $table = 'registered_users'; // database table name
public $timestamps = 'false' ; // to disable default timestamp fields
// model function to store form data to database
public static function saveFormData($data)
{
DB::table('registered_users')->insert($data);
}
}
最后我解决了我的 problem.My 问题是我将密码存储为 plaintext.for 解决了我的问题 我刚刚编辑了我的 models\Register.php 文件。 这是我编辑的 models\Register.php
<?php
class Register extends Eloquent {
protected $guarded = array();
protected $table = 'registered_users'; // database table name
public $timestamps = 'false' ; // to disable default timestamp fields
// model function to store form data to database
public static function saveFormData($data)
{
// DB::table('registered_users')->insert($data);
$name = Input::get('name');
$email = Input::get('email');
$password = Hash::make(Input::get('password'));
$user = new User;
$user->name = $name;
$user->email = $email;
$user->password = $password;
$user->save();
}
}
Auth::attempt() 函数会自动散列您的密码。问题是您将密码存储为纯文本。因此,当该方法检查您的密码是否正确时,它会对其进行哈希处理并将其与保存的密码进行比较。该密码未经过哈希处理,因此除非您在注册时对密码进行哈希处理,否则它不会起作用。