PHP Gets the current method relative to the domain name directory

Source: Internet
Author: User
Tags php file relative zend framework

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.