...

View Full Version : $_SERVER['REQUEST_URI'] wildcard question



Bobafart
01-04-2008, 01:44 AM
Is there a way to utilize $_SERVER['REQUEST_URI'] in an IF statement so that the condition applies to all files under a specific subdir?

so for the subdir www.foo.com/people/ -- all links under this will apply to the below conditional?

thanks



if(($_SERVER['REQUEST_URI']) == '/people/'){

// do something

}

ziggy1621
01-04-2008, 02:36 AM
I would set it as a variable first


$folder = $_SERVER['REQUEST_URI'];

if ($folder=='people'){
echo("yes its people")
}
else{
echo("no its not")
}


but to answer.. yes you can... I use it to regulate meta tags by filename


$page=$_SERVER['PHP_SELF'];
if($page==....

Bobafart
01-04-2008, 03:15 AM
I don't think that does it.. let me elaborate.

If I have the following URLs:

www.foo.com/people/adam
www.foo.com/people/tom/
www.foo.com/people/sue/
www.foo.com/people/mary/
www.foo.com/people/joe
...
and an infinite more based on various user names
...

www.foo.com/people/mack
www.foo.com/people/sally/

how do I make an IF conditional that applies to every subdir under the parent dir "/people/"?

something like:


if(($_SERVER['REQUEST_URI']) == '/people/*'){


where * is a wildcard would be nice.. but that is obviously incorrect.

ziggy1621
01-04-2008, 03:37 AM
ah, i got you... now we are getting sticky... (i'm about to go to sleep, so i'll get to the reply in the a.m.) but, my mind is thinking that you will need to obtain the entire url (http://foo.com/people/sally/) then explode it by '/' (after removing the http://) and have it as:

url[0]=www.foo.com
url[1]=people
url[2]=sally

then if url[1]=='people' && url[2]!='', then do whatever

again, its getting late and i'm running on fumes, but i think that is a start in the right direction... tell me if i'm still way off

Inigoesdr
01-04-2008, 06:08 AM
Generally you would use mod_rewrite to pass the page(people) and the name to a PHP page that would generate the content from there.


RewriteEngine On

# Ignore all real files and directories.
RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule .* - [L]

RewriteRule ^people/([^/]+)/?$ /people.php?name=$1 [QSA,L]

This example just passes the name, which should be fine for your purposes.

ziggy1621
01-04-2008, 02:34 PM
Generally you would use mod_rewrite t...

i was definitely taking a long way around

i-am-ZZZ
08-08-2012, 07:10 PM
I know this thread is quite old but on my search for a solution to this problem I came across these solutions and none helped in my case.

As I found an additional solution which solved my problem I hope that maybe someone else can benefit from my approach:



$url = $_SERVER['REQUEST_URI'];
$subdir = substr($url,0,8);
if ( $subdir == '/people/' ) {

// whatever you want to do in this case

}


Of course you have to modify substr and the if-statement to match your situation.

MarkR
08-09-2012, 12:18 PM
Could also use regex:




$ptn = "@/people/*@";
$str = "/people";
preg_match($ptn, $str, $matches);


I'm a bit of a regex noob so I wouldn't take it at face value.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum