View Full Version : domain q: www.foo.com/bar, $x='bar'; how to do this?

08-29-2007, 04:54 PM
If I have a URL for my website:


I want to parse just the subdir 'bar' so that I get

$x = 'bar'

Similarly if I have http://www.foo.com/bar/poo/

I want

$y = 'bar/foo'

is there a way to do this?

08-29-2007, 05:15 PM
Try using $_SERVER['REQUEST_URI'] predefined constant (or some other).

eg if you are loading:

$_SERVER['REQUEST_URI'] would be equal to "/test/index.php?id=1&test=wet&id_theme=512"

08-29-2007, 05:44 PM
That's not a proper solution.

The server would receive the request at domain.com/bar/foo/ and look for the proper file there. Obviously it wouldn't be there.

A few frameworks use this technique (and I've used it as well). You need to use mod_rewrite to rewrite somewhat like this: domain.com/bar/foo/ becomes: index.php?url=bar/foo/

Then your PHP script can parse out the specifics.

08-29-2007, 08:25 PM
Actually, I am not doing this for a mod_rewrite.

I am doing this for a menu system.

I have a single script on the root which serves as a menu system. I have hundreds of pages within various dirs and subdirs on my server for my website. I want to use only the one menu script for my entire site.

The menu is set up as a parent menu "Home About Forums Contact Us Tools, etc" and a sub menu for each item from the parent menu.

So "Tools" would have "Blogging Bookmarks RSS" as submenus.

So, if the user is currently browsing a page under the "foo.com/tools/" menu I would have an if statement saying something like:

if(($_SERVER['REQUEST_URI']) = 'tools/'){
echo '<a href="">Blogging</a> <a href="">Bookmarks</a> <a href="">RSS</a>';

I don't want to have to make a hundred menu scripts for each and every page.

Is this a dumb way to solve this problem? Is there a better way?

08-29-2007, 08:28 PM
aedrin has the proper solution. You will need mod_rewrite to accomplish what you're looking to do with your menu system.

08-29-2007, 09:19 PM
Well, you could either check the entire URL for the presence of "tools" using strpos() (http://www.php.net/strpos) or else if you need the exact part of the URL as indicated in your original post you could explode (http://www.php.net/explode) the URL around "/" then return the appropriate part of the resulting array.