Last active
December 17, 2015 08:39
-
-
Save varemenos/5581503 to your computer and use it in GitHub Desktop.
PHP - Basic URL Routing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if(!empty($_REQUEST)){ | |
// get real parameters | |
$parameters = $_REQUEST; | |
} | |
if(isset($_SERVER['PATH_INFO'])){ | |
// path = the trimmed PATH_INFO string | |
// allowed characters: | |
// {'a','b',..,'z'},{'A','B',...,'Z'},{'0','1',...,'9'},{'_','/','.'} | |
// every other character will be replaced with an empty string | |
$temp = $_SERVER['PATH_INFO']; | |
$path = preg_replace('/[^a-zA-Z0-9_\/.]/', '', strtolower($_SERVER['PATH_INFO'])); | |
// check if inserted url and cleaned url are the same | |
if($temp != $path){ | |
// if they are not then print an error message | |
echo '<span class="warning">please make sure the current link is correct. Some characters were omitted. | |
<br>From "'.$temp.'" to "'.$path.'".</span>'; | |
} | |
// explode the path string into an array of commands | |
$commands = explode('/', $path); | |
// calculate the amount of commands | |
$amount_of_commands = count($commands); | |
for ($i=0; $i < $amount_of_commands; $i++) { | |
// if current command is an empty string | |
if($commands[$i] == '' || $commands[$i] == 'index.php'){ | |
// then remove it | |
unset($commands[$i]); | |
} | |
} | |
// reset the array keys | |
$commands = array_values($commands); | |
// var_dump($commands); | |
} | |
// initialize type object | |
$type->name = null; | |
$type->id = 0; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment