Advertisement
rgman

PHP Document Root, Path and URL detection

Apr 23rd, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.65 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * This snippet was originally written in Sébastien Lavoie's blog.
  5.  * You can find the post at
  6.  * http://blog.lavoie.sl/2013/02/php-document-root-path-and-url-detection.html
  7.  */
  8.  
  9.  
  10. $baseDir = __DIR__;
  11. $docRoot = preg_replace( "!{$_SERVER['SCRIPT_NAME']}$!", '', $_SERVER['SCRIPT_FILENAME'] );
  12. $baseUrl = preg_replace( "!^{$docRoot}!", '', $baseDir );
  13. $protocol = empty( $_SERVER['HTTPS'] ) ? 'http' : 'https';
  14. $port = $_SERVER['SERVER_PORT'];
  15. $dispPort = ( $protocol == 'http' && $port == 80 || $protocol == 'https' && $port == 443 ) ? '' : ":$port";
  16. $domain = $_SERVER['SERVER_NAME'];
  17. $fullUrl = "$protocol://{$domain}{$dispPort}{$baseUrl}";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement