user2185041
user2185041

Reputation: 99

How to disable cache on logout in CakePHP

I am using cakephp 2.3 version. I need to disable cache only when the user logs out. This is to prevent login in case the back button is pressed.

But for all other scenarios I don't want to disturb caching mechanism.

Right now i use the following function in AppController:

public function beforeRender() {
    $this->response->disableCache();
}

But I doubt that it disables the caching process completely. Please help!

Upvotes: 1

Views: 2078

Answers (2)

A_S
A_S

Reputation: 127

Add this to the header page of your application:

<?php
  header("Cache-Control: no-cache, no-store, must-revalidate"); 
  header("Pragma: no-cache");
  header("Expires: 0");
?>

Upvotes: 1

Sam Delaney
Sam Delaney

Reputation: 1305

I don't believe the user is automatically logged back into your site if they click on the back button after logging out. I'm guessing by your question that when the user does press the back button after logging out, you want them to be forwarded to the login page. Unfortunately, the browser may cache the last page it was on and give the user the illusion that they're still logged in. Rest assured that when they try and do something that requires them to be logged in, the session ID stored in the cookie, won't be recognized and they will be forwarded to the authentication page.

One possible work around is to redirect the user to another controller action after the logout has taken place - this will introduce and additional transition in the browsers history. I don't know what will happen if they click back twice though.

Upvotes: 3

Related Questions