Home  >  Article  >  Backend Development  >  How to use PHP to obtain the current host, domain name, URL, path, port and other parameters

How to use PHP to obtain the current host, domain name, URL, path, port and other parameters

巴扎黑
巴扎黑Original
2017-06-16 10:10:182214browse

This article mainly introduces how PHP obtains the current host, domain name, URL, path, port and other parameters. It has certain reference value. Those who are interested can learn about it

Experimental environment:

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

Get the code:

Get the current directory:


 getcwd();
// /mnt/qa/test
dirname(__FILE__);
// /mnt/qa/test

Get the domain name or host address


$_SERVER['HTTP_HOST']; 
 //daxiangtravel.com

Get the web address


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

Get the URL parameters


$_SERVER["QUERY_STRING"];
// v=433
获取用户代理

$_SERVER['HTTP_REFERER'];
// http://daxiangtravel.com/qa/test/index.php?v=433

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

Get only the path


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

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

The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope that everyone will support Script House.

The above is the detailed content of How to use PHP to obtain the current host, domain name, URL, path, port and other parameters. For more information, please follow other related articles on 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