Change Language:
USA English French Deutsch (Germen) Spanish Portugese Italian Nederlands (Dutch) Polish Russian Swidesh Arabic Korian Chinese Japanese UK English Bookmark and Share

Get current URL in php

PHP: How to Get the Current Page URL
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();
?>


See Also or Related Articles:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's guide,howto,reference,examples,samples,source code,tags,demos,tips,links') as ' at line 1