Solved

Examine Part of URL

Posted on 2013-10-25
5
338 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
[X]
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
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 110

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
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.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 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