laravel身份证验证

This article has been upgraded to work with Laravel 4.1.26
本文已升级为可与Laravel 4.1.26一起使用

Today we'll be creating a simple Laravel authentication. Using migrations, seeding, routes, controllers, and views, we'll walk through the entire process.

今天,我们将创建一个简单的Laravel身份验证。 使用迁移,播种,路由,控制器和视图,我们将逐步完成整个过程。

This tutorial will walk us through:

本教程将引导我们完成:

  • Creating a users table using migrations 使用迁移创建用户表
  • Filling our new users table with sample users using seeding 使用种子填充示例用户来填充我们的新用户表
  • Using the great artisan command line tool to migrate and seed 使用出色的artisan命令行工具进行迁移和播种
  • Creating a login form and processing a login using routes, controllers, and views 使用路由,控制器和视图创建登录表单并处理登录
  • Handling login errors 处理登录错误
  • Use Eloquent ORM to validate a user 使用雄辩的ORM验证用户

准备我们的数据库 ( Getting our Database Ready )

To get our authentication working, we will need to have a database and users to login with.

为了使我们的身份验证有效,我们将需要有一个数据库和用于登录的用户。

数据库设置 (Database Setup)

Set up your database and user. Assign that user to the database and make sure you update your settings in app/config/database.php.

设置您的数据库和用户。 将该用户分配给数据库,并确保您更新app/config/database.php

移居 (Migrations)

Migrations are a way we can manipulate our database within our codebase. This means we don't have to get our hands dirty by doing any SQL commands or messing around inside a tool like phpmyadmin. For more information and the benefits of migrations, see the official docs.

迁移是我们可以在代码库中操作数据库的一种方式。 这意味着我们不必通过执行任何SQL命令或在诸如phpmyadmin之类的工具中弄乱自己的手。 有关更多信息以及迁移的好处,请参见官方文档 。

Migrations are very easy to create. The easiest way to create a migration will be to use the great artisan command line interface created by Taylor Otwell. To create the migration, via the command line, in the root folder of your application, simply type:

迁移非常容易创建。 创建迁移的最简单方法是使用Taylor Otwell创建的出色的工匠命令行界面。 要通过命令行在应用程序的根文件夹中创建迁移 ,只需键入:

php artisan migrate:make create_users_table ––create=users

php artisan migrate:make create_users_table ––create=users

This will automatically create a migrations file inside of your app/database/migrations folder. Let's take a look at the newly created file.

这将在您的app/database/migrations文件夹内自动创建一个迁移文件。 让我们看一下新创建的文件。

// app/database/migrations/####_##_##_######_create_users_table.php<?phpuse IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;class CreateUsersTable extends Migration {/*** Run the migrations.** @return void*/public function up(){Schema::create('users', function(Blueprint $table){$table->increments('id');$table->timestamps();});}/*** Reverse the migrations.** @return void*/public function down(){//}}

Laravel generates the core of the migration file for you and the --create command will let the migration create the table for you. It will create a table for you with an id field and the timestamps field. This will make created_at and updated_at fields. Now we use the Schema Builder to create our users table.

Laravel为您生成迁移文件的核心,而--create命令将使迁移为您创建表。 它将为您创建一个具有id字段和timestamps字段的表。 这将创建created_at和Updated_at字段。 现在,我们使用Schema Builder创建我们的users表。

@media (max-width: 1280px) { .go-go-gadget-react img:first-child { display: none; } }@media (max-width: 780px) {.go-go-gadget-react { flex-direction: column; }.go-go-gadget-react img { margin-left: 0 !important; margin-bottom: 12px !important; }.header-thingy { margin-top: 20px; }.button-thingy { margin-left: 0 !important; margin-top: 12px !important; }} @media (max-width: 1280px) { .go-go-gadget-react img:first-child { display: none; } }@media (max-width: 780px) {.go-go-gadget-react { flex-direction: column; }.go-go-gadget-react img { margin-left: 0 !important; margin-bottom: 12px !important; }.header-thingy { margin-top: 20px; }.button-thingy { margin-left: 0 !important; margin-top: 12px !important; }}

