Methods for prohibiting illegal calls and the introduction of hard path files in PHP

Source: Internet
Author: User

Methods for prohibiting illegal calls and the introduction of hard path files in PHP 

There are some common files in PHP for convenience, we will make a common file, so that the unused files are called together. In order to prohibit public files from being called very separately,
You can make a constant on the file, prohibiting very calls:
Define a constant on the public file as follows

Define (' custom_string ', true);

In the file to be called, add a judgment to call the public file:

     if (!defined (' custom_string ')) {            exit (' illegal call ');        }

In general, to invoke the file can be used Include,include_once,require, require_once to call, but from a speed perspective, you can use the hard path to introduce the file.
In PHP with the magic constant __file__ can get the path of the file, with DirName (__file__), you can get the root directory address,
If you introduce a header.php file, you can introduce it in the following ways:

Require DirName (__file__). ' /filename.php ';

However, sometimes it may be inconvenient to call this Dirnam, which can be converted into a constant to make it easier to invoke.

The substr () function in PHP can intercept the root directory of a file. You can define this path as a constant:

Define (' Site_path ', substr (DirName (__file__), 0,-n));

A negative number allows the file to be intercepted from the back, and N is the amount of path characters stored in the public file. For example, if the public file store relative path is abc/includes/filename.php, then the number of characters of N abc/includes: 12. You can use constants to introduce a path directly after you define it as a constant:

Require Site_path. ' Filename.php ';

Methods for prohibiting illegal calls and the introduction of hard path files in PHP

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.