Home Backend Development PHP Tutorial PHP Security - Forms and Data

PHP Security - Forms and Data

Feb 22, 2017 am 09:31 AM



##Form and data

In typical PHP application development, most logic involves data processing tasks, such as confirming whether the user has successfully logged in, adding items to the shopping cart, and processing credit card transactions.

Data may come from countless sources. As a security-conscious developer, you need to easily and reliably distinguish between two types of data:

l Filtered data

l Contaminated data

All trusted data that you set yourself can be considered as filtered data. A data that you set yourself is any hard-coded data, such as the following email address data:

  $email = 'chris@example.org';
Copy after login


The above email address chris@example.org does not come from any remote data source. It's obviously credible. Any data coming from a remote data source is input, and all input data is tainted and must be filtered before use.

Contaminated data refers to all data that cannot be guaranteed to be legal, such as forms submitted by users, emails received from mail servers, and xml documents sent from other web applications. In the previous example, $email is a variable containing filtered data. The data is the key, not the variables. Variables are just containers for data, which are often overwritten by contaminated data as the program executes:

  $email = $_POST['email'];
Copy after login


## Of course , this is why $email is called a variable. If you don’t want the data to change, you can use a constant instead:

CODE:

define('EMAIL', 'chris@example.org');
Copy after login

If defined with the above statement, EMAIL is an unchanging constant with the value chris@example.org throughout the script, and will not change even if you try to reassign it (usually accidentally). For example, the following code outputs chris@example.org (Attempting to redefine a constant will cause an error message with level Notice).


CODE:

Copy after login


Tips

For more information about constants, please visit http://www.php.cn /

As discussed in Chapter 1, register_globals can make it difficult to determine the origin of a variable such as $email. All data from external sources should be considered contaminated until proven legitimate.

Although a user can send data in a variety of ways, most applications perform the most important operations based on form submission results. Another attacker can compromise simply by manipulating the submitted data (which your app operates on), and the form conveniently opens up your app's design and the data you need to use to them. This is why form handling is the first concern among all web application security issues.

A user can transfer data to your application in three ways:

l Through the URL (such as GET data method)

l Through the content of a request (such as POST data method)

l Through HTTP header information (Such as Cookie)

Since HTTP header information is not directly related to form processing, it will not be discussed in this chapter. In general, suspicion of GET and POST data can be extended to all input, including HTTP header information.

表单通过GET或POST请求方式传送数据。当你建立了一个HTML表单,你需要在form标签的method属性中指定请求方式:

  <form action="http://example.org/register.php"
method="GET">
Copy after login


在前例中,请求方式被指定为GET,浏览器将通过URL的请求串部分传输数据,例如,考虑下面的表单:

CODE:

<form action="http://example.org/login.php"
method="GET">
  <p>Username: <input type="text"
name="username" /></p>
  <p>Password: <input type="password"
name="password" /></p>
  <p><input type="submit" /></p>
  </form>
Copy after login


如果我输入了用户名chris和密码mypass,在表单提交后,我会到达URL为http://www.php.cn/的页面。该URL最简单的合法HTTP/1.1请求信息如下:

CODE:

 GET /login.php?username=chris&password=mypass
HTTP/1.1
  Host: example.org
Copy after login


并不是必须要使用HTML表单来请求这个URL,实际上通过HTML表单的GET请求方式发送数据与用户直接点击链接并没有什么不同。

记住如果你在GET方式提交的表单中的action中试图使用请求串,它会被表单中的数据所取代。

而且,如果你指定了一个非法的请求方式,或者请求方式属性未写,浏览器则会默认以GET方式提交数据。

为说明POST请求方式,只对上例进行简单的更改,考虑把GET请求方式更改为POST的情况:

CODE:

<form action="http://example.org/login.php"
method="POST">
  <p>Username: <input type="text"
name="username" /></p>
  <p>Password: <input type="password"
name="password" /></p>
  <p><input type="submit" /></p>
  </form>
Copy after login


如果我再次指定用户名chris和密码mypass,在提交表单后,我会来到http://www.php.cn/页面。表单数据在请求的内部而不是一个URL的请求串。该方式最简单的合法HTTP/1.1请求信息如下

CODE:

POST /login.php HTTP/1.1
  Host: example.org
  Content-Type: application/x-www-form-urlencoded
  Content-Length: 30
 
  username=chris&password=mypass
Copy after login


 

      现在你已看到用户向你的应用提供数据的主要方式。在下面的小节中,我们将会讨论攻击者是如何利用你的表单和URL作为进入你的应用的缺口的。

以上就是PHP安全-表单与数据的内容,更多相关内容请关注PHP中文网(www.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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

See all articles