Home >>PHP Programs >How to Get the Current Page URL

How to Get the Current Page URL

Get the Current Page URL

With the help of this tutorial page, you can see the Current Page URL. Sometimes, you might want to get the current page URL that is shown in the browser URL window. For example if you want to let your users submit a comment on any post, you want to get that same exact URL. Here is how you can do that. Add the following code to a page:
<?php

 function currentPageURL()

 {

 $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;

}

?>

Now see the current page url using currentPageURL() function

<?php
 
echo currentPageURL();

?>
In the given above example Define a function named currentPageURL( ). Create a variable $PageURL with value "http". Now, with the help of Super Global Variable $_SERVER['https'] checked server is secure server or not.Used Global variable $_SERVER["SERVER_PORT"] which returns the current port of the server, Now checked if the port number is 80, it means server is local server, then displayed the server name using $_SERVER["SERVER_NAME"] and page url using $_SERVER["REQUEST_URI"] and assign the values inside $PageURL variable. $PageURL variable returns the server name, server port and server url. Call the function currentPageURL( ) to display the current page URL.

No Sidebar ads