Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

url_helper: redirect() - HTTP/1.1 compliance for redirect after post. #338

Closed
wants to merge 2 commits into from

2 participants

@ktomk

For a more seamless integration with the Post/Redirect/Get (PRG) pattern in CI, see http://en.wikipedia.org/wiki/Post/Redirect/Get .

In HTTP/1.1 on a post request it should be opted for the 303 redirect code.

To have this easily available, the redirect() function inside the url_helper could just take care of that.

@narfbg
Owner

I like this one, and prepared a little more polished patch based on the current version in develop: https://gist.github.com/2942361

Tested it and works fine, but as it's described in the link from the description above - the benefit is that the script can continue its exectuion and what redirect() does is to exit immediately after sending the Location header. So is there a real benefit? Did some research but I couldn't find anything other than that.

@narfbg narfbg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 29, 2011
  1. @ktomk
Commits on Aug 31, 2011
  1. @ktomk

    WS/Identation.

    ktomk authored
This page is out of date. Refresh to see the latest.
Showing with 15 additions and 2 deletions.
  1. +15 −2 system/helpers/url_helper.php
View
17 system/helpers/url_helper.php 100755 → 100644
@@ -532,8 +532,21 @@ function url_title($str, $separator = 'dash', $lowercase = FALSE)
*/
if ( ! function_exists('redirect'))
{
- function redirect($uri = '', $method = 'location', $http_response_code = 302)
+ function redirect($uri = '', $method = 'location', $http_response_code = NULL)
{
+ if (NULL === $http_response_code)
+ {
+ $is_http11_post =
+ (
+ isset ($_SERVER['REQUEST_METHOD'])
+ && 'POST' === $_SERVER['REQUEST_METHOD']
+ && isset ($_SERVER['SERVER_PROTOCOL'])
+ && 'HTTP/1.1' === $_SERVER['SERVER_PROTOCOL']
+ );
+
+ $http_response_code = $is_http11_post ? 303 : 302;
+ }
+
if ( ! preg_match('#^https?://#i', $uri))
{
$uri = site_url($uri);
@@ -595,4 +608,4 @@ function _parse_attributes($attributes, $javascript = FALSE)
/* End of file url_helper.php */
-/* Location: ./system/helpers/url_helper.php */
+/* Location: ./system/helpers/url_helper.php */
Something went wrong with that request. Please try again.