How to Get the Page URL with php code?

Posted by om 4 March, 2009

Every php coder ask questions to get CURRENT PAGE URL with PHP, it is not as straightforward in raw php coding as one may think to get the current url to use it inside your application. Here is a code which you can use to get url easily.


function CPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
  • Share/Bookmark
Categories : PHP Tags :

Comments
August 28, 2009

Nice tutorial i learned a lot thanks keep up the good work.

Posted by Christopher Cuizon
August 28, 2009

Thanks for appreciation, let me know if anything specific you need in php.

Posted by om
November 6, 2009

Do you have a tutorial on $curl>

cause i am very much interested on this topics..by the way this site is so cool .. A site for programmers..Great work

Posted by kitchen_naskti
February 22, 2010

Replace the use of $_SERVER["SERVER_NAME"] variable by $_SERVER["HTTP_HOST"]
IF you use a shared server hosting this cause problems
:)

Posted by Christian
Leave a comment

(required)

(required)