fruzle.com logo


Class B no-www status

Get Firefox
 

Home » Various tutorials » Easy local linking (PHP)


Easy local linking (PHP) 

Code added since previous step is in blue.
Comments are in green (you can remove those from your code if you want).


When I was making this site I realized I was going to have a big problem: the development was being done in a sub-directory of the localhost root directory.
This meant that links back to JS and CSS files that I might put in includes would have to be something like "/directory/filename.ext". But if I used those on the web server the files wouldn't load, as there those files start in the root, not a sub-directory.

Here's a very simple PHP script I use to simplify these things:
<?php
    $domain = $_SERVER['HTTP_HOST'];

if ($domain == "localhost") :
    /* OFFLINE vars */
    $baseurl = "/directory/";
else :
    /* ONLINE vars */
    $baseurl = "/";
endif;
    ?>


What does it do?
The line in blue determines what the host (the site's domain basically) is identified as and assigns that info to a variable ($domain).
<?php
    $domain = $_SERVER['HTTP_HOST'];

if ($domain == "localhost") :
    /* OFFLINE vars */
    $baseurl = "/directory/";
else :
    /* ONLINE vars */
    $baseurl = "/";
endif;
    ?>


This line says that IF the variable's data equels localhost, something has to be done:
<?php
    $domain = $_SERVER['HTTP_HOST'];

if ($domain == "localhost") :
    /* OFFLINE vars */
    $baseurl = "/directory/";
else :
    /* ONLINE vars */
    $baseurl = "/";
endif;
    ?>


Then I go on setting a variable $baseurl to the desired directory:
<?php
    $domain = $_SERVER['HTTP_HOST'];

if ($domain == "localhost") :
    /* OFFLINE vars */
    $baseurl = "/directory/";
else :
    /* ONLINE vars */
    $baseurl = "/";
endif;
    ?>


If the original question (2 steps back) isn't true, something else is - in other words: we're not online.
<?php
    $domain = $_SERVER['HTTP_HOST'];

if ($domain == "localhost") :
    /* OFFLINE vars */
    $baseurl = "/directory/";
else :
    /* ONLINE vars */
    $baseurl = "/";
endif;
    ?>


So let's define the variable for the root:
<?php
    $domain = $_SERVER['HTTP_HOST'];

if ($domain == "localhost") :
    /* OFFLINE vars */
    $baseurl = "/directory/";
else :
    /* ONLINE vars */
    $baseurl = "/";
endif;
    ?>


And of course close the IF-statement:
<?php
    $domain = $_SERVER['HTTP_HOST'];

if ($domain == "localhost") :
    /* OFFLINE vars */
    $baseurl = "/directory/";
else :
    /* ONLINE vars */
    $baseurl = "/";
endif;
    ?>


Put this code in a PHP file and include it at the top of your pages before you call anything that might need to use the base URL variable.
Don't forget you will have to is set the local and remote directories as desired.

To use it for linking to page, files, or loading images simply call the variable $baseurl like this:
HTML example:
<img src="<?php echo $baseurl; ?>image.png"; />
PHP echo example:
echo "<img src=\"".$baseurl."image.png" />";


If you're using the same CSS or JS files in multiple directories, you can also use this to link to them.
Just remember to put the include for this code at the top of the page's code.

Need more explanation? Found a typo? Contact me.