API:sendRedirect

From MODx Wiki
Jump to: navigation, search
 API Function Definition: 
sendRedirect
Versions 
> 0.9.5
Return Values
Success: null
Fail: messageQuit
Data Type 
void
Object Hierarchy 
DocumentParser
void sendRedirect(string $url[, int $count_attempts= 0[, string $type= [, string $responseCode= ]]]);


Send a redirection to the browser, either with META tag or header code. If count_attempts is set to 1 , it will start counting the redirections and stop after 3 redirections. Possible values for $type are:

  • REDIRECT_REFRESH
  • REDIRECT_META
  • REDIRECT_HEADER

If left empty, REDIRECT_HEADER will be used. Optional HTTP code can be send along with the redirection. See the W3C Status Code Definitions

Examples

// Redirect the user to a new web site, and tell HTTP agents that the site has moved permanently
$modx->sendRedirect('http://www.newdomain.com', 0, 'REDIRECT_HEADER', 'HTTP/1.1 301 Moved Permanently');

Related Functions

Function Source

File: manager/includes/document.parser.class.inc.php
Line: 63
function sendRedirect($url, $count_attempts= 0, $type= '', $responseCode= '') {
        if (empty ($url)) {
            return false;
        } else {
            if ($count_attempts == 1) {
                // append the redirect count string to the url
                $currentNumberOfRedirects= isset ($_REQUEST['err']) ? $_REQUEST['err'] : 0;
                if ($currentNumberOfRedirects > 3) {
                    $this->messageQuit('Redirection attempt failed - please ensure the document you\'re trying to redirect to exists. <p>Redirection URL: <i>' . $url . '</i></p>');
                } else {
                    $currentNumberOfRedirects += 1;
                    if (strpos($url, "?") > 0) {
                        $url .= "&err=$currentNumberOfRedirects";
                    } else {
                        $url .= "?err=$currentNumberOfRedirects";
                    }
                }
            }
            if ($type == 'REDIRECT_REFRESH') {
                $header= 'Refresh: 0;URL=' . $url;
            }
            elseif ($type == 'REDIRECT_META') {
                $header= '<META HTTP-EQUIV="Refresh" CONTENT="0; URL=' . $url . '" />';
                echo $header;
                exit;
            }
            elseif ($type == 'REDIRECT_HEADER' || empty ($type)) {
                // check if url has /$base_url 
                global $base_url, $site_url;
                if (substr($url, 0, strlen($base_url)) == $base_url) {
                    // append $site_url to make it work with Location:
                    $url= $site_url . substr($url, strlen($base_url));
                }
                if (strpos($url, "\n") === false) {
                    $header= 'Location: ' . $url;
                } else {
                    $this->messageQuit('No newline allowed in redirect url.');
                }
            }
            if ($responseCode && (strpos($responseCode, '30') !== false)) {
                header($responseCode);
            }
            header($header);
            $this->postProcess();
        }
    }

Notes

Personal tools