This article is an example of how PHP obtains the current method relative to the domain name directory. Share to everyone for your reference. Specifically as follows:
Http://127.0.0.1/dev/classd/index.php/download
For example this address, I want to get the red area address for the link within the build station, called BaseURL.
With $_server[' Script_name '], $_server[' script_name ' is the relative path of the PHP file currently being accessed, as long as the following is done:
Gets the directory section from $_server[' Script_name ' and replaces the backslash problem that may occur with the DirName function
The code is as follows:
$BASEURL = Str_replace (', '/', dirname ($_server[' script_name '));
Guaranteed to return the normal value that can be used when empty
The code is as follows:
$baseUrl = Empty ($BASEURL)? '/': '/'. Trim ($BASEURL, '/'). ' /';
This allows you to get the directory address for this part of the/dev/classd/normally.
By the way, look at the BaseURL () function of the Zend Framework, it turns out that a whole bunch of code doesn't go on, and I can't figure out the same thing. He used a whole bunch of code, got a bunch from $_server, and then went through two or three classes in the middle, Four or five methods to finally be directly called by us, in addition to the middle of three lines is to get the current PHP file, the other is useless and unnecessary processing, ZF itself is running slow enough, this is what a meaning. Interested friends can study.
I hope this article will help you with your PHP program design.