How to use PHP to get all the url parameter information of the current page?

藏色散人
Release: 2023-04-03 18:08:01
Original
4876 people have browsed it

This article mainly introduces how to use php to obtain the complete URL. First, let’s give a brief introduction to the novices about what a URL is. As explained on Baidu Encyclopedia, the Uniform Resource Locator is a concise representation of the location and access method of resources that can be obtained from the Internet. It is the address of a standard resource on the Internet. Every file on the Internet has a unique URL, which contains information indicating the location of the file and what the browser should do with it. In fact, it is simply a URL. That is, this article teaches you how to use php to obtain the complete URL and various parameters of the current page.

php The specific code examples for obtaining the url and other parameters are as follows:

1. Get the domain name or host address

echo $_SERVER[&#39;HTTP_HOST&#39;]."<br>"; #localhost
Copy after login

2. Get Web page address

echo $_SERVER[&#39;PHP_SELF&#39;]."<br>"; #/blog/testurl.php
Copy after login

3. Get the URL parameters

echo $_SERVER["QUERY_STRING"]."<br>"; #id=5
Copy after login

4. Get the complete url

echo &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;REQUEST_URI&#39;];
echo &#39;http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].$_SERVER[&#39;PHP_SELF&#39;].&#39;?&#39;.$_SERVER[&#39;QUERY_STRING&#39;];
#http://localhost/blog/testurl.php?id=5
Copy after login

How to use PHP to get all the url parameter information of the current page?

5 , Get the complete url including the port number

echo &#39;http://&#39;.$_SERVER[&#39;SERVER_NAME&#39;].&#39;:&#39;.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 
#http://localhost:80/blog/testurl.php?id=5
Copy after login

6. Get only the path

$url=&#39;http://&#39;.$_SERVER[&#39;SERVER_NAME&#39;].$_SERVER["REQUEST_URI"]; 
echo dirname($url);
#http://localhost/blog
Copy after login

Note: $_SERVER is a file containing header information (header), path (path), and script location. (script locations) and other information array. The items in this array are represented by Web server creation. There is no guarantee that every server will offer all items; servers may ignore some, or serve items not listed here.

【Recommended related articles】

What are the ways to request url in php

Detailed explanation of the definition of php parse_url() function and Usage

#Details introduction to one of the methods of obtaining URL parameters in JavaScript

The above is the detailed content of How to use PHP to get all the url parameter information of the current page?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!