How to Get the Page URL with php code?

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

4 thoughts on “How to Get the Page URL with php code?

  1. Christian

    Replace the use of $_SERVER[“SERVER_NAME”] variable by $_SERVER[“HTTP_HOST”]
    IF you use a shared server hosting this cause problems
    🙂

  2. kitchen_naskti

    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

Leave a Reply

Your email address will not be published. Required fields are marked *