$_SERVER['REQUEST_URI']

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomnoble
Forum Commoner
Posts: 28
Joined: Tue Jun 15, 2010 1:18 pm

$_SERVER['REQUEST_URI']

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: $_SERVER['REQUEST_URI']

Post 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));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: $_SERVER['REQUEST_URI']

Post by AbraCadaver »

In the switch case you have to omit the starting / because it will match and return position 0 which will not == true.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tomnoble
Forum Commoner
Posts: 28
Joined: Tue Jun 15, 2010 1:18 pm

Re: $_SERVER['REQUEST_URI']

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: $_SERVER['REQUEST_URI']

Post by requinix »

1. Using the function in the wrong way. Check the manual
2. Use the appropriate string function
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: $_SERVER['REQUEST_URI']

Post 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/"))
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tomnoble
Forum Commoner
Posts: 28
Joined: Tue Jun 15, 2010 1:18 pm

Re: $_SERVER['REQUEST_URI']

Post 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;
   }
}
?>
Post Reply