// app/database/migrations/####_##_##_######_create_users_table.php<?phpuse IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;class CreateUsersTable extends Migration {/*** Run the migrations.** @return void*/public function up(){Schema::create('users', function(Blueprint $table){$table->increments('id');$table->string('name', 32);$table->string('username', 32);$table->string('email', 320);$table->string('password', 64);// required for Laravel 4.1.26$table->string('remember_token', 100)->nullable();$table->timestamps();});}/*** Reverse the migrations.** @return void*/public function down(){Schema::drop('users');}}

Now this migration file will be responsible for creating the users table and also destroying it if needed. To run the migration and create our user table, use the command line again and run:

现在,此迁移文件将负责创建users表并在需要时销毁它。 要运行迁移创建我们的用户表 ,请再次使用命令行并运行:

php artisan migrate

php artisan migrate

Just like that, the command will use the up() function and bam! We have our users table with all the columns we wanted.

就像这样,该命令将使用up()函数和bam! 我们的用户表包含我们想要的所有列。

Reverting Migrations: Now if you wanted to rollback migrations, we could use php artisan migrate:rollback or php artisan migrate:reset.
还原迁移:现在,如果您要回滚迁移,我们可以使用php artisan migrate:rollback migration php artisan migrate:rollbackphp artisan migrate:reset migration php artisan migrate:reset

Now that we have our table, lets create sample users.

现在我们有了表,让我们创建示例用户。

播种 (Seeding)

Seeding is the technique of filling our database with sample data so we can test and create our applications. It really does make building applications much easier. For seeder files, we won't be using artisan. We'll make these the good old fashioned way... New file. In your app/database/seeds folder, create a file called UserTableSeeder.php.

播种是一种使用示例数据填充数据库的技术,因此我们可以测试和创建应用程序。 确实确实使构建应用程序容易得多。 对于种子文件,我们将不使用工匠。 我们将采用老式的好方法...新文件。 在您的app/database/seeds文件夹中,创建一个名为UserTableSeeder.php的文件。

// app/database/seeds/UserTableSeeder.php<?phpclass UserTableSeeder extends Seeder
{public function run()
{DB::table('users')->delete();User::create(array('name'     => 'Chris Sevilleja','username' => 'sevilayha','email'    => 'chris@scotch.io','password' => Hash::make('awesome'),));
}}

We will create a user and all of the above is pretty self explanatory aside from password. We will use Laravel's Hash class to create a secure Bcrypt hashing of our password. This is always a good practice to hash our password and to read more about Laravel security, check out the docs. Now that we have created our file, we need to Laravel to call it. Inside the app/database/seeds/DatabaseSeeder.php, add the line $this->call('UserTableSeeder');.

我们将创建一个用户,除密码外,以上所有内容都非常容易解释。 我们将使用Laravel的Hash类为密码创建安全的Bcrypt哈希。 散列密码并阅读有关Laravel安全性的更多信息,这是一个好习惯,请查阅docs 。 现在我们已经创建了文件,我们需要Laravel来调用它。 在app/database/seeds/DatabaseSeeder.php ,添加$this->call('UserTableSeeder');

// app/database/seeds/DatabaseSeeder.php<?phpclass DatabaseSeeder extends Seeder {/*** Run the database seeds.** @return void*/public function run(){Eloquent::unguard();$this->call('UserTableSeeder');}}

Once we're done with our seeder file, we can inject that user into our database using:

一旦完成了种子文件,我们可以使用以下方法将该用户注入数据库中:

php artisan db:seed

php artisan db:seed

应用程序 ( The Application )

Now that we have a database, a table thanks to migrations, and a user thanks to seeding, we can build the authentication system. We will need to create routes, controllers, and views for our form.

现在我们有了数据库,有了表,有了迁移,有了用户,有了种子,有了用户,我们可以构建身份验证系统。 我们将需要为表单创建路由,控制器和视图。

路线 (Routes)

In Laravel, our routes file dictates the lay of the land in our application. We will define 2 routes for our login, one for the http get to show the form, and one for the http post request to process the form. Laravel let's us define routes based on HTTP request types and this helps to organize our application and how a user interacts around the site. For more info on this: show link. Add the following routes we need in our app/routes.php file:

在Laravel中,我们的路线文件规定了我们应用程序中的土地位置。 我们将为登录定义2条路径,一条用于显示表单的http get路径,另一条用于处理表单的http post请求。 Laravel让我们根据HTTP请求类型定义路由,这有助于组织我们的应用程序以及用户如何在站点周围进行交互。 有关此的更多信息:显示链接。 在我们的app/routes.php文件中添加以下所需的路由:

