-2

I have my template, and I want it to display a certain image if you are on certain page like http://example.com/test and if you aren't on that page, then I want it to display another image.

I also want it to display the image if you are in any sub directory like http://example.com/test/stuff

Also, is there a way to do this with multiple pages in the same code?

So like

if page = example.com/test then display testimg.jpg

if page = example.com/archive then display archive.jpg

else, display defaultimg.jpg

thanks!

Khurram Sharif
  • 504
  • 1
  • 4
  • 20
QBcrusher
  • 3
  • 3
  • Related: http://stackoverflow.com/questions/7118823/check-if-url-has-certain-string-with-php – Sandeep Chatterjee Jan 18 '15 at 17:52
  • thanks, but didn't really answer my question. still not sure how to implement images with that. I'm rather new to PHP and still trying to learn the ropes. – QBcrusher Jan 18 '15 at 18:04
  • 1
    Please consider posting your attempt(codewise). See [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Sandeep Chatterjee Jan 18 '15 at 18:14

2 Answers2

0
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( strpos($url, 'test') !== false ) {
    echo('<img src="image_path/testimg.jpg">');
}
elseif ( strpos($url, 'archive') !== false ) {
    echo('<img src="image_path/archive.jpg">');
}
else {
    echo('<img src="image_path/defaultimg.jpg">');
}
Dmitry G
  • 93
  • 1
  • 8
  • Function strpos() may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function. http://php.net/manual/en/function.strpos.php – Dmitry G Jan 18 '15 at 19:10
0

U can also using strpbrk() function and get more compact code: (>PHP5)

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( strpbrk($url, 'test') ) {
    echo('<img src="image_path/testimg.jpg">');
}
elseif ( strpbrk($url, 'archive') ) {
    echo('<img src="image_path/archive.jpg">');
}
else {
    echo('<img src="image_path/defaultimg.jpg">');
}
Dmitry G
  • 93
  • 1
  • 8