• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

change background in PHP

Not open for further replies.


I have successfully made the background change on rollover (as stated in on of my other posts in this section) and now I need it to keep that background on a post after clicking on that link.

Any ideas how I can set this to work?


Active Member
Well if the background's changed via Javascript I would personally add it to the link as a get variable when the onmouseover event kicks in and then look for the get variable on the next page and if it is present change the body tag, or whatever, background. Set a cookie as well and test for that as well then it stays like that cross site.

Need a hand with it let me know.


Active Member
Well if you have several options then this will work.
<a href="background.php?colour=red">Red design</a>

Then on all other pages just write something similar to ~
1. <?php if(isset($_GET['colour']) && $_GET['colour']=="blue" || $_GET['colour']=="red") {
2. if(!isset($_COOKIE['Colour'])){
3. setcookie("Colour",$_GET['colour'])?>
4. body{background:url("path/to/<?php echo $_GET['colour'];?>.png")}
5. <?php }
6. }if(isset($_COOKIE['Colour']) && $_COOKIE['Colour']=="blue" || $_COOKIE['Colour']=="red"){?>
7. <body{background:url("path/to/<?php echo $_COOKIE['Colour'];?>.png")}
8. <?php }else{?>
9. <body{background:url("path/to/default.png")}
10. <?php }?>
1. Test to see if the get variable is set and that it is set to 1 of the backgrounds that you want, add the double pipe followed by $_GET['Colour'] followed by double equal sign followed by the background's name in quotes to add more to test by. Important otherwise they could get any file really not something you want to happen.

2. We then test to see if the cookie is set.

3. If it is not set it to the get variable which we know can only be 1 of a few things.

4. Set the background of, I used the body tag but you could add a linked in CSS sheet in their.

5. We end the cookie test.

6. We end the get test and start a test looking for the cookie and if it is found test to make sure it only contains data we want.

7. Set the body tag to the background set in the cookie.

8. End the cookie test.

9. If the user came to the page and neither are set show the default background.

10. End the last else.

bare in mind that will mean that the session cookie will take precedence, if you want the get to take precedence let me know.

Let me know if you get an error.

Now if you just have 1 link rather than a selection we can create it dynamically and add the right get variable on.

Not open for further replies.