Home > Backend Development > PHP Tutorial > PHP method to get the current directory relative to the domain name, php to get the current domain name_PHP tutorial

PHP method to get the current directory relative to the domain name, php to get the current domain name_PHP tutorial

WBOY
Release: 2016-07-13 09:48:27
Original
829 people have browsed it

How PHP obtains the current directory relative to the domain name, PHP obtains the current domain name

The example in this article describes the method of PHP obtaining the current directory relative to the domain name. Share it with everyone for your reference. The details are as follows:

http://127.0.0.1/dev/classd/index.php/download
For example, with this address, I want to get the address in the red area to use to generate a link within the site, named baseurl.

Just use $_SERVER['SCRIPT_NAME']. $_SERVER['SCRIPT_NAME'] is the relative path of the PHP file currently being accessed. Just do the following:

Get the directory part from $_SERVER['SCRIPT_NAME'] and replace possible backslash issues in the dirname function
Copy code The code is as follows: $baseUrl = str_replace('\','/',dirname($_SERVER['SCRIPT_NAME']));

Guaranteed to return a normal value that can be used when empty

Copy code The code is as follows: $baseUrl = empty($baseUrl) ? '/' : '/'.trim($baseUrl,'/').'/' ;

In this way, the directory address of /dev/classd/ can be obtained normally.

By the way, I took a look at the baseUrl() function of Zend Framework. I saw such a lot of code that I stopped reading. I couldn’t figure out that it used a lot of code for the same function. A bunch of things are obtained in $_SERVER, and then after two or three classes, four or five methods are finally called directly by us. Except for the three lines in the middle that get the current PHP file, the rest are useless and meaningless processing, ZF It's already running slow enough. What does this mean? Interested friends can research it.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1022783.htmlTechArticleHow to get the current domain name directory with PHP, get the current domain name with php. This article describes the example of PHP getting the current domain name with respect to it. directory method. Share it with everyone for your reference. The details are as follows...
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