Page 1 of 1

Stripping the folder from a URL

Posted: Wed Jun 03, 2009 12:50 pm
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.

Re: Stripping the folder from a URL

Posted: Wed Jun 03, 2009 2:23 pm
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']));

Re: Stripping the folder from a URL

Posted: Wed Jun 03, 2009 2:31 pm
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...

Re: Stripping the folder from a URL

Posted: Wed Jun 03, 2009 2:37 pm
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.

Re: Stripping the folder from a URL

Posted: Wed Jun 03, 2009 2:49 pm
by Inkyskin
ah lol i see :) i'll give that a go, thanks!

Posted: Wed Jun 03, 2009 8:19 pm
by Jenk
alternative..

Code: Select all

$folders = str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath('.'));
$stripped = str_replace($folders, '', $_SERVER['REQUEST_URI']);
untested, but should work.

Re:

Posted: Wed Jun 03, 2009 9:51 pm
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!

Re: Stripping the folder from a URL

Posted: Thu Jun 04, 2009 3:06 am
by Inkyskin
That works great, thanks!

Re: Stripping the folder from a URL

Posted: Thu Jun 04, 2009 4:18 am
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.