From 4760be6915bf4611898abdee9ce81c14abd8d7f5 Mon Sep 17 00:00:00 2001 From: Olav Morken <olav.morken@uninett.no> Date: Tue, 18 Dec 2007 13:10:42 +0000 Subject: [PATCH] Utilities: Added a redirect function. git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@130 44740490-163a-0410-bde0-09ae8108e29a --- lib/SimpleSAML/Utilities.php | 97 ++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index 741b1691f..8a30040f3 100644 --- a/lib/SimpleSAML/Utilities.php +++ b/lib/SimpleSAML/Utilities.php @@ -237,6 +237,103 @@ class SimpleSAML_Utilities { exit; } + + + /* This function redirects the user to the specified address. + * An optional set of query parameters can be appended by passing + * them in an array. + * + * This function will use the HTTP 303 See Other redirect if the + * current request is a POST request and the HTTP version is HTTP/1.1. + * Otherwise a HTTP 302 Found redirect will be used. + * + * The fuction will also generate a simple web page with a clickable + * link to the target page. + * + * Parameters: + * $url URL we should redirect to. This URL may include + * query parameters. If this URL is a relative URL + * (starting with '/'), then it will be turned into an + * absolute URL by prefixing it with the absolute URL + * to the root of the website. + * $parameters Array with extra query string parameters which should + * be appended to the URL. The name of the parameter is + * the array index. The value of the parameter is the + * value stored in the index. Both the name and the value + * will be urlencoded. If the value is NULL, then the + * parameter will be encoded as just the name, without a + * value. + * + * Returns: + * This function never returns. + */ + public static function redirect($url, $parameters = array()) { + assert(is_string($url)); + assert(strlen($url) > 0); + assert(is_array($parameters)); + + /* Check for relative URL. */ + if(substr($url, 0, 1) === '/') { + /* Prefix the URL with the url to the root of the + * website. + */ + $url = self::selfURLhost() . $url; + } + + /* Determine which prefix we should put before the first + * parameter. + */ + if(strpos($url, '?') === FALSE) { + $paramPrefix = '?'; + } else { + $paramPrefix = '&'; + } + + /* Iterate over the parameters and append them to the query + * string. + */ + foreach($parameters as $name => $value) { + + /* Encode the parameter. */ + if($value === NULL) { + $param = urlencode($name); + } else { + $param = urlencode($name) . '=' . + urlencode($value); + } + + /* Append the parameter to the query string. */ + $url .= $paramPrefix . $param; + + /* Every following parameter is guaranteed to follow + * another parameter. Therefore we use the '&' prefix. + */ + $paramPrefix = '&'; + } + + + /* Set the HTTP result code. This is either 303 See Other or + * 302 Found. HTTP 303 See Other is sent if the HTTP version + * is HTTP/1.1 and the request type was a POST request. + */ + if($_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1' && + $_SERVER['REQUEST_METHOD'] === 'POST') { + $code = 303; + } else { + $code = 302; + } + + /* Set the location header. */ + header('Location: ' . $url, TRUE, $code); + + /* Show a minimal web page with a clickable link to the URL. */ + echo '<html><body><h1>Redirect</h1>You were redirected to: <a href="' . + htmlspecialchars($url) . '">' . htmlspecialchars($url) + . '</a></body></html>'; + + /* End script execution. */ + exit; + } } ?> \ No newline at end of file -- GitLab