Forum Moderators: coopster

Message Too Old, No Replies

Making include dependent on URL

         

encyclo

7:20 pm on Oct 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, here's a simple scenario: I want to use a basic PHP include within a CMS template. However, I would like to make the include only appear on pages which correspond to a certain URL pattern.

For example, I would like the include to happen if the address is

example.com/[b]widgets[/b]/whatever.htm
but not if the directory is called anything other than "widgets".

Is there an easy way to make an include conditional on an URL string?

Many thanks for your help!

HughMungus

7:28 pm on Oct 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm a rookie, but, what about just using $current_url = $_SERVER['PHP_SELF']; then parsing that out using explode or something (or strpos to see if a string (directory name) exists in the URL)?

mincklerstraat

9:35 pm on Oct 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



an example along the lines of Mr. Mungus's suggestion:

if(strpos($_SERVER['PHP_SELF'], '/widgets/')!== FALSE) {
include 'conditionalinclude.php';
}

* note the "!==" and not "!=" ; this needs to check for type as well as value, since 0 returned from

strpos
would mean starting at the beginning of the string.

jatar_k

10:33 pm on Oct 20, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



or you could something like so

$dir_check = $_SERVER['PHP_SELF'];
if (strstr($dir_check,"somedir")) $section = "mysection";

then just use the $section var for any conditional statements in the page