Skip to content
  • Archives
  • Buy Adspace
  • Contact me
  • Hide Ads for Premium Members

zen of coding

holistic web development

  • Archives
  • Buy Adspace
  • Contact me
  • Hide Ads for Premium Members
  • Home
  • 2009
  • May
  • 7
  • Don't cache my form (and session) data!
CakePHP

Don't cache my form (and session) data!

May 7, 2009 vladko

This is probably overly simple, but I felt like posting something anyway…

Every once in a while you hear a complaint that you’ve got some sort of login (account), then a user actually logs out, and then yet, hits the back button in their browser… and, whoopty-doo, they get back to see the account information. Which, of course, comes from the cached browser page.

First, let’s take a decent web-app, like facebook or gmail and see what happens…

Login, logout, click “Back” and we are not going to see our cached page from the browser. Instead we get redirected back to the login page (or whatever you decide for your specific app).

So, what’s their solution?
Nothing special, really, just sending the right headers via PHP to let the browser know not to cache any account pages. (Sure there are other implications involved, so you might consider a redirect while doing the logout).

Let’s try and do it the cake-way…

Say, we have a purchase something page, and the user fills out the form and then gets a “thank you” (or confirmation) page.
If the they click “back” in the browser we do not want to re-display the purchase form. Rather we’d have some page, that would say something like “Hey, you’ve just made the purchase. Don’t be a moron and click back to have your credit card charged twice, but if you really wish to… please, click here and do another purchase”.

First, we’ll disable caching for the purchase page:

function beforeFilter() {
     if($this->action == 'purchase') {
         $this->disableCache();
     }
}

Yep, $this->disableCache() will tell cake to send the right header in order to prevent the page from caching.

Surely, we’ll have a “purchase” action in our Controller (let’s say Users Controller).

function purchase( $id = null, $purchased = null) {
     if(!empty($this->data)) {
         //write whatever we need to our DB and perhaps talk to the payment method processor.
         //also, just in case, let's write something to the user's session
         //... and do redirect to the "thank you" page

         $this->Session->write('Purchased', true);
     }
     else {
        //and now we do our check for the silly user who tried to click "back"
        if($this->Session->check('Purchased')) {
            $this->render('silly_user_you_have_already_made_a_purchase');
        }
     }

}

I hope you see how this all works, based on the rather simple code above.

To sum things up…

  1. We ensure that purchase form is not cached
  2. When the purchase is complete we keep a track of that in the session
  3. If the user clicks “back” in the browser we render a different view (than the purchase form) based on the value in the session
  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Reddit (Opens in new window) Reddit
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Email a link to a friend (Opens in new window) Email

Like this:

Like Loading…

Related

Related Posts

php mvc frameworks and microservices
CakePHP Design Docker Frameworks Laravel Lumen Microservices MVC Symfony Zend

PHP MVC Frameworks preview of 2018 (Phalcon 3, Symfony 4, Laravel 5.x and others)

December 31, 2017December 5, 2020 vladko
CakePHP Devops Docker Microservices MVC Open source

The State of PHP MVC Frameworks in 2017 (Laravel, Symfony, CodeIgniter, CakePHP, Zend)

February 27, 2017January 11, 2018 vladko

Post navigation

Previous: Happy cinco de mayo and 1 year anniversary
Next: Send your database on vacation by using CakePHP + Memcached

New Posts

  • I don’t bother with “whiteboard interviews”.
  • PHP MVC Frameworks preview of 2018 (Phalcon 3, Symfony 4, Laravel 5.x and others)
  • Case study: building a better search with Algolia and Lumen PHP
  • The State of PHP MVC Frameworks in 2017 (Laravel, Symfony, CodeIgniter, CakePHP, Zend)
  • The great PHP MVC Framework Showdown of 2016 – (CakePHP 3 vs Symfony 2 vs Laravel 5 vs Zend 2)

Top Posts & Pages

  • Book review: CakePHP Application Development
  • Case study: building a better search with Algolia and Lumen PHP
May 2009
M T W T F S S
 123
45678910
11121314151617
18192021222324
25262728293031
« Apr   Jun »
All Rights Reserved 2024.
Proudly powered by WordPress | Theme: Fairy by Candid Themes.
%d