Home > Backend Development > PHP Problem > How to prohibit access method in php

How to prohibit access method in php

藏色散人
Release: 2023-03-13 16:22:01
Original
3821 people have browsed it

php access prohibition method: 1. Write the "if( $_SERVER['HTTP_REFERER'] == "" ){...}" code in the header of the php file; 2. Define a Identify the variables; 3. Get the variables in config.php.

How to prohibit access method in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How to prohibit access to php?

php method to prohibit directly entering the address from the browser to access the .php file:

The specific implementation method is as follows:

Generally speaking For some important files, we do not want users to directly enter the address to access them, so we need to make some settings for this. The following summarizes some PHP methods to prohibit accessing .PHP files directly from entering the address in the browser, which are very practical.

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

But if you cannot access https://www.jb51.net/xx.php from any website, you will not be able to access another address after establishing a connection locally.

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

The code is as follows:

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

This way you only need to simply forge the source. , we can also do the following:
2. Define an identification variable in the program

The code is as follows:

define('IN_SYS', TRUE);
Copy after login

3. Get this variable in config.php

The code is as follows:

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

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

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to prohibit access method in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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