RSS

|| php || helping code || how to get base url in custom php?

19 Oct

Sometimes while coding in php, relative paths doesn’t work properly where they suppose to be, so for such cases one need an absolute url and for that need to concatenate the base url with the relative url as per requirement. Following code is there to get the base url.

$file_name = explode('/',$_SERVER['REQUEST_URI']);
$root_dir = "";
for($i = 0 ; $i< (count($file_name)-1) ;$i++) 
// -1 to exclude filename
{
$root_dir .='/'.$file_name[$i];
}

$base_url = 'http://'.$_SERVER['HTTP_HOST'].$root_dir;

$_SERVER array has many other useful info, just do print_r($_SERVER); and you can trace the SCRIPT_FILENAME of the current file,QUERY_STRING , SCRIPT_NAME, REQUEST_METHOD etc.

Advertisements
 
Leave a comment

Posted by on October 19, 2011 in php

 

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: