3 ms·
"every PHP app"?? What about apps that don't limit themself to just index.php? I never understood this obsession with having everything flow through index.php
by ars 15y ago
"every PHP app"??
What about apps that don't limit themself to just index.php?
I never understood this obsession with having everything flow through index.php. Apache already has a method to dispatch requests to the proper controller AKA file. Why rewrite it again?
This is fictional security. If someone can upload a file, they can also upload index.php (and from my experience cleaning up a few hacks, that is exactly what they do).
And if you (correctly) set it so they can't write to the top level directory, what you really should do is remove the php handler from the directories they can write to.
- kijin 15y agoHaving everything go through index.php (the "front controller") makes it a lot easier to manage common libraries and use PHP's autoloading capability. You don't have to add a bunch of includes to every script. You don't have to change every script if you decide to replace one of the common libraries. You can force every request to go through the same set of checks (such as authentication). You can have pretty URLs like "/blog/post/123". The front controller pattern doesn't have much to do with security, though. It also doesn't work with apps like WordPress.
- jontas 15y agoI personally run my applications entirely through index.php but your argument does not make sense. You can easily include one file at the top of all your scripts and have that handle things like autoloading, authentication, etc. Then if you need to make a change you are still only changing it one spot.
- kijin 15y agoIf you have a front controller, you don't even need to repeat that single include in any file. Why repeat the same include in 100 different files if you can avoid it? Also, what if you realize after a while that you need to change the location of the single include file because you carelessly placed it in a publicly accessible directory (bad for security) ? Now you have to edit 100 files. Another reason: What would your templates look like if your app had 100 different entry points? It would seem silly to have 100 template files in addition to 100 PHP scripts. So the developer is most likely to mix business logic with templates in the same file(s). Not good. Of course, if you're a good enough developer to know that you should avoid problems like this, it doesn't really matter how you organize your site. But I think that the front controller pattern helps to push developers (especially novices) toward the right direction.
- roel_v 15y ago"I never understood this obsession with having everything flow through index.php. Apache already has a method to dispatch requests to the proper controller AKA file. Why rewrite it again?" How? Is there a way to let Apache, for every request to host.domain.com execute /var/www/site/index.php ? I've seen a post somewhere claiming that years ago, and I've tried many times to get it to work or re-find that post but I never managed to do so.
- ars 15y agoYes, there is a way by rewriting (not redirect) the url. RewriteEngine on RewriteCond %{REQUEST_URI} !^/index.php RewriteRule ^/(.*) /index.php/$1 I didn't test it, but it's probably as simple as that. But that's not what I meant. Instead of having index.php look at the url parameters and include the proper controller, just directly execute that controllers .php file. Make a file that you include in each controller that does any prep work you need and that's it, let apache execute the controller directly.
- roel_v 15y agoYes, using mod_rewrite is what I've been doing for years. My question was on the second situation - how do you let Apache execute the controller directly?
- snorkel 15y agoNo, just because a site allows users to upload files doesn't mean they can replace the top-level index.php, for if that were true then whoever setup that server needs to look for job opportunities in the circus. The problem with removing the PHP handler from all other writable directories is the reality of dealing with many 3rd party modules, themes, and apps that have their own upload directories, and sure you can audit all 3rd party code and rewrite it yourself, but you should also assume the code is full of holes anyway and configure the other layers in your stack according to the principle of least privilege.