Advertisement
Jodyone

Redirect

May 16th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1.  
  2.     /**
  3.      * Redirects user to destination, which can be
  4.      * a URL or a relative path on the local host.
  5.      *
  6.      * Because this function outputs an HTTP header, it
  7.      * must be called before caller outputs any HTML.
  8.      */
  9.     function redirect($destination)
  10.     {
  11.         // handle URL
  12.         if (preg_match("/^https?:\/\//", $destination))
  13.         {
  14.             header("Location: " . $destination);
  15.         }
  16.  
  17.         // handle absolute path
  18.         else if (preg_match("/^\//", $destination))
  19.         {
  20.             $protocol = (isset($_SERVER["HTTPS"])) ? "https" : "http";
  21.             $host = $_SERVER["HTTP_HOST"];
  22.             header("Location: $protocol://$host$destination");
  23.         }
  24.  
  25.         // handle relative path
  26.         else
  27.         {
  28.             // adapted from http://www.php.net/header
  29.             $protocol = (isset($_SERVER["HTTPS"])) ? "https" : "http";
  30.             $host = $_SERVER["HTTP_HOST"];
  31.             $path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
  32.             header("Location: $protocol://$host$path/$destination");
  33.         }
  34.  
  35.         // exit immediately since we're redirecting anyway
  36.         exit;
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement