Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I not allow a user to go back after logout in PHP?

Tags:

php

I just wrote a PHP login script, and what I'm trying to accomplish is that when the user click to the log out link, after they log out, regardless clicking the back button of the browser, they cannot access the page.

Here is the logout function:

//Start the Session
session_start();
session_destroy();

header("location:login.php");
exit();

I did place the following code on all the pages, and this seems not do the job:

header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
header ("Pragma: no-cache");

//Start the Session
session_start();

Any suggestions?

like image 528
Ole Media Avatar asked Jun 23 '09 22:06

Ole Media


People also ask

How do I stop PHP from going back after logout?

Here's an easy and quick solution. To the login form tag add target="_blank" which displays content in a different window. Then after logout simply close that window and the back button problem (Safari browser) is solved. Even trying to use the history will not display the page and instead redirect to login page.

How do I restrict someone to go back after logging out?

The HTML Markup of Home page consists of an HTML Anchor link to the Logout page. The Disable Browser Back Button Script is placed in the HEAD section so that User cannot access the Home page using Browser Back button from Logout page. The following is the HTML Markup of Logout.

How do you prevent your browser from going back to the login page once a user is logged in?

You should convert the login screen to a php file - it needs a little bit of server-side logic to check if the user is logged in. It can be the same as your current html file except with a . php extension and this bit of php at the top <? php if(isset($_SESSION['username'])){ header('location:dashboard.

How do I prevent an auto logout SESSION in PHP?

Use password_hash and password_verify instead.


2 Answers

You can't control the workings of the client-side back button on the server. You could destroy the history data using javascript on the client.

The client can completely ignore the no-cache headers.

like image 63
jmucchiello Avatar answered Sep 17 '22 18:09

jmucchiello


Check when the user is logged out if the session global is still set with the correct value.

print_r($_SESSION);

The reason for this is that you are doing a session_destroy and then a header redirect, what happens is that you force a redirect and the destroying of the session isnt written to the server that way.

like image 29
Ólafur Waage Avatar answered Sep 20 '22 18:09

Ólafur Waage