Forum Moderators: coopster

Message Too Old, No Replies

check for URL if . then. include file.

how?

         

marcus76

1:38 pm on Jun 4, 2005 (gmt 0)

10+ Year Member



Hi All,

I'm using the following piece of code:

if (
(stristr(basename($PHP_SELF),'index')) and
(!isset($_GET['cPath']))
) {
require(DIR_WS_INCLUDES . 'header2.php');
} else {
require(DIR_WS_INCLUDES . 'header.php');
}

Problem is my manufacturer URL's i.e - index.php?manufacturers_id=12 , defaults to the 'header2.php' i need it to include the 'header.php'.

Can anyone suggest a revision to the code snippet above which may help accomplish this?

Many thanks for your help.

Marcus

coopster

4:48 pm on Jun 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That's because the cPath GET variable is not set (only $_GET['manufacturers_id'] is set in this case). You can either add it to your if statement, or append it to your GET string. Since you asked for the former ...

Add it to the if statement:

if (
(stristr(basename($PHP_SELF),'index')) and
(!isset($_GET['cPath']))
OR
(stristr(basename($PHP_SELF),'index')) and
(isset($_GET['manufacturers_id']))

) {
require(DIR_WS_INCLUDES . 'header2.php');
} else {
require(DIR_WS_INCLUDES . 'header.php');
}

marcus76

6:23 pm on Jun 4, 2005 (gmt 0)

10+ Year Member



Thanks Coopster,

The logic doesn't seem correct however. I need to include 'header.php' for a URL that contains the manufacturers_id.

I tried the following, this didn't work either, incorrect syntax perhaps?:

<?php
if (stristr(basename($PHP_SELF),'index')) and
(!isset($_GET['cPath']))
{ require(DIR_WS_INCLUDES . 'header2.php');
} elseif (stristr(basename($PHP_SELF),'index')) and
(isset($_GET['manufacturers_id']))
{ require(DIR_WS_INCLUDES . 'header.php');
} else { require(DIR_WS_INCLUDES . 'header.php');
}
?>

With the above snippet, i get :

Parse error: parse error, unexpected T_LOGICAL_AND in /public_html/dev/index.php on line 172

Any ideas?

Thanks

Marcus

coopster

7:43 pm on Jun 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm sorry, now that I read that again I believe I understand what you are trying to accomplish. A hint when trying to figure logic like this out is to simply write it in plain language first, then code to the plan.

If the request is for the index page, then give them header2, but only if they are (not requesting a "cPath" and they are not requesting a "manufacturers_id"):

if (
(stristr(basename($PHP_SELF),'index')) and
(!isset($_GET['cPath']) and !isset($_GET['manufacturers_id']))
) {
require(DIR_WS_INCLUDES . 'header2.php');
} else {
require(DIR_WS_INCLUDES . 'header.php');
}

marcus76

12:32 am on Jun 5, 2005 (gmt 0)

10+ Year Member



Coopster, you are a legend.

Thanks so much for your help.

Appreciate it.

Marcus

JamShady

2:50 pm on Jun 5, 2005 (gmt 0)

10+ Year Member



You should use $_SERVER['PHP_SELF'] instead of $PHP_SELF so your code is more secure (i.e. malicious users can't generate their own $PHP_SELF variable) and will work on servers with the register globals set to on or off

coopster

9:05 pm on Jun 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, JamShady, and good point.
Using Register Globals [php.net] has some more information, marcus76.

JamShady

3:26 am on Jun 6, 2005 (gmt 0)

10+ Year Member



Thanks :)