• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

Examine Part of URL

Hello all. I'm trying to figure out how to parse part of a url to determine the associated directory. For example, the URL is:


I need to determine if the URL contains 'admin' or 'user'.

PHP 5.4

1 Solution
Beverley PortlockCommented:
This is probably a good place to start

Steven HarrisPresidentCommented:
Have you looked into dirname() for php4 and php5?

string dirname ( string $path )
Beverley PortlockCommented:

$bits = parse_url("http://example.com/user/somedir/index.html");

if ( preg_match( '#^/admin/.*?$#', $bits['path'] ) )
     echo "contains 'admin'";

if ( preg_match( '#^/user/.*?$#', $bits['path'] ) )
     echo "contains 'user'";

Open in new window

Ray PaseurCommented:
You can find this information in $_SERVER["HTTP_HOST"] and $_SERVER["REQUEST_URI"].  The HTTP_HOST will be mywebsite.com and the REQUEST_URI will be /admin/something.php

But that said, whenever I see a question like this, I wonder about the back-story.  Why does your PHP script need to know this information?  There is almost always some reason that can build a security flaw into your design.
formadmirerAuthor Commented:
This worked perfect for my needs - thank you!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now