Don't show this if the url contains the follow

2020-05-06 07:13发布

问题:

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 ?

回答1:

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.



回答2:

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



回答3:

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

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



标签: php wordpress