SimpleSAML\Utils\HTTP::redirect PHP Method

redirect() private static method

This function will use the "HTTP 303 See Other" redirection if the current request used the POST method and the HTTP version is 1.1. Otherwise, a "HTTP 302 Found" redirection will be used. The function will also generate a simple web page with a clickable link to the target page.
Author: Olav Morken, UNINETT AS ([email protected])
Author: Mads Freek Petersen
Author: Jaime Perez, UNINETT AS ([email protected])
private static redirect ( string $url, string[] $parameters = [] ) : void
$url string The 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 string[] An 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.
return void This function never returns.
    private static function redirect($url, $parameters = array())
    {
        if (!is_string($url) || empty($url) || !is_array($parameters)) {
            throw new \InvalidArgumentException('Invalid input parameters.');
        }
        if (!empty($parameters)) {
            $url = self::addURLParameters($url, $parameters);
        }
        /* 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;
        }
        if (strlen($url) > 2048) {
            Logger::warning('Redirecting to a URL longer than 2048 bytes.');
        }
        if (!headers_sent()) {
            // set the location header
            header('Location: ' . $url, true, $code);
            // disable caching of this response
            header('Pragma: no-cache');
            header('Cache-Control: no-cache, must-revalidate');
        }
        // show a minimal web page with a clickable link to the URL
        echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
        echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"';
        echo ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' . "\n";
        echo '<html xmlns="http://www.w3.org/1999/xhtml">' . "\n";
        echo "  <head>\n";
        echo '    <meta http-equiv="content-type" content="text/html; charset=utf-8">' . "\n";
        echo "    <title>Redirect</title>\n";
        echo "  </head>\n";
        echo "  <body onload=\"window.location.replace('" . htmlspecialchars($url) . "');\">\n";
        echo "    <h1>Redirect</h1>\n";
        echo '      <p>You were redirected to: <a id="redirlink" href="' . htmlspecialchars($url) . '">';
        echo htmlspecialchars($url) . "</a>\n";
        echo '        <script type="text/javascript">document.getElementById("redirlink").focus();</script>' . "\n";
        echo "      </p>\n";
        echo "  </body>\n";
        echo '</html>';
        // end script execution
        exit;
    }