API15

JApplication/redirect

From Joomla! Documentation

< API15:JApplication

The "API15" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

Redirect to another URL.


<! removed transclusion to a non-existant red link page >

Syntax[edit]

redirect($url, $msg='', $msgType='message')
Parameter Name Default Value Description
$url $url The URL to redirect to. Can only be http/https URL
$msg $msg An optional message to display on redirect.
$msgType 'message' $msgType An optional message type.

Returns[edit]

none; calls exit().

Defined in[edit]

libraries/joomla/application/application.php

Importing[edit]

jimport( 'joomla.application.application' );

Source Body[edit]

function redirect( $url, $msg='', $msgType='message' )
{
        // check for relative internal links
        if (preg_match( '#^index[2]?.php#', $url )) {
                $url = JURI::base() . $url;
        }

        // Strip out any line breaks
        $url = preg_split("/[\r\n]/", $url);
        $url = $url[0];

        // If we don't start with a http we need to fix this before we proceed
        // We could validly start with something else (e.g. ftp), though this would
        // be unlikely and isn't supported by this API
        if(!preg_match( '#^http#i', $url )) {
                $uri =& JURI::getInstance();
                $prefix = $uri->toString(Array('scheme', 'user', 'pass', 'host', 'port'));
                if($url[0] == '/') {
                        // we just need the prefix since we have a path relative to the root
                        $url = $prefix . $url;
                } else {
                        // its relative to where we are now, so lets add that
                        $parts = explode('/', $uri->toString(Array('path')));
                        array_pop($parts);
                        $path = implode('/',$parts).'/';
                        $url = $prefix . $path . $url;
                }
        }


        // If the message exists, enqueue it
        if (trim( $msg )) {
                $this->enqueueMessage($msg, $msgType);
        }

        // Persist messages if they exist
        if (count($this->_messageQueue))
        {
                $session =& JFactory::getSession();
                $session->set('application.queue', $this->_messageQueue);
        }

        /*
         * If the headers have been sent, then we cannot send an additional location header
         * so we will output a javascript redirect statement.
         */
        if (headers_sent()) {
                echo "&lt;script>document.location.href='$url';</script>\n";
        } else {
                //@ob_end_clean(); // clear output buffer
                header( 'HTTP/1.1 301 Moved Permanently' );
                header( 'Location: ' . $url );
        }
        $this->close();
}


<! removed transcluded page call, red link never existed >

Examples[edit]

Code Examples[edit]