gpt4 book ai didi

php - Laravel 5 - 如何检查用户名和密码是否与表匹配?

转载 作者:可可西里 更新时间:2023-11-01 13:53:28 24 4
gpt4 key购买 nike

我在 Laravel 5 中创建了以下登录表单,我想简单地检查用户名和密码是否与数据库表匹配,如果匹配,则重定向到仪表板页面,否则继续登录页面。我也在尝试自己寻找解决方案,但我发布这个问题是为了了解如何在 Laravel 5 中执行这些操作。

有什么想法吗??

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

数据库结构

enter image description here

enter image description here

login.blade.php( View )

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

    <?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(模型)

    <?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'];
}

路由.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');

最佳答案

尝试使用身份验证尝试

    $email=$request->email;
$password=$request->password;

if(Auth::attempt(['email'=>$email,'password'=>$password]))
{
return redirect()->intended('admin/dashboard');
}

这将检查身份验证

在这里你可以阅读官方文档

http://laravel.com/docs/5.1/authentication#authenticating-users

更新

首先你需要创建名为users的表

id|username|password|email|remember_token|created_at|updated_at

然后在你的用户模型中

protected $table = 'users';

protected $fillable = ['username', 'email', 'password'];

无论你想插入数据的哪一列都应该写在可填充数组中,created_at 和 updated_at 类型在 mysql 中是数据时间,所以它会自动插入数据和时间

在你的用户 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');
}

请注意 auth::attempt 会自动散列密码,因此您无需散列密码。

登录认证前插入一条记录和Hash密码。

$data=[];
$data['email']=$request->email;
$data['password']=Hash::make($password);
User::create($data);

更新 2

  public function insert()
{
$data=[];
$data['email']=$request->email;
$data['password']=Hash::make($password);
AdminLoginModel::create($data);
}

关于php - Laravel 5 - 如何检查用户名和密码是否与表匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32496980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com