prasad chinthala prasad chinthala - 13 days ago 7
PHP Question

How to display login page with back button after logout?

After logout again if user clicks on the browser back button then the previous page showing again instead of login page. I am using a wordpress method called "wp_logout_url() " as a logout url. can anyone out there please help me in this. Thanks in advance.

Answer

You need to use session variable for login and logout. More Details on PHP Session

Use below code to check for session variable

<?php 
      if(!isset($_SESSION['login'])) : 
      header("Location: login.php");  
?>

When user logout, simply unset the login session variable, and destroy the session as below.

<?php
      unset($_SESSION['login']);  
      session_destroy();  
?>

Also if you want to disable the back button using javascript. I found here something useful for you.

history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});