// app/routes.php<?php// route to show the login form
Route::get('login', array('uses' => 'HomeController@showLogin'));// route to process the form
Route::post('login', array('uses' => 'HomeController@doLogin'));

Now if we go to our application in our browser and go to www.example.com/login, we will get an error because we haven't defined the HomeController@showLogin function yet. Let's do that.

现在,如果我们在浏览器中转到我们的应用程序并转到www.example.com/login ,则会收到一个错误消息,因为我们尚未定义HomeController @ showLogin函数。 来做吧。

控制者 (Controller)

In our app/controllers directory, Laravel should already come with a HomeController.php and a BaseController.php. Inside our HomeController.php, we are going to create the two functions we need. Add these two.

在我们的app/controllers目录中,Laravel应该已经带有HomeController.phpBaseController.php 。 在HomeController.php中 ,我们将创建所需的两个函数。 加上这两个。

// app/controllers/HomeController.php<...public function showLogin()
{// show the formreturn View::make('login');
}public function doLogin()
{
// process the form
}

For now, we will only deal with the function to show the form.

现在,我们将只处理显示表单的函数。

视图 (View)

The easiest part of this process will be creating our login view. In the app/views folder, create a file called login.blade.php. The .blade.php extension lets Laravel know that we will be using its Blade Templating system.

此过程最简单的部分将是创建登录视图。 在app/views文件夹中,创建一个名为login.blade.php的文件。 .blade.php扩展名使Laravel知道我们将使用其刀片模板系统。

<!-- app/views/login.blade.php --><<!doctype html>
<html>
<head>
<title>Look at me Login</title>
</head>
<body><{{ Form::open(array('url' => 'login')) }}
<h1>Login</h1><!-- if there are login errors, show them here -->
<p>{{ $errors->first('email') }}{{ $errors->first('password') }}
</p><p>{{ Form::label('email', 'Email Address') }}{{ Form::text('email', Input::old('email'), array('placeholder' => 'awesome@awesome.com')) }}
</p><p>{{ Form::label('password', 'Password') }}{{ Form::password('password') }}
</p><p>{{ Form::submit('Submit!') }}</p>
{{ Form::close() }}

When someone submits the form, it posts to the HomeController@doLogin function. Let's validate the information and process the form.

当有人提交表单时,它将发布到HomeController @ doLogin函数。 让我们验证信息并处理表单。

If there are validation errors, they will be redirected here and the email input will already be filled in with their old input. Errors will also show if there are any.

如果存在验证错误,则将在此处将其重定向,并且电子邮件输入将已经用其旧输入填写。 如果有错误,也会显示错误。

处理表格 (Processing the Form)

Back in our HomeController.php, let's build out our doLogin() function. We have to validate the information sent to make sure that we have an email and a password. Both fields are required.

回到HomeController.php中 ,让我们构建doLogin()函数。 我们必须验证发送的信息,以确保我们具有电子邮件密码 。 这两个字段都是必需的。

