Solved

Display Different Graphic On Each Page

Posted on 2014-09-25
6
87 Views
Last Modified: 2014-10-09
Hi,

I need to display a different graphic on each page of my site (in the top header).

www.mywebsite.com = graphic01.png
www.mywebsite.com/about = graphic02.png
www.mywebsite.com/contact = graphic03.png
www.mywebsite.com/portfolio = graphic04.png
www.mywebsite.com/services = graphic05.png

Then for all other pages, i just want to display the default graphic (graphic-default.png).

What would be the best way to accomplish this using PHP, thanks in advance for your help.
0
Comment
Question by:oo7ml
  • 3
  • 3
6 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
PHP is self-aware.  You can locate $_SERVER['REQUEST_URI'] and test the value to see if the strings "about", "contact", etc. are present in the variable.  You can use an array to map the names of the pages to the names of the graphics, and choose the right one with an if() statement or a switch() statement.
0
 
LVL 10

Accepted Solution

by:
Chris_Gralike earned 250 total points
Comment Utility
Code example based on Ray's comment.

<?php
// See http://php.net/manual/en/reserved.variables.server.php for more info on $_SERVER superglobal.

switch($_SERVER['REQUEST_URI']) {
                      case '/about':
                             echo '<img src="./images/graphic02.png" />';
                             break;
                      case '/contact':
                             echo '<img src="./images/graphic03.png" />';
                             break;
                       // etc
                      default:
                             // Mainpage (or case '/' for performance)
                             echo '<img src="./images/graphic00.png" />';
                             break;
}
?>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
@Chris: just wondering - did you test that code?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 10

Expert Comment

by:Chris_Gralike
Comment Utility
As a matter of fact i did.
Why you ask?

EE-contact-.png

(I admit the example is loosely written and requires url rewrite to be configured. It will be more stable using basename and various checks)
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Just thinking out loud - what if there is a URL parameter request variable?  I think it might be worth isolating the desired substring(s) from the URL.  After I wrote, "switch()" I got to thinking that maybe it wasn't the right application for switch()!
0
 
LVL 10

Expert Comment

by:Chris_Gralike
Comment Utility
My code doesn't handle that at all and in my opinion it should not.

Its an example that needs refinement (sure). But an example non the less. Copy-pasting any example in production without a second thought and precise consideration is bad practice in any situation. It should be rewritten to answer the requirements set out by the user matching its context, and thus needs 'work' ;-)

On how to overcome the risk you set out without masses of code, is by url rewrite: https://wiki.apache.org/httpd/RewriteQueryString. But this in my opinion is a different question.

But!
of course you are absolutely right the code in the example is a huge risk without additional coding.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now