TokenMismatchException in VerifyCsrfToken.php line 53 in Laravel 5 - php

I am using HTML pages for UI purpose in laravel and when I try to login then it shows me an error page "TokenMismatchException in VerifyCsrfToken.php line 53:".
I am getting this error while login as well as registration.
I have the login.html page as follows:
<div class="container">
<form method="POST" action="login_display" accept-charset="UTF-8">
<h1>Login</h1>
<hr>
<div class="form-group">
<label for="email">Email ID :</label>
<input class="form-control" name="email" type="text" id="email" placeholder="Please enter email here">
</div>
<div class="form-group">
<label for="password">Password :</label>
<input class="form-control" name="password" type="password" value="" id="password" placeholder="Please enter password here">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Login">
</div>
</form>
</div>

Try adding, <input type="hidden" name="_token" value="{{ csrf_token() }}"> to your form. Laravel uses this token to check that the form submission was valid.

I tried by adding following line to the form:
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
and this time it works well and I didn't get the TokenMismatchException error afterwards as well.

Related

How can I extract the text of HTML5 Constraint validation using PHP + Selenium

When incorrect value is entered in textbox next hint is appeared:
I have tried approach like for Java + Selenium . But it did not work for PHP. I did not get validation error message:
HTML:
<form action="/login" method="post" class="form-horizontal"> <input type="hidden">
<div class="form-group form-group-withLabel">
<input type="email" name="_username" id="inputEmail" required="" autofocus="" autocomplete="off">
<label for="inputEmail" class="inner-label">Email address</label>
</div>
<div class="form-group form-group-withLabel">
<input type="password" name="_password" id="inputPassword" placeholder="Password" required="">
<label class="inner-label" for="inputPassword">Password</label>
</div>
</form>
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='inputEmail' and #name='_username']"))).getAttribute("validationMessage"));
$this->client->wait()->until(WebDriverExpectedCondition::elementToBeClickable($by));
$attr = $this->client->findElement($by)->getAttribute('validationMessage');

Basic HTML Link Send PHP Variable

This is my first go at trying to create a secure login feature.
Right now I have a mysql database storing a few usernames and passwords.
I also have a bootstrap template for a login page.
Here is some of the html:
<form>
<div class="form-group">
<div class="form-label-group">
<input type="email" id="inputEmail" class="form-control" placeholder="Email address" required="required" autofocus="autofocus">
<label for="inputEmail">Email address</label>
</div>
</div>
<div class="form-group">
<div class="form-label-group">
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required="required">
<label for="inputPassword">Password</label>
</div>
</div>
<a class="btn btn-primary btn-block" href="connect.php" method="post">Login</a>
</form>
I added the method and changed the action for the login button/link.
I would like to be able to click the login link and have the input values sent to an external php program connect.php shown below:
<!DOCTYPE html>
<html lang="en">
<body>
<div>
<?php
$user = $_POST["inputEmail"];
$pass = $_POST["inputPassword"];
echo "<h1>Hello: ".$user." also:".$pass"</h1>";
?>
</div>
</body>
</html>
Once I can get the values to send to the external script, I can then start to check the values against those in my database.
Solutions I have seen are mostly dealing with the form action and not a link. Those that are dealing with a link use get and hard code the values being sent rather than input.
EDIT 1:
I realized my html page was launching outside of my server. I made some changes to my html:
<form method="post">
<div class="form-group">
<div class="form-label-group">
<input type="email" id="inputEmail" name="inputEmail" class="form-control" placeholder="Email address" required="required" autofocus="autofocus">
<label for="inputEmail">Email address</label>
</div>
</div>
<div class="form-group">
<div class="form-label-group">
<input type="password" id="inputPassword" name="inputPassword" class="form-control" placeholder="Password" required="required">
<label for="inputPassword">Password</label>
</div>
</div>
<a class="btn btn-primary btn-block" href="connect.php" method="post" name="submit">Login</a>
</form>
after these changes its telling me my inputs are Unidentified index's
SOLUTION
change submit link to submit button & make sure to run on a server
<form action="connect.php" method="POST">
<div class="form-group">
<div class="form-label-group">
<input type="email" id="inputEmail" name="inputEmail" class="form-control" placeholder="Email address" required="required" autofocus="autofocus">
<label for="inputEmail">Email address</label>
</div>
</div>
<div class="form-group">
<div class="form-label-group">
<input type="password" id="inputPassword" name="inputPassword" class="form-control" placeholder="Password" required="required">
<label for="inputPassword">Password</label>
</div>
</div>
<button class="btn btn-primary btn-block" name="submit" type="submit">Login</button>
</form>

