Advertisement
azmicolejr

Cara Membuat Fungsi base_url pada Native PHP

Dec 25th, 2016
7,853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. <?php
  2. if (!function_exists('base_url')) {
  3. function base_url($atRoot=FALSE, $atCore=FALSE, $parse=FALSE){
  4. if (isset($_SERVER['HTTP_HOST'])) {
  5. $http = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
  6. $hostname = $_SERVER['HTTP_HOST'];
  7. $dir = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
  8. $core = preg_split('@/@', str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(dirname(__FILE__))), NULL, PREG_SPLIT_NO_EMPTY);
  9. $core = $core[0];
  10. $tmplt = $atRoot ? ($atCore ? "%s://%s/%s/" : "%s://%s/") : ($atCore ? "%s://%s/%s/" : "%s://%s%s");
  11. $end = $atRoot ? ($atCore ? $core : $hostname) : ($atCore ? $core : $dir);
  12. $base_url = sprintf( $tmplt, $http, $hostname, $end );
  13. }
  14. else $base_url = 'http://localhost/';
  15. if ($parse) {
  16. $base_url = parse_url($base_url);
  17. if (isset($base_url['path'])) if ($base_url['path'] == '/') $base_url['path'] = '';
  18. }
  19. return $base_url;
  20. }
  21. }
  22. $base_url = base_url();
  23. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement