redirect_function.php
1 <?php
2
/*
3 -- Option 1
4 ----------------------------------------------
5 sleep(10);//seconds to wait..
6 header("Location:http://www.domain.com");
7
8 -- Option 2
9 ----------------------------------------------
10 //  refresh / redirect to an external web page
11 //  ------------------------------------------
12 header( 'refresh: 0; url=http://www.example.net' );
13 echo '<h1>You won\'t know what hit you!</h1>';
14 */
15
16 /**
17  * Redirect
18  *
19  * @param string $url the url to be redirected, must start with http://
20  * @param integer $delay how many seconds to be delayed
21  * @param boolean $js whether to return JavaScript code for redirection
22  * @param boolean $js_wrapped whether to use <script> tag when returing JavaScript
23  * @param boolean $return whether to return JavaScript code
24  */
25
function redirect_function($url$delay 0$js FALSE$js_wrapped TRUE$return FALSE) {
26     if ( ! 
$js) {
27         if (
headers_sent() || $delay 0) {
28             echo <<<EOT
29
                <html>
30                 <head>
31                 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
32                 <meta http-equiv="refresh" content="
{$delay};url={$url}" />
33                 </head>
34                 <body>
35                 Redirecting to <a href="
{$url}">{$url}</a>.
36                 </body>
37                 </html>
38
EOT;
39             exit;
40         } else {
41             
header("Location: {$url}");
42             exit;
43         }
44     }
45
46     
$out '';
47     if (
$js_wrapped) {
48         
$out .= '<script language="JavaScript" type="text/javascript">';
49     }
50     
51     if (
$delay 0) {
52         
$out .= "window.setTimeout(function () { document.location='{$url}'; }, {$delay});";
53     } else {
54         
$out .= "document.location='{$url}';";
55     }
56     
57     if (
$js_wrapped) {
58         
$out .= '</script>';
59     }
60
61     if (
$return) {
62         return 
$out;
63     }
64
65     echo 
$out;
66     exit;
67 }
68
69
/* End of file: ./system/functions/redirect/redirect_function.php */

Page URI: http://www.infopotato.com/index.php/code/function/redirect/