PHP Gets the URL function instance of the current page

  • 2021-07-22 09:18:47
  • OfStack

This article example tells the PHP obtains the current page URL function example, shares to everybody for everybody reference. The specific implementation method is as follows:

In PHP, there is no default Function to get the URL of the current page, so today we will introduce an PHP function to get the complete URL of the current page in PHP.

The function code is as follows, and you only need to use curPageURL () when calling:

/*  Get the current page URL Begin  */ 
function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {    // If it is SSL Encryption is added with " s "
        $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;
}
/* Get the current page URL End */

Supplement 1 server parameter description, the code is as follows:

// Get the domain name or host address   
echo $_SERVER['HTTP_HOST']."<br>"; #localhost
 
// Getting the Web Page Address  
echo $_SERVER['PHP_SELF']."<br>"; #/blog/testurl.php
 
// Get URL parameters  
echo $_SERVER["QUERY_STRING"]."<br>"; #id=5
 
// Get User Agent  
echo $_SERVER['HTTP_REFERER']."<br>";
 
// Gets the complete url
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
#http://localhost/blog/testurl.php?id=5
 
// Contains the complete port number url
echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 
#http://localhost:80/blog/testurl.php?id=5
// Path-only
$url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]; 
echo dirname($url);
#http://localhost/blog

I hope this article is helpful to everyone's PHP programming.


Related articles: