Forum Moderators: coopster

Message Too Old, No Replies

PHP IF/ELSE Statement based on URL

PHP IF/ELSE Statement based on URL

         

ChildeRoland

1:10 pm on Apr 21, 2009 (gmt 0)

10+ Year Member



Is there a way to create an if/then statement using PHP to say

IF
(the url in the address bar contains an = sign)
Action: display the following code

ELSE
(if the url in the address bar DOES NOT contain an = sign)
Action: display the following code

Thanks in advance. We're using a Magento Commerce site and they don't have anywhere where it tells us how to make a static block disappear on a category page, if a left column filter is applied. My easy fix is IF its possible to create this if/else statement based on the URL, then the left column filter's, when selected, causes something like this to be added to the end of the URL, ?cat=87&price=100

So in short, if I can control the if/else statement via telling it if an = sign appears ANYWHERE IN THE URL, show something else, then I'm good to go.

henry0

2:24 pm on Apr 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php

$str = 'http://www.webmasterworld.com/php/3896968.htm +';
if (eregi('\+', $str)) {
echo "'$str' contains a '+'!";
}
else
{
echo" $str does not contain a '+'";
}

?>

idfer

8:18 pm on Apr 21, 2009 (gmt 0)

10+ Year Member



I can't quite follow the part about Magento Commerce, so i hope i'm answering the right question... You want to know if the URL contains any CGI parameters, e.g. example.com/script.php?x=1 ? There are two ways you can do this: either check to see if $_SERVER['QUERY_STRING'] is non-empty (or contains an = to be really safe), or if the global array $_REQUEST is non-empty. So...

if(strpos($_SERVER['QUERY_STRING'], '=') !== false)
// Note the double-equal in !==

-or-

if(!empty($_REQUEST))

If you want to know if the = sign appears in the URI part (PHP separates the stuff following the ? into a separate variable), check to see if $_SERVER['REQUEST_URI'] contains an '='. So...

if(strpos($_SERVER['REQUEST_URI']), '=') !== false)

Here's the documentation for $_SERVER [php.net]. Hope this helps.