About Blog Login

PHP Routing

So, how do we do handle multiple URIs in the same PHP file? We use the variable $_SERVER['REQUEST_URI']. This will give us the full URI, including the query string. Basically anything after the server-name, starting from the first / character.

The HTTP method (GET, POST, PUT and others) will be in $_SERVER['REQUEST_METHOD'].

A very crude but working approach to this is to first obtain these in the index.php, then do a long list of if, elseif statements, or a long switch. This works for the simplest applications but will quickly get too long and error prone if you have more than a handful of pages.

Add a Comment