Obvious In Hindsight
Archive of tips, tricks and solutions for designers and programmers!

PHP: Using predefined variables in paths


Predefined variables can be used to replace specific parts of the paths of files or images.

Predefined variable in a path for an include file

It’s important to understand that the path for an include file is relative to the web server and not the web root where the home page is stored.

The predefined variable:

$_SERVER[‘DOCUMENT_ROOT’]

It produces something like this:

/home/yoursite/public_html

Using it in a path:

<?php include($_SERVER[‘DOCUMENT_ROOT’].”/somefolder/somefile.html”); ?>

Predefined variable for images

This path is relative to the web root (unlike the path for the  include file).

The predefined variable:

$_SERVER[‘HTTP_HOST’]

It produces something this:

http://www.yoursite.com

Using it in a path:

<img src=”<?php echo $_SERVER[‘HTTP_HOST’];?>/images/logo.png”>

Learn more about predefined variables here: http://php.net/manual/en/reserved.variables.php

Everything’s obvious in hindsight, eh?

Advertisements

No Responses to “PHP: Using predefined variables in paths”

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: