Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Examine Part of URL

Posted on 2013-10-25
5
335 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 109

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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

828 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