php Laravel 5 - 如何检查用户名和密码与表匹配?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32496980/
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
Laravel 5 - How to check username & password is match with table?
提问by Mr.Happy
I have created the below login form in Laravel 5
and I want to simply check if the username & password matches with that of the database table and if so, redirect to the dashboard page else stay on the login page. I am also trying to find the solution by myself but I am posting this question to get an idea of how to do these things in Laravel 5
.
我在其中创建了以下登录表单Laravel 5
,我想简单地检查用户名和密码是否与数据库表的用户名和密码匹配,如果匹配,则重定向到仪表板页面,否则留在登录页面上。我也在尝试自己找到解决方案,但我发布这个问题是为了了解如何在Laravel 5
.
Any idea??
任何的想法??
2015_09_10_050324_admin_details.php (migrations)
2015_09_10_050324_admin_details.php(迁移)
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AdminDetails extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('admin_details', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password', 60);
$table->integer('status');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('admin_details');
}
}
Database structure
数据库结构
login.blade.php (view)
login.blade.php(查看)
<form name="frmLogin" action="{{ URL::to('administrator/userAuthentication') }}" method="post">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<div class="form-group has-feedback">
<input type="text" name="username" id="username"class="form-control" placeholder="Username">
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" name="password" id="password" class="form-control" placeholder="Password">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-4">
<button type="submit" class="btn btn-primary btn-block btn-flat">Login</button>
</div><!-- /.col -->
</div>
</form>
AdminLoginController.php (controller)
AdminLoginController.php(控制器)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
use App\Http\Controllers\Controller;
use App\AdminLoginModel;
class AdminLoginController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return view('backend.login');
}
/**
* Handle an authentication attempt for admin user.
*
*/
public function userAuthentication(Request $request)
{
if (Auth::attempt(array('username' => $request->username, 'password' => $request->password))){
return "success";
}else{
return "Wrong Credentials";
}
die;
}
}
AdminLoginModel.php (model)
AdminLoginModel.php(模型)
<?php
/*namespace App;
use DB;
use Illuminate\Database\Eloquent\Model;*/
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class AdminLoginModel extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword;
protected $table = 'admin_details';
protected $fillable = ['username', 'password'];
}
routes.php
路由文件
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::resource('dashboard','DashboardController');
Route::resource('administrator','AdminLoginController');
Route::resource('users','AdminLoginController');
Route::resource('administrator/userAuthentication', 'AdminLoginController@userAuthentication');
采纳答案by RaMeSh
try like this
像这样尝试
login.blade.php
登录名.blade.php
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h3>Login Form</h3>
{!! Form::open(array('url' => 'login', 'method' => 'post')) !!}
<div class="form-group">
{!! Form::label('UserName') !!}
{!! Form::text('username', null,
array(
'class'=>'form-control',
'placeholder'=>'Your UserName')) !!}
</div>
<div class="form-group">
{!! Form::label('password') !!}
{!! Form::text('password', null,
array(
'class'=>'form-control',
'placeholder'=>'Your Password')) !!}
</div>
<div class="form-group">
{!! Form::submit('Login',
array('class'=>'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
</div>
</body>
</html>
Model:-
模型:-
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class UserRegisters extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
protected $table = 'userregisters';
protected $fillable = ['user_name', 'password'];
}
?>
Controller:-
控制器:-
<?php namespace App\Http\Controllers;
use Input;
use App\Http\Requests;
use App\User;
use App\UserRegisters;
use App\UserProfiles;
use Validator;
use View;
use Auth;
use App\Http\Controllers\Redirect;
use Session;
use Hash;
use DB;
class UserRegisterController extends Controller
{
/**
* Login a Registered Users.
*
*/
public function login(){
$uname = Input::get('username');
$password = Input::get('password');
if (Auth::attempt(array('user_name' => $uname, 'password' => $password))){
return "success";
}
else {
return "Wrong Credentials";
}
}
}
}
route:-
路线:-
Route::post('/login', 'UserRegisterController@login');
migration:-
移民:-
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Userregisters extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('userregisters', function($table)
{
$table->increments('id');
$table->string('first_name', 128);
$table->string('last_name', 128);
$table->string('user_name', 128);
$table->string('password', 128);
$table->string('email', 128);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('userregisters');
}
}
let me know if there any errors.
让我知道是否有任何错误。
回答by iCoders
try using auth attempt
尝试使用身份验证尝试
$email=$request->email;
$password=$request->password;
if(Auth::attempt(['email'=>$email,'password'=>$password]))
{
return redirect()->intended('admin/dashboard');
}
this will check authentication
这将检查身份验证
Here you can read official documentation
在这里你可以阅读官方文档
http://laravel.com/docs/5.1/authentication#authenticating-users
http://laravel.com/docs/5.1/authentication#authenticating-users
Update
更新
first you need to create table called users
首先,您需要创建名为 users
id|username|password|email|remember_token|created_at|updated_at
then in your user model
然后在您的用户模型中
protected $table = 'users';
protected $fillable = ['username', 'email', 'password'];
whichever column you want to insert data that should write in fillable array and created_at and updated_at type is datatime in mysql so it automatically insert data and time
您要插入应写入可填充数组和 created_at 和 updated_at 类型的数据的任何列在 mysql 中都是 datatime,因此它会自动插入数据和时间
In your user controller
在您的用户控制器中
public function loginPost(Request $request)
{
$email=$request->email;
$password=$request->password;
if(Auth::attempt(['email'=>$email,'password'=>$password]))
{
return redirect()->intended('admin/dashboard');
}
return Redirect::to('login');
}
and note that auth::attempt will automatically hash password so you no need to hash password.
并注意 auth::attempt 将自动散列密码,因此您无需散列密码。
Before login authentication insert one record and Hash password.
在登录认证之前插入一条记录和哈希密码。
$data=[];
$data['email']=$request->email;
$data['password']=Hash::make($password);
User::create($data);
update 2
更新 2
public function insert()
{
$data=[];
$data['email']=$request->email;
$data['password']=Hash::make($password);
AdminLoginModel::create($data);
}
回答by Amirouche Zeggagh
first import
第一次进口
use Hash;
then encode your password with Hash
然后用哈希编码你的密码
Hash::make($input['password']);
finally check your password and email like this
最后像这样检查您的密码和电子邮件
$model = YourModelHere::where('email', $request->email)->first();
if (Hash::check($request->password, $model->password, [])) {
// success
}
//failed