dynamic-base-url.php


SUBMITTED BY: Guest

DATE: May 8, 2013, 2:56 a.m.

FORMAT: PHP

SIZE: 1.1 kB

HITS: 1086

  1. <?php
  2. function base_url()
  3. {
  4. /* Get protocol information whether using secure or normal */
  5. $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"], 0, 5)) == 'https://' ? 'https://' : 'http://';
  6. /* Call current script */
  7. $path = $_SERVER['PHP_SELF'];
  8. /*
  9. * returns arrays:
  10. * Array (
  11. * [dirname] => /path/
  12. * [basename] => script.php
  13. * [extension] => php
  14. * [filename] => script
  15. * )
  16. */
  17. $path_parts = pathinfo($path);
  18. $directory = $path_parts['dirname'];
  19. /*
  20. * Replace backslash dirname,
  21. * If it's in main directory only return / if within folder return dirname/
  22. */
  23. $directory = str_replace('\\', '', $directory);
  24. $directory = ($directory == "/") ? "/" : $directory .'/';
  25. /* return domain name (localhost / domain.com) */
  26. $host = $_SERVER['HTTP_HOST'];
  27. /* Final Output */
  28. return $protocol . $host . $directory;
  29. }
  30. echo base_url();
  31. /* End of dynamic-base-url.php */

comments powered by Disqus