Get current URL in PHP
PHP programmers need sometimes current page URL to show or save in database. There is no built-in function exist in PHP to get current page url. I made a function to show current page URL.
I used the following built-in $_SERVER array in this function. If you want to show all element return by $_SERVER array then you can use these commands.
.
.
.
.
<?php echo "<pre>"; print_r ($_SERVER); echo "</pre>"; ?>
All elements by $_SERVER will appear in your browser window.
I made function which returns current page URL:
<?php
function currenturl() {
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
}
function strleft($s1, $s2) {
return substr($s1, 0, strpos($s1, $s2));
}
?>
Example using function above:
<?php echo currenturl(); ?>