Skip to content

Instantly share code, notes, and snippets.

@itsjavi
Created February 20, 2014 09:12
Show Gist options
  • Save itsjavi/9109741 to your computer and use it in GitHub Desktop.
Save itsjavi/9109741 to your computer and use it in GitHub Desktop.
Current request path in PHP
<?php
function url_path(){
// Base path
$script_name = isset($_SERVER["SCRIPT_NAME"]) ? $_SERVER["SCRIPT_NAME"] : '';
$base_path = '';
if (!empty($script_name)) {
$base_path = trim(str_replace('\\', '/', dirname($script_name)), '/ ');
}
// Path
$path = explode("?", trim(isset($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : '', " /"), 2);
$path = $path[0];
if (!empty($base_path)) {
$path = preg_replace("/^" . str_replace('/', '\/', $base_path) . "/", "", $path);
}
return preg_replace("/^" . preg_quote(basename($script_name)) . "\/?/", "", trim($path, " /"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment