Home Backend Development PHP Tutorial PHP data filtering method_PHP tutorial

PHP data filtering method_PHP tutorial

Jul 13, 2016 am 10:25 AM
php

At the beginning of the guide, we said that data filtering is the cornerstone of WEB application security in any language and on any platform. This includes verifying the data input to the application and the data output from the application, and a good software design can help developers:
Ensure that data filtering cannot be bypassed,
Ensure that illegal information does not affect Legal information, and
identifying the source of the data.
There are various views on how to ensure that data filtering cannot be bypassed, and two of them are more general than others and provide a higher level of assurance.
Scheduling Method
This method is scheduled with a single php script (via URL). Any other operations are included using include or require when necessary. This approach generally requires that each URL be passed a separate GET variable for dispatch. This GET variable can be thought of as a more simplified design that replaces the script name. For example:
http://a.org/dispatch.php?task=PRint_formdispatch.php is the only root file (Document root). It allows developers to do two very important things:
Implement some global security processing at the beginning of dispatch.php, and ensure that these processing cannot be bypassed.
Easy to determine where necessary to perform data filtering, especially in some special-purpose control flow operations.
Look at the following example for further discussion of the dispatch.php script:
If this is the only publicly accessible PHP script, One thing you can be sure of is that the design of this program ensures that the initial global security processing cannot be bypassed. It also makes it easy for developers to see the control flow of specific tasks. For example, it is easy to know without browsing the entire code: when $form_valid is true, end.inc is the only one displayed to the user; since it is before process.inc is included and has just been initialized to false, it can be determined that The internal logic of process.inc will set it to true; otherwise the form will be displayed again (possibly with an associated error message).
Note
If you use a directory-directed file such as index.php (instead of dispatch.php), you can use the URL address like this: http://a.org/?task=print_form.
You can also use ApacheForceType redirection or mod_rewrite to adjust the URL address: http://a.org/app/print-form.
Include methods
Another way is to use a single module, which is responsible for all security processing. This module is included at the front (or very front) of all public PHP scripts. Refer to the following script security.inc

Copy code The code is as follows:



In this example, each form submitted They all believe that the unique verification value of form should be included, and security.inc independently processes the data in the form that needs to be filtered. The HTML form that implements this requirement is as follows:
Copy code The code is as follows:

Username:

Password:


The array called $allowed is used to check which form variables are allowed. This list should be consistent before the form is processed. Process control decides what to execute, and process.inc is where the actual filtered data arrives.
Note
A better way to ensure that security.inc is always included at the beginning of every script is to use the auto_prepend_file setting.
Example of filtering
Establishing a whitelist is very important for data filtering. Since it's impossible to give examples for every type of form data you may encounter, some examples can help you get a general understanding.
The following code verifies the email address:
Copy the code The code is as follows:

]+@([-a-z0-9]+.)+[a-z]{2,}$/i';if (preg_match ($email_pattern, $_POST['email'])){$clean['email'] = $_POST['email'];}?>

The following code ensures $_POST The content of ['color'] is red, green, or blue:
Copy code The code is as follows:

[/code]
The following code ensures that $_POST['num'] is an integer:
[code]


The following code ensures that $_POST['num'] is a float:
Copy code The code is as follows:



Name conversion
Every example before used the array $clean. This is a good practice for developers to determine if their data is potentially compromised. Never save data in $_POST or $_GET after validating it. As a developer, you should always be fully suspicious of data saved in super global arrays.
It should be added that using $clean can help to think about what has not been filtered, which is more similar to the role of a whitelist. Can improve the level of security.
If you only save the validated data in $clean, the only risk in data validation is that the array element you reference does not exist, not unfiltered dangerous data.
Timing
Once the PHP script starts executing, it means that all HTTP requests have ended. At this point, the user has no chance to send data to the script. Therefore, no data can be entered into the script (even if register_globals is turned on). This is why initializing variables is a very good practice.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824954.htmlTechArticleAt the beginning of the guide, we said that data filtering is the cornerstone of WEB application security in any language and on any platform. . This includes verifying data input to and from the application,...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles