When getting a url address in php, I will use the super global variable $ _ SERVER, which includes various parameters, such as HTTP_HOST, PHP_SELF, and QUERY_STRING.
Introduction to several functions used by PHP to obtain URLs
| The Code is as follows: |
Copy code |
|
<? Php
// Obtain the domain name or host address
Echo $ _ SERVER ['HTTP _ host']. "<br> ";
// Obtain the webpage address
Echo $ _ SERVER ['php _ SELF ']. "<br> ";
// Obtain URL parameters
Echo $ _ SERVER ["QUERY_STRING"]. "<br> ";
// Detailed address of the source webpage
Echo $ _ SERVER ['HTTP _ referer']. "<br> ";
?> |
Combine the above functions to obtain the complete URL address.
| The Code is as follows: |
Copy code |
|
<? Php
// Description: Obtain the complete URL
Function curPageURL ()
{
$ 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;
}
?> |
After defining this function, you can directly call it:
| The Code is as follows: |
Copy code |
|
<? Php
Echo curPageURL ();
?> |