wzul

Get Route After "index.php"

Jul 31st, 2017
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.46 KB | None | 0 0
  1. <?php
  2.  
  3. function get_route()
  4. {
  5.     $str = '';
  6.     /*
  7.      * $_SERVER['SCRIPT_NAME'] will provide full sub-directory path after domain until "index.php" file.
  8.      * Example: www.google.com/aa/bb/index.php => It will provide /aa/bb/index.php
  9.      *
  10.      * Preg Replace: Only allow A-Z, a-z, 0,9 and special character "/" and "."
  11.      */
  12.     $script_name = explode('/', preg_replace("/[^a-zA-Z0-9\/\.]+/", "", $_SERVER['SCRIPT_NAME']));
  13.  
  14.     /*
  15.      * Find array index until found the script name.
  16.      * Save the array index number at variable $i
  17.      */
  18.     for ($i = 0; $i < sizeof($script_name); $i++) {
  19.         if (strtolower($script_name[$i]) === 'index.php') {
  20.             /*
  21.              * Set the maximum $i value to strip string from Request URI
  22.              */
  23.             break;
  24.         }
  25.     }
  26.  
  27.     /*
  28.      * $_SERVER['REQUEST_URI'] will provide full sub-directory path after domain.
  29.      * Example: www.google.com/aa/bb/index.php/aa/cc => It will provide /aa/bb/index.php/aa/cc
  30.      * ______________________
  31.      *
  32.      * Assuming www.google.com has index.php and mode rewrite enabled
  33.      * Example: www.google.com/aa => It will provide /aa
  34.      *
  35.      * Preg Replace: Only allow A-Z, a-z, 0,9 and special character "/" and "."
  36.      */
  37.     $request_uri = explode('/', preg_replace("/[^a-zA-Z0-9\/\.]+/", "", $_SERVER['REQUEST_URI']));
  38.  
  39.     /*
  40.      * Set the array index of $request_uri to match with the current directory name of "index.php"
  41.      */
  42.     for ($a = 0; $a < sizeof($request_uri); $a++) {
  43.         if (strtolower($request_uri[$a]) === $script_name[($i - 1)]) {
  44.             /*
  45.              * If there is presence "index.php" name in request uri (in-case mod rewrite not enabled)
  46.              * +1 the counter
  47.              */
  48.             if (strtolower($request_uri[($a + 1)]) === 'index.php') {
  49.                 $a++;
  50.             }
  51.  
  52.             /*
  53.              * +1 the counter to get directory or path after the current script directory folder or after "index.php"
  54.              */
  55.             $a++;
  56.  
  57.             /*
  58.              * Iterate the loops until the end of the route
  59.              */
  60.             for (; $a < sizeof($request_uri); $a++) {
  61.                 if ($a === (sizeof($request_uri) - 1)) {
  62.                     $str .= $request_uri[($a)];
  63.                 } else {
  64.                     $str .= $request_uri[($a)] . '/';
  65.                 }
  66.             }
  67.             break;
  68.         }
  69.     }
  70.  
  71.     return $str;
  72. }
Add Comment
Please, Sign In to add comment