OneLogin_Saml2_Utils::redirect PHP Method

redirect() public static method

Executes a redirection to the provided url (or return the target url).
public static redirect ( string $url, array $parameters = [], boolean $stay = false ) : string | null
$url string The target url
$parameters array Extra parameters to be passed as part of the url
$stay boolean True if we want to stay (returns the url string) False to redirect
return string | null $url
    public static function redirect($url, $parameters = array(), $stay = false)
    {
        assert('is_string($url)');
        assert('is_array($parameters)');
        if (substr($url, 0, 1) === '/') {
            $url = self::getSelfURLhost() . $url;
        }
        /* Verify that the URL is to a http or https site. */
        if (!preg_match('@^https?:\\/\\/@i', $url)) {
            throw new OneLogin_Saml2_Error('Redirect to invalid URL: ' . $url, OneLogin_Saml2_Error::REDIRECT_INVALID_URL);
        }
        /* Add encoded parameters */
        if (strpos($url, '?') === false) {
            $paramPrefix = '?';
        } else {
            $paramPrefix = '&';
        }
        foreach ($parameters as $name => $value) {
            if ($value === null) {
                $param = urlencode($name);
            } else {
                if (is_array($value)) {
                    $param = "";
                    foreach ($value as $val) {
                        $param .= urlencode($name) . "[]=" . urlencode($val) . '&';
                    }
                    if (!empty($param)) {
                        $param = substr($param, 0, -1);
                    }
                } else {
                    $param = urlencode($name) . '=' . urlencode($value);
                }
            }
            if (!empty($param)) {
                $url .= $paramPrefix . $param;
                $paramPrefix = '&';
            }
        }
        if ($stay) {
            return $url;
        }
        header('Pragma: no-cache');
        header('Cache-Control: no-cache, must-revalidate');
        header('Location: ' . $url);
        exit;
    }

Usage Example

Example #1
0
 /**
  * Obtains the SSO URL containing the AuthRequest
  * message deflated.
  *
  * @param OneLogin_Saml2_Settings $settings Settings
  */
 public function getRedirectUrl($returnTo = null)
 {
     $settings = $this->auth->getSettings();
     $authnRequest = new OneLogin_Saml2_AuthnRequest($settings);
     $parameters = array('SAMLRequest' => $authnRequest->getRequest());
     if (!empty($returnTo)) {
         $parameters['RelayState'] = $returnTo;
     } else {
         $parameters['RelayState'] = OneLogin_Saml2_Utils::getSelfURLNoQuery();
     }
     $url = OneLogin_Saml2_Utils::redirect($this->auth->getSSOurl(), $parameters, true);
     return $url;
 }
All Usage Examples Of OneLogin_Saml2_Utils::redirect