Home > Backend Development > PHP Tutorial > PHP obtains the current host, domain name, path, port and other parameters code sharing

PHP obtains the current host, domain name, path, port and other parameters code sharing

小云云
Release: 2023-03-20 16:22:02
Original
1853 people have browsed it

This article mainly introduces how PHP obtains the current host, domain name, URL, path, port and other parameters. It has certain reference value. If you are interested, you can learn more. I hope it can help everyone.

Experimental environment:

Test domain name daxiangtravel.com, apache root directory/mnt/, test directory/mnt/qa/test, test file name is index.php.

Get the code:

Get the current directory:

  getcwd();
// /mnt/qa/test
dirname(__FILE__);
// /mnt/qa/test
Copy after login

Get the domain name or host address

$_SERVER['HTTP_HOST'];
 //daxiangtravel.com
Copy after login

Get the web page address

$_SERVER['PHP_SELF'];
// /qa/test/index.php
Copy after login

Get the URL Parameters

$_SERVER["QUERY_STRING"];
// v=433
获取用户代理
 
$_SERVER['HTTP_REFERER'];
// http://daxiangtravel.com/qa/test/index.php?v=433
Copy after login

Get the complete url

'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
// http://daxiangtravel.com/qa/test/index.php?v=433
 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
// http://daxiangtravel.com/qa/test/index.php?v=433
Copy after login

Get only the path

$url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
echo dirname($url);
// http://daxiangtravel.com/qa/test
Copy after login

The complete url including the port number

'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"]
.$_SERVER["REQUEST_URI"];
// http://daxiangtravel.com:80/qa/test/index.php?v=433
Copy after login

Related recommendations:

How to get domain name IP address code function in php

The above is the detailed content of PHP obtains the current host, domain name, path, port and other parameters code sharing. 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