Laravel : I click on logout button and successfully done but when i click on back button - laravel-5.8

I am not able to flush cache after logout in laravel.
On logout I have done lots things but nothing is working for me . I am not able to find a way in laravel to flush cache on my logout.
return redirect(\URL::previous());
return redirect(\URL::previous());
also tried Clouser class

This Code might help you.
public function logout(Request $request) {
header("cache-Control: no-store, no-cache, must-revalidate");
header("cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
Session::flush();
$request->session()->regenerate();
Session::flash('succ_message', 'Logged out Successfully');
return redirect('signin');
}
For more details please click on this Link.

Related

LARAVEL 8 - redirect in lock page after session expired

I'm new to Laravel and I have a problem with the redirect after the session expired.
I set the session_lifetime at 120.
How can I delete the session after it's expired?
Now, after 120 minutes, when clicking on a link inside the website it redirects to login page, but I tried to redirect to the lock screen page with the user details.
App\Exception\Handler
protected function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson()
? response()->json(['message' => 'Unauthenticated.'], 401)
: redirect()->guest(route('auth/lockscreen/'.Auth::id()));
}
Route/Web
Route::get('auth/lockscreen/{id}', 'App\Http\Controllers\UserController#lockscreen')
->name('auth/lockscreen');
Thanks in advance!
thanks for answer.
Here there is a web.php (routes/web.php)
Route::middleware(['auth'])->group(function() {
Route::get('/todos', 'App\Http\Controllers\TodoController#index')->name('todos');
Route::get('/todo/create', 'App\Http\Controllers\TodoController#create')->name('todo/create');
Route::get('todo/edit/{id}', 'App\Http\Controllers\TodoController#edit')->name('todo/edit');
Route::post('todo/update/{id}', 'App\Http\Controllers\TodoController#update')->name('todo/update');
Route::post('todo/store', 'App\Http\Controllers\TodoController#store')->name('todo/store');
Route::delete('/todo/delete/{todo}', 'App\Http\Controllers\TodoController#destroy')->name('todo/delete');
Route::get('auth/lockscreen/{id}', 'App\Http\Controllers\UserController#lockscreen')->name('auth/lockscreen');
Route::post('auth/confirm_p/{id}', 'App\Http\Controllers\UserController#confirm_p')->name('auth/confirm_p');
Route::get('/todo/category', 'App\Http\Controllers\TodoCategoryController#index')->name('todo/category');
Route::post('/todo/category/store', 'App\Http\Controllers\TodoCategoryController#store')->name('todo/category/store');
Route::get('/todo/category/create', 'App\Http\Controllers\TodoCategoryController#create')->name('todo/category/create');
Route::get('/todo/category/edit/{id}', 'App\Http\Controllers\TodoCategoryController#edit')->name('todo/category/edit');
Route::delete('/todo/category/delete/{id}', 'App\Http\Controllers\TodoCategoryController#destroy')->name('todo/category/delete');
});
here the lockscreen method inside models User.php
public function lockscreen($id) {
$user = Auth::user();
return view('auth.lockscreen.'$id)->with('user', $user);
}
I don't understand if the unauthenticated function provide to redirect user... Thanks

Laravel API works locally but not on hosting server

These are my routes:
Route::post('get_client', 'API\ClientController#ShowClient');
Route::post('new_loans', 'API\LoanController#ShowNewLoans');
Route::post('new_loans/{BR_ID}', 'API\LoanController#ShowNewLoans');
get_client, but works locally on my personal computer:
public function ShowClient(Request $request) {
return response()->json(['message' => 'Okay'], 200);
}
Below works perfectly locally and on the server:
public function ShowNewLoans($BR_ID = null) {
if ($BR_ID == null) {
$newLoans = Loans::where([
['APPLICATION_NO', NULL],
['OL_TEMP_APP_NO', '!=', NULL],
['ActivityListID', '!=', 5]
])->get();
} else {
$newLoans = Loans::where([
['APPLICATION_NO', NULL],
['OL_TEMP_APP_NO', '!=', NULL],
['ActivityListID', '!=', 5],
['BR_ID', $BR_ID]
])->get();
}
if ($newLoans->isEmpty()) {
return response()->json(["message" => "There are no online new loan applications found."], 204);
} else {
return response()->json($newLoans, 200);
}
}
What am I missing here? They both use the same namespace. The codes work perfectly in my computer but when its on my hosting server (Amazon EC2) it returns an error 500. These are the headers from postman:
Date
Thu, 21 May 2020 08:16:22 GMT
Server
Apache/2.4.41 (Amazon) OpenSSL/1.0.2k-fips PHP/7.3.15
X-Powered-By
PHP/7.3.15
Cache-Control
no-cache, private
Content-Length
1750
Connection
close
Content-Type
text/html; charset=UTF-8

ERR_CACHE_MISS error in codeigniter when click backbutton

