Redirect with POST parameters

I came across a requirement recently where I wanted to redirect a user to a new page and pass along a few parameters. The Zend Framework redirector helper gotoSimple() function takes an array of parameters as the 4th argument, so this seemed fairly easy at first. The parameters however are simply appended to the end of the redirect URL. I didn't want this to happen as I was redirecting users to a dynamic error page. So, naively, I stared looking for a way to force the redirect to use POST parameters...

This of course is not possible if you think about how a redirect works. The Zend gotoSimple function simply sets the HTTP location header to the specified action-controller URL, and appends the parameters to the URL if supplied. This header is sent to the client as a simple string. When a client browser encounters the location header in a response, it performs a GET on the location URL. There is no way to tell the client to perform a POST instead (the URL is just a simple query string anyway).

The simple workaround to this is to use session variables. That is, before sending the redirect headers, stick all your POST parameters in a session token, then on the redirect page logic, check the session and retrieve the parameters! This way you're still doing a redirect (and the client is still accessing your page via GET), but the parameters never leave the server.

Comments

  1. I've had the same problem, useful post, thankyou!

    ReplyDelete
  2. This is cool -- however if your code relies on $request->isPost() to distinguish POSTs from GETs then what?

    ReplyDelete
  3. Update your code so it checks $request->isPost() || $_SESSION['post_token'] instead? Don't know, depends on your site architecture.

    ReplyDelete
  4. You can always use Zend_Controller_Action::_forward($action, $controller, $module, array());
    It's good enough!

    ReplyDelete
  5. I've had the same problem, useful post, thankyou!

    ReplyDelete

Post a Comment

Popular posts from this blog

Wkhtmltopdf font and sizing issues

Import Google Contacts to Nokia PC Suite

Can't delete last blank page from Word