Skip to content

Instantly share code, notes, and snippets.

@aufa
Last active August 13, 2016 13:05
Show Gist options
  • Save aufa/5c0aafc6b8c7d218db05 to your computer and use it in GitHub Desktop.
Save aufa/5c0aafc6b8c7d218db05 to your computer and use it in GitHub Desktop.
Determine Base Url On PHP Scripts www.inforiatif.com
<?php
/**
* This is index main file
* Base URL helper
* @author awan <http://www.inforiatif.com>
* artcile : http://www.inforiatif.com/cara-menentukan-url-base-domain-di-php
*/
/* ------------------------------------------------------------------------------------------------------ \
HELPER
PUT THIS ON YOUR index.php
\ ------------------------------------------------------------------------------------------------------ */
/**
* isset( $_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' # port 443 is not always for ssl
* load balancer fix that has x forwarder protocol set with. add $_SERVER['HTTPS'] -> 'on' value
*/
if (isset( $_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$_SERVER['HTTPS'] = 'on';
}
/* GETTING PROTOCOL
----------------------------------------- */
$protocol = ( isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] == 'https' ) ||
( isset( $_SERVER['HTTPS']) && ( strtolower( $_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == '1' ) )
? 'https' : 'http';
/* GETTING DOCUMENT ROOT USING REALPATH AVOID SYMLINK
----------------------------------------- */
$docroot = function_exists('realpath') ? realpath($_SERVER['DOCUMENT_ROOT']) : $_SERVER['DOCUMENT_ROOT'];
// clean path ( windows directory separator fix )
$docroot = str_replace( array( "\\", "\\\\" ), "/", $docroot );
/* GETTING DOCUMENT ROOT USING REALPATH AVOID DIR SYMLINK IF PROVIDE
----------------------------------------- */
$base = function_exists('realpath') ? realpath(dirname(__FILE__)) : dirname(__FILE__);
$base = str_replace( array( "\\", "\\\\" ), "/", $base );
/* GETTING HOST AS DOMAIN
----------------------------------------- */
$host = ( isset( $_SERVER['HTTP_HOST'] ) ? $_SERVER['HTTP_HOST'] : isset( $_SERVER['SERVER_NAME'] ) ? $_SERVER['SERVER_NAME'] : false );
/**
* if no host exits
* if server is not support !important
*/
if( ! $host ) {
header( 'HTTP/1.1 400 Bad Request');
die('Server Not Supported, HTTP_HOST / SERVER_NAME is not defined!');
}
/* ------------------------------------------------------------------------------------------------------ \
END HELPER
\ ------------------------------------------------------------------------------------------------------ */
// this is the path after domain will be directory after domain name
$path = ltrim(dirname($_SERVER['SCRIPT_NAME']), '/');
// remove slashes on start and ending with adding slash on start
$path = $path == '' ? '' : '/'.$path;
// real domain base url
$baseurl = rtrim( $protocol.'://'. $host.$path );
/**
* This is additional as example to use
* application define prevent overide
*/
define('BASEDIR', $base);
define('BASEPATH', $path);
define('PROTOCOL', $rotocol);
define('BASEURL', $baseurl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment