Home  >  Article  >  Backend Development  >  How to prohibit access to .php files in php

How to prohibit access to .php files in php

藏色散人
藏色散人Original
2020-05-19 11:22:383266browse

How to prohibit access to .php files in php

#How to prohibit access to .php files in php?

The example in this article describes how PHP prohibits accessing .php files by directly entering the address from the browser.

The specific implementation method is as follows:

Generally speaking, for some important files, we do not want users to directly enter the address for access, so we need to make some settings for this.

The following summarizes some PHP methods to prohibit accessing .PHP files by directly entering the address from the browser, which is very practical.

For example, I don’t want others to access the file //www.xxx.net/xx.php by entering the address directly from the browser.

But if you cannot access //www.xxx.net/xx.php from any website, you will not be able to access another address even if you establish a connection locally.

1. Just write the following code in the header of the xx.php file.

The code is as follows:

$fromurl="//www.xxx.net/"; //跳转往这个地址。
if( $_SERVER['HTTP_REFERER'] == "" )
{
header("Location:".$fromurl); exit;
}

This way you only need to simply forge the source. For this We can also perform the following operations:

2. Define an identification variable in the program

The code is as follows:

define('IN_SYS', TRUE);

3. Get this variable in config.php

The code is as follows:

if(!defined('IN_SYS')) {
exit('禁止访问');
}

The following two methods are what we have encountered in many cms.

Recommended: "PHP Tutorial"

The above is the detailed content of How to prohibit access to .php files in php. 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