tags:

views:

309

answers:

6

This has to be pretty simple, but I'd like to parse the current URL and execute conditional code depending on whether the user is on the /sitemap/ directory.

So for example, if the site is example.com, and if the request is example.com/sitemap/.

Then I want to execute conditional code in that case. I'm using wordpress so I'm not sure if there is a built-in function that gets this...

A pure PHP solution is fine.

+1  A: 

I don’t know if Wordpress has some function for that. But you could do this:

$_SERVER['REQUEST_URI_PATH'] = preg_replace('/\\?.*/', '', $_SERVER['REQUEST_URI']);

$_SERVER['REQUEST_URI'] contains the URI path plus the query and the code above will just get the URI path. Then you can compare it to your string:

if ($_SERVER['REQUEST_URI_PATH'] == '/sitemap/') {
    // URI path is "/sitemap/"
}

Or you do a starts with test:

$start = '/sitemap/';
if (substr($_SERVER['REQUEST_URI_PATH'], 0, strlen($start)) == $start) {
    // URI path starts with "/sitemap/"
}
Gumbo
Thanks Gumbo. Exactly what I was looking for.
Scott B
A: 

You can use build-in web server variables for this. Here are some example:

  • $_SERVER['HTTP_HOST'] - host name for current request
  • $_SERVER['REQUEST_URI'] - requested url with get parameters

You can also see full list by print_r($_SERVER);.

Ivan Nevostruev
A: 
$paths = explode ('/', $_SERVER['PHP_SELF']);

This will give you an array of paths for the given script. You can perform logic based on the resulting array.

ChronoFish
Note: PHP_SELF is the "web path" where as PATH_TRANSLATED is the "file system path"
ChronoFish
As noted by Ivan HTTP_HOST will give you your host name....
ChronoFish
+2  A: 

And you run phpinfo() in a simple script, you will see EVERYTHING you might want to get your hands on from pure PHP. (and a lot of other stuff too, just scroll to the bottom of the output for PHP variables.)

Don
While a round-a-bout answer, I was thinking of suggesting the same thing. I'm glad you threw it out there.
ChronoFish
A: 
current( explode( '/', trim( $_SERVER['REQUEST_URI'], '/' ) ) )

will return the first part of the url

explode( '/', trim( $_SERVER['REQUEST_URI'], '/' ) )

will create an array of the url parts

Galen
A: 

With the following line of PHP code you will get the URL of the HTTP Request of the current page:

$url = ((preg_match("/(^\z|^off\z)/i", $_SERVER['HTTPS'])) ? 'http://' : 'https://') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

Then you may want to parse this URL to get a reliable result array like this:

  • [SCHEME] => http
  • [AUTHORITY] => user:[email protected]:80
  • [USERINFO] => user:pass
  • [USER] => user
  • [PASS] => pass
  • [HOST] => www.domain.com
  • [REGNAME] => www.domain.com
  • [DOMAIN] => www.domain.com
  • [LABEL][] =>
  • [LABEL][] => com
  • [LABEL][] => domain
  • [LABEL][] => www
  • [PORT] => 80
  • [PATH] => /dir1/dir2/dir3/page.html
  • [SEGMENT][] => dir1
  • [SEGMENT][] => dir2
  • [SEGMENT][] => dir3
  • [SEGMENT][] => page.html
  • [QUERY] => key1=value1&key2=value2
  • [GET][key1] => value1
  • [GET][key2] => value2
  • [FRAGMENT] => anchor/line
  • [ANCHOR][] => anchor
  • [ANCHOR][] => line

There's a standard-compliant, robust and performant PHP Class for handling and parsing URLs / URIs according to RFC 3986 and RFC 3987 available for download and free use:

http://andreas-hahn.com/en/parse-url

Andreas M. Hahn