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

标签 php laravel laravel-5

我在 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/

相关文章:

php - Laravel:离线模式?

php - 将多个 csv 文件导入 mysql 表

PHP array_filter 带参数

laravel - Homestead 2.0 多个站点,全部链接到同一网址

laravel - 在 laravel 5 测试期间如何模拟外部 API?

php - 无法通过 Composer 安装 laravel 安装程序

php - Laravel whereSlug 不工作

php - 如何在Docker容器中创建自动启动服务?

php - 无法使用包含命令从另一个 PHP 文件检索 PHP 值

php - laravel 存储过程错误