PHP

HOW TO GET FULL URL & URL PARTS IN PHP

PROTOCOL echo isset($_SERVER["HTTPS"])     ? "https://" : "http://";   HOST echo $_SERVER["HTTP_HOST"];

$_SERVER SUPERGLOBAL

01

PORT echo $_SERVER["SERVER_PORT"];   PATH + FILE + QUERY echo $_SERVER["REQUEST_URI"];   QUERY STRING echo $_SERVER["QUERY_STRING"];

FULL URL (WITH QUERY STRING) $full = (isset($_SERVER["HTTPS"])   ? "https://" : "http://")  .   $_SERVER["HTTP_HOST"] .   $_SERVER["REQUEST_URI"];   REMOVE QUERY STRING $full = strtok($full, "?");

HOW TO GET FULL URL

02

PROTOCOL + DOMAIN $host = isset($_SERVER["HTTPS"])  ?   "https://" : "http://"  .   $_SERVER["HTTP_HOST"] ;

COMMON URL PARTS (A)

03

PATH ONLY $path = parse_url(   $_SERVER["REQUEST_URI"], PHP_URL_PATH );

FILENAME ONLY $file = basename(   $_SERVER["REQUEST_URI"], "?".   $_SERVER["QUERY_STRING"]);

COMMON URL PARTS (B)

04

PATH + FILE $filepath = strtok($_SERVER ["REQUEST_URI"], "?");