Home  >  Article  >  php教程  >  How to get the complete URL of the current page in PHP

How to get the complete URL of the current page in PHP

高洛峰
高洛峰Original
2017-01-03 17:41:511879browse

1. Use javascript to implement:

top.location.href The address of the top-level window

this.location.href The address of the current window

2. Use PHP to implement

http://localhost/PHP/XX.php?id=5

//Get the domain name or host address

echo $_SERVER['HTTP_HOST']; #localhost

//Get Web page address

echo $_SERVER['PHP_SELF']; #/PHP/XX.php

//Get URL parameters

echo $_SERVER["QUERY_STRING"]; #id=5

//Get user agent

echo $_SERVER['HTTP_REFERER'];

//Get user agent

echo $_SERVER['HTTP_REFERER'];

//Get the complete url

echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
#http://localhost/PHP/XX.php?id=5

//The complete url including the port number

echo 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
#http://localhost:80/PHP/XX.php?id=5

//Get the path only

$url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]; 
echo dirname($url);
#http://localhost/PHP

Summary

That’s it That’s the entire content of this article. I hope the content of this article can be of some help to everyone’s study or work. If you have any questions, you can leave a message to communicate.

For more articles related to PHP’s method of obtaining the complete URL of the current page, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn