NYCPHP Meetup

NYPHP.org

[nycphp-talk] POSTDATA and back/resubmit revisited

Paul M Jones pmjones88 at gmail.com
Mon Jun 11 14:12:12 EDT 2007


On Jun 11, 2007, at 11:04 AM, Cliff Hirsch wrote:

> From the wiki, this describes it in depth:
>     http://www.theserverside.com/tt/articles/article.tss? 
> l=RedirectAfterPost
>
> It also has a brief explanation of 302 versus 303 codes.

After reading that theserververside.com entry, it seems like we've  
been doing this in Solar (framework for PHP5) for a little while  
now.  Essentially, after processing a form, you call $this- 
 >_redirectNoCache('controller/action') and you shouldn't get any re- 
POST troubles.

Boring code from the page-controller follows.

   <http://solarphp.com/svn/trunk/Solar/Controller/Page.php>

     /**
      *
      * Redirects to another page and action after disabling HTTP  
caching.
      *
      * The _redirect() method is often called after a successful POST
      * operation, to show a "success" or "edit" page. In such cases,  
clicking
      * clicking "back" or "reload" will generate a warning in the
      * browser allowing for a possible re-POST if the user clicks OK.
      * Typically this is not what you want.
      *
      * In those cases, use _redirectNoCache() to turn off HTTP  
caching, so
      * that the re-POST warning does not occur.
      *
      * This method sends the following headers before setting Location:
      *
      * {{code: php
      *     header("Cache-Control: no-store, no-cache, must- 
revalidate");
      *     header("Cache-Control: post-check=0, pre-check=0", false);
      *     header("Pragma: no-cache");
      * }}
      *
      * @param Solar_Uri_Action|string $spec The URI to redirect to.
      *
      * @param int|string $code The HTTP status code to redirect  
with; default
      * is '303 See Other'.
      *
      * @return void
      *
      */
     protected function _redirectNoCache($spec, $code = 303)
     {
         // reset cache-control
         $this->_response->setHeader(
             'Cache-Control',
             'no-store, no-cache, must-revalidate'
         );

         // append cache-control
         $this->_response->setHeader(
             'Cache-Control',
             'post-check=0, pre-check=0',
             false
         );

         // reset pragma header
         $this->_response->setHeader('Pragma', 'no-cache');

         // continue with redirection
         return $this->_redirect($spec, $code);
     }


--

Paul M. Jones  <http://paul-m-jones.com>

Solar: Simple Object Library and Application Repository
for PHP5.   <http://solarphp.com>

Savant: The simple, elegant, and powerful solution for
templates in PHP.   <http://phpsavant.com>





More information about the talk mailing list