Example #1
0
 /**
  * Performs a HTTP redirection to specified URL with additional data.
  * It may generate redirected request using GET or POST HTTP method.
  * The function never returns.
  *
  * @param string $url URL to redirect to
  * @param array $params additional variable/value pairs to send
  * @param Response $response
  * @param string $method redirection method ('GET' or 'POST')
  */
 public static function redirect($url, $params = null, Response $response = null, $method = 'GET')
 {
     $url = self::absoluteUrl($url);
     $body = "";
     if (null === $response) {
         $response = new Response();
     }
     if ($method == 'POST') {
         $body = "<html><body onLoad=\"document.forms[0].submit();\">\n";
         $body .= "<form method=\"POST\" action=\"{$url}\">\n";
         if (is_array($params) && count($params) > 0) {
             foreach ($params as $key => $value) {
                 $body .= '<input type="hidden" name="' . $key . '" value="' . $value . "\">\n";
             }
         }
         $body .= "<input type=\"submit\" value=\"Continue OpenID transaction\">\n";
         $body .= "</form></body></html>\n";
     } else {
         if (is_array($params) && count($params) > 0) {
             if (strpos($url, '?') === false) {
                 $url .= '?' . self::paramsToQuery($params);
             } else {
                 $url .= '&' . self::paramsToQuery($params);
             }
         }
     }
     if (!empty($body)) {
         $response->setContent($body);
     } elseif (headers_sent()) {
         $response->setContent("<script language=\"JavaScript\"" . " type=\"text/javascript\">window.location='{$url}';" . "</script>");
     }
     $response->setStatusCode(302);
     $response->headers()->addHeaderLine('Location', $url);
     if (!headers_sent()) {
         header($response->renderResponseLine());
         foreach ($response->headers() as $header) {
             header($header->toString());
         }
     }
     echo $response->getBody();
     if (self::$exitOnRedirect) {
         exit;
     }
 }