API16:JApplication/redirect
From Joomla! Documentation
Description
Redirect to another URL.
Syntax
redirect($url, $msg='', $msgType='message', $moved=false)
| Parameter Name | Default Value | Description |
|---|---|---|
| $url | The URL to redirect to. Can only be http/https URL | |
| $msg | An optional message to display on redirect. | |
| $msgType | 'message' | An optional message type. |
| $moved | false | True if the page is 301 Permanently Moved, otherwise 303 See Other is assumed. |
Returns
none; calls exit().
Defined in
libraries/joomla/application/application.php
Importing
jimport( 'joomla.application.application' );
Source Body
public function redirect($url, $msg='', $msgType='message', $moved = false)
{
// Check for relative internal links.
if (preg_match('#^index2?\.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 {
// It's 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 "<script>document.location.href='$url';</script>\n";
} else {
header($moved ? 'HTTP/1.1 301 Moved Permanently' : 'HTTP/1.1 303 See other');
header('Location: '.$url);
}
$this->close();
}
<! non-existant page notice to transclude >
Examples
Code Examples