tags:

views:

286

answers:

3

Hi there,

I have a simple div which I don't want to load if the visitor loads up a certain URL.

It looks like this:

<?php
if( stristr($_SERVER['PHP_SELF'], 'blog') == FALSE )
{

echo "<div id="stuff"></div>";

}
?>

Problem is... it doesn't work... when I load up www.url.com/blog the div#stuff still shows.

Am I just lacking sleep or should the above work? What would you do to not have a div display if the url contains blog ?

+3  A: 

Try $_SERVER['REQUEST_URI'] instead:

if (substr($_SERVER['REQUEST_URI'], 0, 5) !== '/blog') {
    echo '<div id="stuff"></div>';
}

REQUEST_URI contains the requested URI path and query and not just the filename of the currently executing script like PHP_SELF does.

Gumbo
worked a charm!
cosmicbdog
+1  A: 

try using the === operator as per the 2nd example on:

http://us2.php.net/manual/en/function.stristr.php

Finer Recliner
is stristr better for this sort of situation than substr? e.g faster / less server resources?
cosmicbdog
+3  A: 

OP's version works for me. Provided that you fix the echo() syntax error.

echo "<div id=\"stuff\"></div>";

tested on PHP Version 5.2.9

Gert
thanks for that! yeah it was poor pseudo coding of me...
cosmicbdog