# Dynamic image change

**URL:** <https://forum.kirupa.com/t/dynamic-image-change/260787>\
**Category:** Uncategorized\
**Created:** [May 19, 2008, 9:58pm UTC](https://forum.kirupa.com/t/dynamic-image-change/260787 "2008-05-19T21:58:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![polymaze](https://avatars.discourse-cdn.com/v4/letter/p/9de0a6/32.png) [@polymaze](https://forum.kirupa.com/u/polymaze)\
**Post date:** [May 19, 2008, 9:58pm UTC](https://forum.kirupa.com/t/dynamic-image-change/260787/1 "2008-05-19T21:58:26Z")

</div>

Hello,

Does anyone know if there is a way to dynamically change an image (header in this case) that is loaded as a background in a css file to a different image for each page using the same header.html include file?

Thanks

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [May 20, 2008, 4:49am UTC](https://forum.kirupa.com/t/dynamic-image-change/260787/2 "2008-05-20T04:49:43Z")

</div>

You would rename your stylesheet from .css to .php. Then you would put the following line in the first line:

```php
header("Content-type: text/css");

```

Then, for the id, you could do something like this:

```php

$thisPage = $_SERVER['REQUEST_URI'];
 
switch($thisPage)
{
   case "/index.php":
     $header = 'header1.jpg';
     break;
 
   case "/aboutus.php":
     $header = 'header2.jpg';
     break;
 
   case "/services.php":
     $header = 'header3.jpg';
     break;
}
echo "#header{ background-image: url($header);";

```

---

<div class="post-metadata">

**Author:** ![polymaze](https://avatars.discourse-cdn.com/v4/letter/p/9de0a6/32.png) [@polymaze](https://forum.kirupa.com/u/polymaze)\
**Post date:** [May 21, 2008, 1:12am UTC](https://forum.kirupa.com/t/dynamic-image-change/260787/3 "2008-05-21T01:12:17Z")

</div>

Thanks for that, however, I couldn’t get it to work.

I’ve set up the files on a test server [http://polymaze.funformentals.com/](http://polymaze.funformentals.com/) with a zip file [here](http://polymaze.funformentals.com/sitefiles.zip) in case you are willing to look at it.

---

<div class="post-metadata">

**Author:** ![kdd](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kdd](https://forum.kirupa.com/u/kdd)\
**Post date:** [May 21, 2008, 1:39am UTC](https://forum.kirupa.com/t/dynamic-image-change/260787/4 "2008-05-21T01:39:23Z")

</div>

Not sure I understand your question, but I’ll try. It seems you want to change the header image for each page.  
Assume you have 1-10 images in a folder called Photos. Then you’d do something like:

```php

<?php
$num = mt_rand ( 1 , 10 );
?>

<style>
body {
background: .../photos/<?= $num; ?>.jpg;
}
</style>

```

Something like that. Again, I don’t quite understand what you want to do, so this is the best I can help.  
🙂

---

<div class="post-metadata">

**Author:** ![polymaze](https://avatars.discourse-cdn.com/v4/letter/p/9de0a6/32.png) [@polymaze](https://forum.kirupa.com/u/polymaze)\
**Post date:** [May 21, 2008, 4:04am UTC](https://forum.kirupa.com/t/dynamic-image-change/260787/5 "2008-05-21T04:04:36Z")

</div>

o, sorry.

What I’m trying to do is set the header image for each page, (home, about, etc…) by designating the header background image through the css. However, each page has a different background image, and I thought it would be possible to have the #header background image in the css file targeted in the first line of each page. Like:

\<?php # index.php  
$page\_title = ‘home’;  
($page\_headerimage - tells which image to load)  
require\_once (’./includes/header.html’);  
?\>

Does that make sense? I’m really new to php and not sure if I’m explaining this correctly.
