Solved

Examine Part of URL

Posted on 2013-10-25
5
332 Views
Last Modified: 2013-10-25
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:

http://mywebsite.com/admin/something.php
-or-
http://mywebsite.com/user/something.php

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

PHP 5.4

Thanks!
0
Comment
Question by:formadmirer
5 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 39600382
This is probably a good place to start

http://www.php.net/parse_url
0
 
LVL 18

Expert Comment

by:Steven Harris
ID: 39600385
Have you looked into dirname() for php4 and php5?

string dirname ( string $path )
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 39600396
<?php

$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

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39600641
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.
0
 

Author Closing Comment

by:formadmirer
ID: 39600666
This worked perfect for my needs - thank you!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now