Posted By

mladoux on 09/21/10


Tagged

php redirect function utility


Versions (?)

Simple HTTP Redirect Function


 / Published in: PHP
 

URL: http://mark.haktstudios.com/

Simple function to handle HTTP Redirections in a script

  1. // place within index.php
  2.  
  3. // figure out our settings and manipulate variables accordingly
  4. $self = 'index.php';
  5. $uri = str_replace($self, '', $_SERVER['SCRIPT_NAME']);
  6. $https = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ) ? true : false;
  7. if ( $https !== false ) $site = "https://{$_SERVER['HTTP_HOST']}{$uri}";
  8. else $site = "http://{$_SERVER['HTTP_HOST']}{$uri}";
  9.  
  10. // define our constant
  11. define('SITE', $site);
  12.  
  13. // free up our variables
  14. unset($self,$uri,$https,$site);
  15.  
  16. /******************************************************************************/
  17. /* redirect($address)
  18. /*
  19. /* Redirects your browser to $address
  20. /******************************************************************************/
  21.  
  22. function redirect($address)
  23. {
  24. // Make sure address is formatted properly
  25. if( stripos($address, 'http://') === false &&
  26. stripos($address, 'https://') === false
  27. ) $address = SITE.$address;
  28.  
  29. // redirect
  30. header("Location: {$address}");
  31. }

Report this snippet  

You need to login to post a comment.