Advertisement
lignite0

Relative REQUEST_URI

Sep 18th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.60 KB | None | 0 0
  1. /**
  2.  * Define the current relative URI.
  3.  *
  4.  * @return string
  5.  */
  6. public function getCurrentUri()
  7. {
  8.     // Get the current Request URI and remove rewrite basepath from it (= allows one to run the router in a subfolder)
  9.     $basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)).'/';
  10.     $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
  11.  
  12.     // Don't take query params into account on the URL
  13.     if (strstr($uri, '?')) {
  14.         $uri = substr($uri, 0, strpos($uri, '?'));
  15.     }
  16.  
  17.     // Remove trailing slash
  18.     $uri = '/'.trim($uri, '/');
  19.  
  20.     return $uri;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement