Page 1 of 1

$_SERVER['REQUEST_URI']

Posted: Tue Jun 15, 2010 1:24 pm
by tomnoble
Hi all,

I am new to PHP Programming, but I am looking to do the following. Here is the code I currently have:

Code: Select all

<?php

//Return the string after the domain name e.g /home
$currentpage = $_SERVER['REQUEST_URI'];

//Check to see if the current page is part of the Catalog
If ($currentpage=='/Catalog') //Check this syntax

{
// set up a switch and set the currency value appropriately

switch ($currentpage) {

case "/catalog/UK/";
//Set the currency value to UK here
break;

case "/catalog/EU/";
//Set the currency value to EU here
break;

case "/catalog/NonEU/";
//Set the currency value to NonEU here
break;

case "/catalog/US/";
//Set the currency value to US here
break;

case "/catalog/ROTW";
Echo $currentpage;
break;
   }
}

?>
The problem I am having is, in the IF Statement I want to put "If $currentpage=='/Catalog/*". Where the * means anything can come after it, but the start of the string must stay as "/Catalog/".

The same in the case statements, I want to allow any value to appear after that, I just need the start to be "/Catalog/<option>/"

If there a wild card symbol I can use to put in place after the last "/". Is there a way I can also make everything insensitive to case.

Kind Regards

Tom Noble

Re: $_SERVER['REQUEST_URI']

Posted: Tue Jun 15, 2010 1:35 pm
by AbraCadaver
You can use stripos() or preg_match() for the if. The switch would need to be something like this, though there is probably a better way to it than switch:

Code: Select all

switch (true) {

	case stripos($currentpage, "catalog/UK/"):
                //Set the currency value to UK here
		break;

        ///etc...
}
If you know the structure of the paths and they will be consistant, then you should be able to use something like this to get an array of things you need:

Code: Select all

$parts = explode('/', dirname($currentpage));

Re: $_SERVER['REQUEST_URI']

Posted: Tue Jun 15, 2010 1:37 pm
by AbraCadaver
In the switch case you have to omit the starting / because it will match and return position 0 which will not == true.

Re: $_SERVER['REQUEST_URI']

Posted: Tue Jun 15, 2010 1:50 pm
by tomnoble
Thanks for the prompt reply. I am using this code now (which I made from your reply):

Code: Select all

<?php

//Return the string after the domain name e.g /home
$currentpage = $_SERVER['REQUEST_URI'];

If (stripos($currentpage)=="/Catalog/")
// set up a switch and set the currency value appropriately

switch ($currentpage) {

case stripos($currentpage, "/catalog/UK");
//Set the currency value
break;

case stripos($currentpage, "/catalog/EU");
//Set the currency value
break;

case stripos($currentpage, "/catalog/NonEU");
//Set the currency value
break;

case stripos($currentpage, "/catalog/US");
//Set the currency value
break;

case stripos($currentpage, "/catalog/ROTW");
//Set the currency value
break;
   }
}
?>
Everything works as expected except the IF. Im sure Im just being stupid. Can you see what I am doing wrong?

Kind Regards

Tom

Re: $_SERVER['REQUEST_URI']

Posted: Tue Jun 15, 2010 1:53 pm
by requinix
1. Using the function in the wrong way. Check the manual
2. Use the appropriate string function

Re: $_SERVER['REQUEST_URI']

Posted: Tue Jun 15, 2010 1:54 pm
by AbraCadaver
You're not using stripos() correctly. Look up the parameters:

Code: Select all

// preferred
if (stripos($currentpage, "/Catalog/") !== false)

// or 

if (stripos($currentpage, "Catalog/"))

Re: $_SERVER['REQUEST_URI']

Posted: Tue Jun 15, 2010 2:02 pm
by tomnoble
AbraCadaver wrote:

Code: Select all

// preferred
if (stripos($currentpage, "/Catalog/") !== false)
[/quote]

Works perfectly. Thankyou for your help. And to others that posted on this. 

Kind Regards

Tom Noble

P.S for anyone having a similar issue, the final code is here. If I can contribute back to people then all the better
[syntax=php]<?php

//Return the string after the domain name e.g /home
$currentpage = $_SERVER['REQUEST_URI'];

if (stripos($currentpage, "/Catalog/") !== false)
{

// set up a switch and set the currency value appropriately

switch ($currentpage) {

case stripos($currentpage, "/catalog/UK");
//Set the currency value
break;

case stripos($currentpage, "/catalog/EU");
//Set the currency value
break;

case stripos($currentpage, "/catalog/NonEU");
//Set the currency value
break;

case stripos($currentpage, "/catalog/US");
//Set the currency value
break;

case stripos($currentpage, "/catalog/ROTW");
Echo $currentpage;
break;
   }
}
?>