Craig M Roberts logo
Find a random article

About

An image displaying the face of Craig Roberts

Recent Articles

Popular Articles

Alternate background images depending on URL

Published on March 4, 2010 @ 10:38 pm

This tutorial will show you how to create display different background images for each of your web pages, just like this website!
To begin this tutorial you must have a PHP file and some images.

Firstly, if you have a master header then you will only need to use this code once, if not you will have to put this code within each header of your individual web pages.

1. Open up a PHP file and input this code anywhere before the tag.
2. Line 4 is the server statement which is used to find what web page is currently being viewed by the visitor.
3. Line 7 and 8 creates variables for the webpages that will have an alternate background image.
4. Basically what line 11 states here is that if the page variable URL (in line 7) is the exact same as the page URL that is being viewed then the value of the new variable, $BGIMAGE is bg-page.jpg. Else if this statement is false then check if the following statement (Line 12) is true.
5. If neither of the statements on lines 11 or 12 is true then use the default background image as the value of the $BGIMAGE variable.

<?php




$currentpage = $_SERVER['REQUEST_URI']; // Get the URL of the current page


$page1 = “/index.php“; //Creating a variable
$page2 = “/about.php“; //Creating a variable


if ($page1==$currentpage) { $BGIMAGE = ‘bg-page1.jpg’; } else
if ($page2==$currentpage) { $BGIMAGE = ‘bg-page2.jpg’; } else


{$BGIMAGE = ‘default-bg.jpg’;}


?>



6. The final step is to add a background-image style to the

<html>

<head></head>

<body style=”background-img:url(‘<?php echo $BGIMAGE; ?> ‘)”>

</body>

</html>



Voila! And your good to go! Please leave a comment. Thanks!

Filed under PHP

2 Responses to "Alternate background images depending on URL" »

  1. emt training on April 5, 2010 @ 2:29 pm said:

    Keep posting stuff like this i really like it

  2. Wordpress Themes on August 14, 2010 @ 12:23 pm said:

    This helped me alot in my college assignement. Thank you.

RSS feed for comments on this post · TrackBack URI



Leave a Comment

Latest Client

Keep in touch

Article Categories

Freelance Work Schedule

Copyright © 2010 Craig M Roberts | Email