I have created a small web application using codeigniter.But sometimes when I click back button this error message display. What Should Ido to avoid this matter?
Confirm Form Resubmission
This webpage requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed.
Press the reload button to resubmit the data needed to load the page.
ERR_CACHE_MISS
This is part of my main controller
<?php
ob_start();
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url','html');
$this->load->helper('html');
$this->load->database();
$this->load->library('form_validation');
$this->load->model('Login_model');
$this->load->model('Select');
$this->load->model('ProjectEmployees');
$this->load->model('Employee_model');
$this->load->model('Welcome_model','welcome');
$this->load->model('Welcome_model4','welcome4');
$this->load->model('Welcome_modelPI','welcomePI');
}
function index()
{
$this->load->view('login_view');
}
function logout()
{
// destroy session
$data = array('login' => '', 'uname' => '', 'uid' => '');
$this->session->unset_userdata($data);
$this->session->sess_destroy();
redirect('Home/index');
}
public function MyHome()
{
$this->load->view('template/navigation');
$this->load->view('template/sideNav');
$this->load->view('template/header');
$this->load->view('profile_view2');
$this->load->view('template/footer');
}
It's because you Expiring the Page & not sending the Cache Header to Browser. That's why Browser not Caching those pages and while you hitting back Browser displaying ERR_CACHE_MISS instead of your html view..
ERR_CACHE_MISS (Screen):
Make sure you are not Sending Expire Cache Header
Make sure your page not prevent by Browser Cache
Check your Response Header in Browser Net Panel, to See what Header
being passed by Server
Check if your are using $this->output->set_header() in your
code/controller/hook.
Here are some CodeIgniter Cache Expire Header Code Example..
$this->output->set_header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
$this->output->set_header('Pragma: no-cache');
$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

Setting Cookie in Laravel Custom Middleware

I want to set a cookie in a custom Laravel middleware. The idea is to set the cookie value any time a user visits my website through any landing page.
So what I did was I created a middleware named UUIDMiddleware. I am using this middleware along with web middleware in my routes. Below is its code from the middleware.
if($request->hasCookie('uuid'))
{
return $next($request);
}
else
{
$uuid = Uuid::generate();
$response = new Response();
return $response->withCookie(cookie()->forever('uuid', $uuid));
}
As you can see I am checking if cookie exists. If not, I am passing control to next request.
The problem is when setting a cookie using return $response, I cannot pass control to next request. How do I resolve this?
What happens in this case is if a cookie is not set, it sets a cookie and a blank screen shows up. If I refresh, I see the website with a cookie set.
There has to be a way to set cookie using middleware in the right way. How do I do it?
The response object in middleware is returned by the $next closure, so you could do something like this:
if($request->hasCookie('uuid')) {
return $next($request);
}
$uuid = Uuid::generate();
return $next($request)
->withCookie(cookie()->forever('uuid', $uuid));
If you're using Laravel 5.8 and like me you want a solution that allows you set the expiration time of the cookie instead of setting it to forever, you could also do something like this in the middleware:
public function handle($request, Closure $next)
{
$time = time() + 60 * 60 * 24; //One day
$res = $next($request);
return $res->cookie('cookie_name', $cookieValue, $time, "/");
}
This would set the cookie when the controller is done with the request and is returning a response.

How to stop logged in user from clicking back button and going to login page

So I noticed the fact that after a user logs in then clicks on the back button it returns them back to the login page. How do I make it so that it doesn't do that?? The logout version of the code I found to stop clicking the back button to go to secured page from happening doesn't seem to apply to the login problem I'm experiencing.
Here's the code I added to constructor and it seems to have stopped logout from using back to return to secured page:
parent::__construct();
$this->output->set_header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
$this->output->set_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
$this->output->set_header('Cache-Control: post-check=0, pre-check=0', FALSE);
$this->output->set_header('Pragma: no-cache');
$this->load->helper('url');
$this->load->driver('cache');
}
Also added a $this->cache->clean() at logout
Here is the sample codes or you, In your login controller check the exist login session.
function __construct() {
parent::__construct ();
$valid_login = $this->session->userdata ( 'valid_login' );
if ($valid_login == 1) {
redirect ( base_url('homeController') );
}
return true;
}
public function checklogin() {
$name = $this->input->post ( 'username' );
$pass = $this->input->post ( 'password' );
$this->load->model ( 'loginModel' );
if ($this->loginModel->login ( $name, $pass )) {
$this->session->set_userdata ( 'username', $name );
$this->session->set_userdata ( 'valid_login', 1 );
return true;
} else {
return false;
}
}
Now in the home controller check for the login session else log out and redirect to the login page like bellow.
function __construct() {
parent::__construct ();
$valid_login = $this->session->userdata ( 'valid_login' );
if ($valid_login == "")
redirect ( 'loginController' );
}
you could try this..
function __construct()
{
parent::__construct();
//$this->load->model('model_session');
$this->load->model('model_vanshaval');
$this->load->model('model_session');
if($this->model_session->check_logged()==FALSE){
redirect('welcome');
}
you could write this constructor in welcome controller to check whether user is logged in or not..

Resources