tags:

views:

66

answers:

3

Hello, I wanted to know if it is possible to treat a php file as a directory so that index.php/abc/def really calls index.php. The index.php should then know the subdirectory path (ie /abc/def).

I'm searching a plain php solution. I know that I could use mod_rewrite to map the directory to GET-parameters.

+1  A: 

A plain PHP solution is impossible since what is called will be the file.

The PHP in the file does not care where it is or how the file it is in is called other than for include purpose.

What you are looking for is to either convince the OS to pass the /abc/def/ as a parameter to the script or to get the Webserver to do the same thing (ie. mod_rewrite for apache).

Killer_X
Not true: Translating `index.php/abc/def` into `index.php` works out of the box on Apache. This is a very common technique to rewrite URLs in absence of mod_rewrite
Pekka
Yes, but it still is apache, not "pure PHP" as in something you could write in your php files and use in CLI and IIS...
Killer_X
+3  A: 

If I recall correctly, you should be able to parse out this info from $_SERVER['PHP_SELF'].

Edit: Even better, take a look at $_SERVER["PATH_INFO"].

George Marian
I don't have to configure the server to make this work, have I?
Mafi
@Mafi usually no, this'll work out of the box on a normally configured Apache.
Pekka
Thanks, that's what I searched.
Mafi
@Mafi You shouldn't have to make any config changes for this work. Here's a link to the server variables page of the PHP manual (it's the 2nd to last variable in the list): http://www.php.net/manual/en/reserved.variables.server.php
George Marian
+3  A: 

This will work if Apache's AcceptPathInfo directive is turned on, which is the default. From the manual:

The treatment of requests with trailing pathname information is determined by the handler responsible for the request. The core handler for normal files defaults to rejecting PATH_INFO requests. Handlers that serve scripts, such as cgi-script and isapi-handler, generally accept PATH_INFO by default.

you can query the path entered using the $_SERVER["PATH_INFO"] directive. You'll have to parse the paths yourself inside the script.

Pekka
+1 For referencing the Apache directive. While that directive seems familiar, it didn't come to mind.
George Marian