4 ms·
Having a router allows you to not have all of your files be web accessible, by using a single point of entry and a whitelist for all possible paths. Most projec
by krapp 10y ago
Having a router allows you to not have all of your files be web accessible, by using a single point of entry and a whitelist for all possible paths. Most projects which map PHP files to URLs have everything in the web root.
If it's just a small brochure site with a few pages then it's no problem, but forums and larger projects built like this can leak information and expose vulnerabilities when PHP files which weren't meant to be directly accessed are, such as pages that perform SQL queries expecting variables in context that don't exist when accessed directly, or old config files, text files, open directories, etc.
But "url=page" is still basically routing using the query string, and IMHO so is access control using .htaccess. Any system where URLs are validated and where they don't directly point to files on the server counts.
- developer2 10y ago>> not have all of your files be web accessible cough wp-config.php cough
- combatentropy 10y ago> Having a router allows you to not have all of your files be web accessible Having a router may make it easier, but it's not the only way. You can just put your libraries, passwords, etc., in some directory outside the document root. /var/www/example.com/public /var/www/example.com/lib Then you use "include" to get them.
- krapp 10y agoHaving worked on projects with includes within includes within includes, and dealing with global variables defined in one file, used in another and reused elsewhere, it's definitely not the best way and it seems to destroy encapsulation. If you can keep it under control, though, of course it's no different than includes in C/C++ (it probably literally is a wrapper around the macros.) But modern PHP prefers using autoloaders, so you never actually have to use include statements to begin with. You could just as well define those directories and include them through a Composer setting.
- combatentropy 10y ago> includes within includes . . . global variables Yeah, that's a pain. You have to exercise a little discipline. Of course none of my code has that ;) With hand-typed routing, can't you accidentally define two routes that overlap, so some URLs could match both? The only reason it goes to one and not the other is something random like the order the routes are defined in the file? Like if you defined this route: /a/b/c Then you slept, added a bunch of code, came back in three months and put this in: /a/*/c Now you have two routes that match the same URL. And maybe they're separated by several lines of code, so that the mistake is hard to spot. You can't do that with a filesystem, put two files in the same place.
- krapp 10y ago> The only reason it goes to one and not the other is something random like the order the routes are defined in the file? To be fair, that's not random, that's pretty explicit. And if you're optimizing for speed, short circuiting at the first match is a good idea. But if you're using a router, chances are you're passing the segments as arguments to some function or method anyway, so route /a/*/c and /a/b/c should both wind up returning the same content if the second segment in both cases is 'b'.