laravel, auto-fill form fields from URL parameters

I'm trying to figure out how to properly set URL parameters to auto fill form fields in laravel
The form works and properly submits data but I have a URL like testsite.com/register?email=email#test.com&password=dK94*DFj
and I'd like to auto fill the form with the email and password from the URL
the form:
<form method="POST" action="{{ route('auth.register') }}">
<div class="reg-card reg-margin-bottom">
<div class="reg-card-content">
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" class="form-control" id="email" name="email" required value="{{ $invitation->email }}" />
</div>
<div class="form-group">
<label for="password">New Password</label>
<input type="password" id="password" class="form-control" required name="password" />
</div>
</div>
</div>
<div class="reg-card">
<div class="reg-card-content">
<button type="submit" class="btn btn-primary btn-block">Create account</button>
</div>
</div>
</form>
Can I set this so that, as long as the params exist in the URL, then they autofill and disable those fields?
You can get it by using
{{ request()->email }} and {{ request()->password }}
However, passing password in the url is no a good idea though!
if you want a cleaner URL, uou can create a route for the URl register.
Route::get('/register/{email?}/{hash?}',function($email,$hash){
return view("viewFile",['email'=>$email,'password'=>$hash]);
});
In your view
<form method="POST" action="{{ route('auth.register') }}">
<div class="reg-card reg-margin-bottom">
<div class="reg-card-content">
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" class="form-control" id="email" name="email" required value="{{ $email }}" />
</div>
<div class="form-group">
<label for="password">New Password</label>
<input type="password" id="password" class="form-control" required name="password" value="{{$password}}" />
</div>
</div>
</div>
<div class="reg-card">
<div class="reg-card-content">
<button type="submit" class="btn btn-primary btn-block">Create account</button>
</div>
</div>
</form>
I don't recommend you to pass a password in the url, but if it is a hash to validate that the email is the correct one, is a good idea I do it when I want to verified that the info is not changed by the user. Also this will need to hash the email and compare the hash with the one on the url.

Without AJAX call simple post method is not working in Laravel, I'm not using AJAX simple form

route ::post()is not working fine on the other hand route::get() is working fine and other method is working, just post method is not working
Route::get('/', 'HomeController#index');
this route is working:
Route::post('/posts', 'Cdesignation#index');
but this route is not working
use same form:
<form action="/form/process" method="POST">
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd">
</div>
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div> <input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<input type="submit" class="btn btn-default" value="Submit">
</form>
how to resolve it
if you notice that the route in the form is /form/process it should be /posts

TokenMismatchException in VerifyCsrfToken.php line 46 ocassionally showing

I already set the token in the form:
<form action="{{ route('user.store') }}" method="post">
<input type="hidden" name="_token" value="{!! csrf_token() !!}">
<legend>Agregar nuevo usuario.</legend>
<div class="form-group">
<label>Código empresa</label>
<input type="number" class="form-control input-sm" name="enterprise" id="enterprise">
</div>
<div class="form-group">
<label>Nombre</label>
<input type="text" class="form-control input-sm" name="name" id="name">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control input-sm" name="email" id="email">
</div>
<div class="form-group">
<label>Usuario</label>
<input type="text" class="form-control input-sm" name="username" id="username">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control input-sm" name="password" id="password">
</div>
<div class="form-group">
<label class="checkbox-inline">
<input type="checkbox" name="create_content" id="create_content"> Crea contenido
</label>
<label class="checkbox-inline">
<input type="checkbox" name="active" id="active"> Activo
</label>
</div>
<button type="submit" class="btn btn-sm btn-primary" id="btn_Crear">Create</button>
</form>
Occasionally I'm receiving the TokenmismathException, and I'm not able to post anymore, If I comment out the line //'App\Http\Middleware\VerifyCsrfToken', in the Kernel.php file and try to post, it works, And if I uncomment the same line again 'App\Http\Middleware\VerifyCsrfToken',, now I don't receive the TokenmismatchException, until it stops working.
I'm not using ajax
Does anyone know why this is happening.
We had the exact same problem and never found a good solution. We did find a workaround although.
In your .env file, set the Session storage to Redis (yap, you have to install Redis on your server than). This worked for us, never encountered the same problem again.
Note, this works for us, but it of course is not a solution, merely a work-around until someone found the right solution.

Categories