PHP implements the login form to submit CSRF and verification code, form csrf

Source: Internet
Author: User

PHP implements the login form to submit CSRF and verification code, form csrf

1. submit the form and submit it to this page.

(1) The form attribute method is the post method. Modify the route so that it can receive post and get requests.

Route::any('/admin/login','Admin\LoginController@login');

(2) LoginController. php

Modify the login method and return different content based on different requests.

If the request method is get, the login page is returned. If the request is post, verification is performed.

use Illuminate\Support\Facades\Input;public function login(){  if($input = Input::all()){   dd($input);  }else {   return view('admin.login');  }}

Refresh the page. If the following is returned

This is laravel csrf protection. solution:

Display correctly:

2. Check whether the verification code is correct.

(1) LoginController. php, modify the login () method

Public function login () {if ($ input = Input: all () {$ code = new \ Code (); $ _ code = $ code-> get (); if (strtoupper ($ input ['code'])! = $ _ Code) {// The strtoupper () function converts the input letters into uppercase letters. Otherwise, the lower case is incorrect. // back () function returns to the previous request page, return back ()-> with ('msg ', 'verification code error');} else {echo' OK ';}} else {return view ('admin. login ');}}

(2) Modify login. blade. php to get the session

Modify the previous username error:

@if(session('msg'))<p style="color:red">{{session('msg')}}</p>@endif

Verification:

Summary:

1. If a session parameter is required, the route must be written in the middleware.

For example, when the controller uses the back () function, return back ()-> with ('msg ', 'verification code error'); if the route is not in middleware, there is no session, even if you are at the session_start () entry, it is useless and difficult to debug. There is also the _ token that does not generate csrf.

Route: group (['ddleware '=> ['web'], function () {// The Route must be placed in the middleware. Otherwise, sessionRoute cannot be generated :: any ('/admin/login', 'admin \ LoginController @ login'); // The post parameter must be accepted here, so you must use a hybrid Route :: get ('/admin/Code', 'admin \ LoginController @ Code'); Route: get ('/Admin/getcode ', 'admin \ LoginController @ getcode ');});

2. When introducing a third-party class, if this type of file is useful to the session, you need to enter the session_start () file. This is the simplest solution. Otherwise, you will not know $ _ SESSION.

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.