Stripping the folder from a URL

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Stripping the folder from a URL

Post by Inkyskin »

Hi all,

I'm starting a new project soon, but I'm stumped on something. The base of the system will essentially be a mini framework - but I want to make it as easy as possible to install on a server. Problem is (for me), that it will most likely be placed in sub folders on servers. I don't want to have the user tell the app what folder it's in either.

Take the following:

Code: Select all

 
Main URL:
http://www.mysite.com
 
App Folder:
httpdocs/stuff/subFolder/app_in_here
 
Exmaple URL the app might get
http://www.mysite.com/stuff/subFolder/c ... /var2/var3
 
Is there a tried and tested method (or function I have plainly missed!), that would let me strip 'stuff/subFolder/' or 'http://www.mysite.com/stuff/subFolder/' from the URI, so I'm only left with 'controller/action/var1/var2/var3'?

Maybe there's a way to find out what the main htdocs folder is, but then it's not always called htdocs.

I could possible take the Request URI, and work forwards on my full app dir structure, stripping things if the exist, but if I had a controller named 'subFolder' for example, that might strip too much...

Yours, Mr Extremely Stumped.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Stripping the folder from a URL

Post by allspiritseve »

Here's what I use:

Code: Select all

$root = str_replace ($_SERVER['DOCUMENT_ROOT'], '', dirname ($_SERVER['SCRIPT_FILENAME']));
$url = new UrlHelper ($root, true);
$parts = explode ('/', str_replace ($url->root(), '', $_SERVER['REQUEST_URI']));
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: Stripping the folder from a URL

Post by Inkyskin »

That looks like it's from an existing framework (CI?), I'll be creating this from scratch - not using an existing system (Its a small app, and I don't really need the bloat of a "proper" framework) - but that means I don't have the access to any ready built classes to help.

It's really the theory I need to grasp...
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Stripping the folder from a URL

Post by allspiritseve »

Nope, its homegrown and pretty simple, the only class in that is the Url class, and you really don't need to know how it works other than the $root variable that's passed in the constructor is the same as $url->root(). I'll go through it line by line for you:

Code: Select all

$root = str_replace ($_SERVER['DOCUMENT_ROOT'], '', dirname ($_SERVER['SCRIPT_FILENAME']));
This finds the 'folder' in the URL.

Code: Select all

$url = new UrlHelper ($root, true);
Ignore this line
 

Code: Select all

$parts = explode ('/', str_replace ($url->root(), '', $_SERVER['REQUEST_URI']));
This takes the URI and removes the folder name, so all that is left is the clean URL for you to work with. It splits it by / to get sections for you to use to find the correct page.
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: Stripping the folder from a URL

Post by Inkyskin »

ah lol i see :) i'll give that a go, thanks!
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

alternative..

Code: Select all

$folders = str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath('.'));
$stripped = str_replace($folders, '', $_SERVER['REQUEST_URI']);
untested, but should work.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re:

Post by allspiritseve »

Jenk wrote:alternative..

Code: Select all

$folders = str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath('.'));
$stripped = str_replace($folders, '', $_SERVER['REQUEST_URI']);
untested, but should work.
Cool, seems to work for me. I didn't know about that realpath() function, thanks!
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: Stripping the folder from a URL

Post by Inkyskin »

That works great, thanks!
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: Stripping the folder from a URL

Post by Paul Arnold »

Sorry to hijack but I think this may be useful to you too.
Does anyone have a good preg_replace / preg_match to validate characters used in the URI?
I've been looking round for a while and can't find anything that works properly.
Post Reply