Home > Article > Backend Development > PHP gets the current page URL address and parameters_PHP tutorial
php gets the current page url address and parameters To get the complete address of the current page, we have to go through a lot of operations, such as HTTP or HTTPS php files and paths, host domain names, and query parameters. Get the protocol - HTTP
php tutorial to get the current page url address and parameters
To get the complete address of the current page, we have to go through a lot of operations, such as http or https tutorials, php files and paths, host domain names, and query parameters. Finally, it is done.
Get protocol - http
The protocol of the url can be read out in the $_server['server_protocol'] variable.
*/
echo $_server['server_protocol'];
/*
If you check the values, you can find that, not just http or https, but a string like this: http/1.1
*/
$protocol = strpos(strtolower($_server['server_protocol']),'https') === false ? 'http' : 'https';
/*
Get host domain name*/
$host = $_server['http_host'];
/*
Use $_server['script_name'] to obtain the php file and path except the domain name
*/
$script = $_server['script_name'];
//Get the query parameters behind?
$params = $_server['query_string'];
//Method 2
$uri = $_server['request_uri'];
//Let’s look at a complete example of getting the current url
$protocol = strpos(strtolower($_server['server_protocol']),'https') === false ? 'http' : 'https';
$host = $_server['http_host'];
$script = $_server['script_name'];
$params = $_server['query_string'];
$currenturl = $protocol . '://' . $host . $script . '?' . $params;
echo $currenturl;