Recent Comments

Thursday, October 16, 2008

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.

echo "
";
print_r ($_SERVER);
echo "
";
?>

All elements by $_SERVER will appear in your browser window.

I made function which returns current page URL:

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:

echo currenturl();
?>

0 comments: