Sunday, September 1, 2024

PHP rewriting path names to index.php without trailing slashes and losing POST info

One drawback of "raw PHP" style coding is often the lack of a router... I've rolled my own at least once (with a mix of .htaccess style rewrites parsing out the path 'by hand' in scrip to a single router file) but usually I just punt by using "[approot]/[ACTIONNAME]/index.php" folders - letting the ability to link to "[approot]/[ACTIONNAME]/" as the URL hide my horrible indiscretion of using PHP. 

 (I do feel like the simplicity PHP offers is making some level of comeback - and for me it has a proven track record of stability and simplicity over decades - when you come back to code, it's - lo and behold - still working years later and most often not THAT hard to trace through, with plenty of transparency)

One mistake I vaguely remembered making before has to do with how Apache and/or PHP rewrites can lose POST information.

Lets say I was making an app for easily conducting surveys and was hosting it at myserver.com/survey/

If I had a form that started

<form action="/survey/save" method="POST">

I might be in trouble without trailing slash! Looking at the network tab, it looks like there's a jump from /survey/save to /survey/save/ (and from thence to /survey/save/index.php) and so information doesn't actually get POSTed. But including in the trailing slash lets it work just fine.


No comments:

Post a Comment