How To Become Base Of Operations Url Amongst Php?

You tin easily croak base of operations URL alongside PHP alongside few steps. Following component subdivision tin live on useful when yous involve base of operations URL for illustration to exhibit right images, charge stylesheets as well as scripts.
<?php component subdivision home_base_url(){     // get croak http protocol if http or https  $base_url = (isset($_SERVER['HTTPS']) &&  $_SERVER['HTTPS']!='off') ? 'https://' : 'http://';  // croak default website rootage directory  $tmpURL = dirname(__FILE__);  // when purpose dirname(__FILE__) volition provide value similar this "C:\xampp\htdocs\my_website",  //convert value to http url purpose string replace,   // supersede whatsoever backslashes to slash inwards this illustration purpose chr value "92"  $tmpURL = str_replace(chr(92),'/',$tmpURL);  // forthwith supersede whatsoever same string inwards $tmpURL value to nix or ''  // as well as volition provide value similar /localhost/my_website/ or simply /my_website/  $tmpURL = str_replace($_SERVER['DOCUMENT_ROOT'],'',$tmpURL);  // delete whatsoever slash graphic symbol inwards get as well as end of value  $tmpURL = ltrim($tmpURL,'/');  $tmpURL = rtrim($tmpURL, '/');   // cheque 1 time again if nosotros discovery whatsoever slash string inwards value thence nosotros tin assume its local automobile      if (strpos($tmpURL,'/')){  // explode that value as well as stimulate got alone get value         $tmpURL = explode('/',$tmpURL);         $tmpURL = $tmpURL[0];        }  // forthwith end steps  // assign protocol inwards get value     if ($tmpURL !== $_SERVER['HTTP_HOST'])  // if protocol its http thence similar this        $base_url .= $_SERVER['HTTP_HOST'].'/'.$tmpURL.'/';      else  // else if protocol is https        $base_url .= $tmpURL.'/';  // hand provide value  provide $base_url;   } ?>  // as well as exam it  echo home_base_url();  
Output volition live on similar this
local automobile : http://localhost/my_website/ or https://myhost/my_website   populace : http://www.my_website.com/ or https://www.my_website.com/ 
purpose home_base_url component subdivision at index.php of your website as well as define it as well as thence yous tin purpose this component subdivision to charge script, css as well as content via url similar
<?php echo '<script src="'.home_base_url().'js/script.js" type="text/javascript"></script>'."\n"; ?> 
volition exercise output similar this :
 <script src="http://www.my_website.com/js/script.js" type="text/javascript"?></script> 
Source: http://stackoverflow.com/a/21723209
Sumber http://developer-paradize.blogspot.com

Comments

Popular posts from this blog

What Are The Main Components of a Computer System

Top Qualities To Look For In An IT Support Team

How To Integrate Google Adwords Api Into Codeigniter?