Will Perone

Getting the URL of the page you're on in PHP can be very useful for automating stuff. Thankfully it's not that hard to do:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];
if (strlen($_SERVER['QUERY_STRING']) > 0) {
	$url= $url . '?' . $_SERVER['QUERY_STRING'];
}


The only potential catch is including the query string (the ?x=something&y=somethingelse stuff) if it's there just in case. You may also want to get the relative path to the root directory on your site. This is commonly used to include resources in a webpage that's in an arbitrary directory. There is no straightforward way to do this though:
$ROOT_DIR="";
while (!file_exists($ROOT_DIR . "somefilethatexistsonroot.html") 
       && strlen($ROOT_DIR) < 70) 
    { $ROOT_DIR="../" . $ROOT_DIR; } 
You have to basically search for a file that you know exists only on the top level directory (like index.html) and keep adding ../ to the relative path. I've also added a failsafe if the file can't be found (the < 70 part). Now you can have all of your images in a central location and include them in any page in any directory on the website. Including them in pages is as easy as:
<img src="<?php echo $ROOT_DIR;?>images/image.jpg">


1 Comment
Linda 2012/09/10 Contact Me0 0
I am really eioyjnng the theme/design of your weblog. Do you ever run into any web browser compatibility problems? A few of my blog readers have complained about my blog not operating correctly in Explorer but looks great in Chrome. Do you have any ideas to help fix this issue?

<- for private contact