The Road to Success – The Daily WTF

Imagine you are building a PHP web application and you need to display different forms on different pages. Now, for most of us, we’d probably be using some kind of framework to solve this problem, but even if we weren’t, the obvious solution of “use a different PHP file for each screen” is a pretty obvious solution.

i dare say also obvious solution?

What if we could have one page that handles requests for many different URLs? Thinking about the convenience of ONE file to run an entire application? Think about ifwith.

   	if( substr( $_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], "=" ) + 1 ) == "request" ) 
   	
   		echo "<form name=\"request\" action=\"\" method=\"post\" enctype=\"multipart/form-data\" onsubmit=\"return validrequest();\">\n";
   	
   	else if( substr( $_SERVER['REQUEST_URI'], strrpos($_SERVER['REQUEST_URI'], "=" ) + 1 ) == "response" ) 
   	
   		echo "<form action=\"\" method=\"post\" onsubmit=\"return validresponse()\">\n";
   	
   	else if( substr( substr( $_SERVER['REQUEST_URI'], stripos($_SERVER['REQUEST_URI'], "=" ) + 1 ), 0, 7 ) == "respond" ) 
   	
   		echo "<form name=\"respond\" action=\"\" method=\"post\" enctype=\"multipart/form-data\" onsubmit=\"return validresponse();\">\n";
   	
   	else if( substr( substr( $_SERVER['REQUEST_URI'], stripos($_SERVER['REQUEST_URI'], "=" ) + 1 ), 0, 6 ) == "upload" )
   	
   		echo "<form name=\"upload\" method=\"post\" action=\"\" enctype=\"multipart/form-data\">\n";
   	
   	else if( substr( substr( $_SERVER['REQUEST_URI'], stripos($_SERVER['REQUEST_URI'], "=" ) + 1 ), 0, 8 ) == "showitem" ) 
   	
   		echo "<form name=\"showitem\" action=\"\" method=\"post\" enctype=\"multipart/form-data\">\n";
   	
   	else if( substr( substr( $_SERVER['REQUEST_URI'], stripos($_SERVER['REQUEST_URI'], "=" ) + 1 ), 0, 7 ) == "adduser" ) 
   	
   		echo "<form name=\"adduser\" action=\"\" method=\"post\" onsubmit=\"return validadduser();\">\n";
   	
   	else if( substr( substr( $_SERVER['REQUEST_URI'], stripos($_SERVER['REQUEST_URI'], "=" ) + 1 ), 0, 8 ) == "edituser" ) 
   	
   		echo "<form name=\"adduser\" action=\"\" method=\"post\" onsubmit=\"return validedituser();\">\n";
   	
   	else
   	
   		echo "<form action=\"\" method=\"post\">\n";
   	

Someone reinvented routing, bad. We split the requested URL into =, so we can compare the tail of the array to one of our defined routes. Oops, no, we don’t divisionwe take a substring, which means we couldn’t have a route upload_image or showitemssince they would collide with upload and showitem.

And yes, you can safely assume there are a bunch more ifwith that control which specific form fields get output.

[Advertisement]

ProGet has you covered with security and access controls for your NuGet feeds. Find out more.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *