Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Examine Part of URL

Posted on 2013-10-25
5
Medium Priority
?
341 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 2000 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 111

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

715 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