// app/controllers/HomeController.php<public function doLogin()
{
// validate the info, create rules for the inputs
$rules = array('email'    => 'required|email', // make sure the email is an actual email'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
);// run the validation rules on the inputs from the form
$validator = Validator::make(Input::all(), $rules);// if the validator fails, redirect back to the form
if ($validator->fails()) {return Redirect::to('login')->withErrors($validator) // send back all errors to the login form->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
} else {// create our user data for the authentication$userdata = array('email'     => Input::get('email'),'password'  => Input::get('password'));// attempt to do the loginif (Auth::attempt($userdata)) {// validation successful!// redirect them to the secure section or whatever// return Redirect::to('secure');// for now we'll just echo success (even though echoing in a controller is bad)echo 'SUCCESS!';} else {        // validation not successful, send back to form return Redirect::to('login');}}
}

We use the Auth class to authenticate a user. Auth::attempt() will check the plaintext password against the hashed password we saved in our database.

我们使用Auth类对用户进行身份验证。 Auth::attempt()将对照我们保存在数据库中的哈希密码来检查纯文本密码。

Try the login: Let's try to login with whatever we put in our app/database/UserTableSeeder.php file. In our case:

尝试登录:让我们尝试使用放入app/database/UserTableSeeder.php文件中的任何内容登录。 在我们的情况下:

<td>Password</td>
</tr>
<td>awesome</td>
</tr>
Email
chris@scotch.io
电子邮件
chris@scotch.io

If the authentication is successful (Auth::attempt() returns true), we will redirect the user to wherever they should go. After they are logged in, that users information is saved and can be accessed using Auth::user()->email;. If the authentication is not successful, we will be kicked back to the login form with errors and the old email input to populate the form.

如果验证成功(Auth :: attempt()返回true),我们将把用户重定向到他们应该去的任何地方。 登录后,将保存用户信息,并可以使用Auth::user()->email;进行访问Auth::user()->email; 。 如果验证不成功,我们将被踢回到登录表单,并输入错误信息和用于填充表单的旧电子邮件。

登出 ( Logout )

Logging out is a simple matter. We'll need a new route and a new function.

注销很简单。 我们需要一条新路线和新功能。

Add this route for logout.

添加此路由以注销。

// app/routes.php
...
Route::get('logout', array('uses' => 'HomeController@doLogout'));
...
POST route for security purposes. This will ensure that your logout won't be accidentally triggered. http://stackoverflow.com/questions/3521290/logout-get-or-post Also, to handle this as a POST路由。 这样可以确保您的登出不会被意外触发。 http://stackoverflow.com/questions/3521290/logout-get-or-post另外,要将其作为POST, you will have to handle your link differently. You'll have to create a POST request to your logout route. POST处理,您将必须以不同的方式处理链接。 您必须为登出路径创建一个POST请求。

For logout, we will flush and clean out the session and then redirect our user back to the login screen. You can change this to redirect a user wherever you would like. A home page or even a sad goodbye page.

对于注销,我们将刷新并清除会话,然后将用户重定向回登录屏幕。 您可以更改此设置,以将用户重定向到您想要的任何位置。 主页,甚至悲伤的再见页面。

// app/controllers/HomeController.php
public function doLogout()
{Auth::logout(); // log the user out of our applicationreturn Redirect::to('login'); // redirect the user to the login screen
}

Now that you have the route and the function, you can create a logout button by using the Laravel URL helper.

有了路由和功能之后,您就可以使用Laravel URL帮助器创建一个注销按钮。

<!-- LOGOUT BUTTON -->
<a href="{{ URL::to('logout') }}">Logout</a>

am! ( Bam! )

Now going to your login page, www.example.com/login and submitting the login form will provide validation, authentication against a user in the database, and a little more understanding of how Laravel makes things like building an authentication system easier. We'll be doing a lot more writeups on Laravel, so sound off in the comments if you have questions or anything else you want to see.

现在转到登录页面www.example.com/login并提交登录表单,将提供验证,针对数据库中用户的身份验证,以及对Laravel如何使诸如构建身份验证系统之类的事情更加容易的理解。 我们将在Laravel上做更多的文章,因此,如果您有任何疑问或想看到的其他内容,请在评论中取消。

Edit Updated the doLogin() function and the view to pass back errors and handle Auth correctly with Hash::check(). 编辑更新了doLogin()函数和视图,以传回错误并使用Hash :: check()正确处理Auth。 Edit #2 Changed back to the Auth::attempt() login method. 编辑#2更改回Auth :: attempt()登录方法。 Edit #3 Cleaned up and commented code examples. Provided code for download. 编辑#3清理并注释代码示例。 提供的代码可供下载。 Edit #4 Added logout. 编辑#4添加了注销。 Edit #5 Upgraded user migration to work with Laravel 4.1.26 编辑#5升级的用户迁移以与Laravel 4.1.26一起使用

翻译自: https://scotch.io/tutorials/simple-and-easy-laravel-login-authentication

laravel身份证验证

laravel身份证验证_简单的Laravel登录身份验证相关推荐

  1. 什么学习软件需要身份证验证_什么是两层身份验证,为什么我需要它?

    什么学习软件需要身份证验证 More and more banks, credit card companies, and even social media networks and gaming ...

  2. firebase登录验证_使用Firebase进行电话号码身份验证

    firebase登录验证 介绍 (Introduction) Ever since Firebase was introduced, I thought it would have a signifi ...

  3. 基于手机SIM卡的一键登录身份验证,你准备好了吗?

    登录作为App最关键的交互端口,是用户发生一切行为的开端.时至今日,登录这个动作已经变成一种不可或缺的需求,我们对于所采用的身份验证技术水平要求越来越高. 从登录场景看,短信验证是目前最常用的身份验证 ...

  4. [转][.NET 基于角色安全性验证] 之三:ASP.NET Forms 身份验证

    在开发过程中,我们需要做的事情包括: 1. 在 web.config 中设置 Forms 身份验证相关参数. 2. 创建登录页. 登录页中的操作包括: 1. 验证用户名和密码是否正确. 2. 创建身份 ...

  5. 两步验证杀手锏:Java 接入 Google 身份验证器实战

    转载自   两步验证杀手锏:Java 接入 Google 身份验证器实战 什么是两步验证? 大家应该对两步验证都熟悉吧?如苹果有自带的两步验证策略,防止用户账号密码被盗而锁定手机进行敲诈,这种例子屡见 ...

  6. loginrequired注解_简单实现一个登录验证的注解来保护私有资源

    背景 自定义注解标注受保护的资源访问,当要访问的url被标注了@LoginRequied的时候就变成了一个受保护的资源,需要用户登录或者更进一步需要用户拥有某个权限才能操作.本项目使用的springb ...

  7. java spring 登录验证_浅析Spring Security登录验证流程源码

    一.登录认证基于过滤器链 Spring Security的登录验证流程核心就是过滤器链.当一个请求到达时按照过滤器链的顺序依次进行处理,通过所有过滤器链的验证,就可以访问API接口了. SpringS ...

  8. JavaWeb简单的单点登录、验证码校验功能实现

    前言 最近项目刚刚告一段落,后期有时间会慢慢分解整理出来给大家分享.本文主要提供思路和核心代码,建立在有一定后台基础读者上.(相信没有基础的同学只要认真细读也是可以理解的) 技术原理 1.单点登录(S ...

  9. 【Unity】Firebase-Google登录身份验证功能接入流程

    思路:接入sdk相关Android接口以aar形式提供给Unity使用 目录 一.创建Firebase项目工程 二.创建Android工程 接入Android接口 三.Unity工程配置 参考文档:h ...

最新文章

  1. c++函数overload 的歧义匹配
  2. 生活问题 | 对华为畅玩手机5X进行升级
  3. 数据中台(四)数栈,企业级一站式数据中台PaaS
  4. linux 内核裁剪的具体过程和方法,Linux内核裁剪的具体过程和方法
  5. 通用的日志处理类(分享)
  6. ado.net Oracle中一次执行多条sql语句
  7. python语言的语法_Python语言学习系列------基础语法(一)
  8. 详解云安全攻防模型,这些攻击战略和战术越早知道越好!
  9. jquery中的过滤filter not的用法以及可以添加this
  10. VMware Workstation 12激活码
  11. 【STM32学习笔记——WIFI模块】
  12. • 硕士论文查重原理与快速通过的七大方法(转载)
  13. html教程:网页字体的设置
  14. mysql数据模型三要素_数据模型的作用及三要素是什么?
  15. Android butterknife黄油刀使用大全
  16. python乘车费用 青少年编程电子学会python编程等级考试二级真题解析2020年6月
  17. NYOJ 925 国王的烦恼
  18. 大数据的数据科学与关键技术是什么?
  19. 经济的1000+篇文章总结
  20. Nginx+Tomcat服务器负载均衡实践方案

热门文章

  1. win10 服务器远程连接数量限制修改
  2. 【物联网】华为云物联网平台-基于C示例代码的快速体验
  3. 您的Android手机可以充当盖革计数器的双重职责
  4. 【IT行业常见缩写】IT行业常见缩写(程序员专用)
  5. Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败(转载)...
  6. Go 程序设计语言书中 源码
  7. 无法启用win安全中心服务器,win10提示“无法启动安全中心服务”怎么办-解决win10提示“无法启动安全中心服务”的方法 - 河东软件园...
  8. php占用服务器内存,php-fpm占用cpu和内存过高100% 解决办法
  9. JS回调函数——简单易懂有实例
  10. IDEA的主题样式20种