


8 very useful PHP security functions, how many do you know? , very useful php_PHP tutorial
8个很有用的PHP安全函数,你知道几个?,很有用php
原文:Useful functions to provide secure PHP application
译文:有用的PHP安全函数
译者:dwqs
安 全是编程非常重要的一个方面。在任何一种编程语言中,都提供了许多的函数或者模块来确保程序的安全性。在现代网站应用中,经常要获取来自世界各地用户的输 入,但是,我们都知道“永远不能相信那些用户输入的数据”。所以在各种的Web开发语言中,都会提供保证用户输入数据安全的函数。今天,我们就来看看,在 著名的开源语言PHP中有哪些有用的安全函数。
在PHP中,有些很有用的函数开源非常方便的 防止你的网站遭受各种攻击,例如SQL注入攻击,XSS(Cross Site Scripting:跨站脚本)攻击等。一起看看PHP中常用的、可以确保项目安全的函数。注意,这并不是完整的列表,是我觉得对于你的i项目很有的一些 函数。
1. mysql_real_escape_string()
这个函数在PHP中防止SQL注入攻击时非常有用。这个函数会对一些例如单引号、双引号、反斜杠等特殊字符添加一个反斜杠以确保在查询这些数据之前,用户提供的输入是干净的。但要注意,你是在连接数据库的前提下使用这个函数。
但是现在已经不推荐使用mysql_real_escape_string()了,所有新的应用应该使用像PDO一样的函数库执行数据库操作,也就是说,我们可以使用现成的语句防止SQL注入攻击。
2. addslashes()
这 个函数的原理跟mysql_real_escape_string()相似。但是当在php.ini文件中,“magic_quotes_gpc“的值是 “on”的时候,就不要使用这个函数。magic_quotes_gpc 的默认值是on,对所有的 GET、POST 和 COOKIE 数据自动运行 addslashes()。不要对已经 被 magic_quotes_gpc 转义过的字符串使用 addslashes(),因为这样会导致双层转义。你可以使用 get_magic_quotes_gpc()函数来确定它是否开启。
3. htmlentities()
这个函数对于过滤用户输入的数据非常有用。它会将一些特殊字符转换为HTML实体。例如,用户输入<时,就会被该函数转化为HTML实体<(<),输入>就被转为实体>.(HTML实体对照表:http://www.w3school.com.cn/html/html_entities.asp),可以防止XSS和SQL注入攻击。
4. htmlspecialchars()
在HTML中,一些特定字符有特殊的含义,如果要保持字符原来的含义,就应该转换为HTML实体。这个函数会返回转换后的字符串,例如‘&’ (ampersand) 转为’&‘(ps:请参照第三点中的实体对照表链接)
ps:此处原文有误(见评论),在此非常感谢瑾瑜 提出。现已更正,另外附上此函数常见的转换字符:
The translations performed are:
- ‘&’ (ampersand) becomes ‘&’
- ‘”‘ (double quote) becomes ‘"’ when
ENT_NOQUOTES
is not set. - “‘” (single quote) becomes ‘'’ (or ') only when
ENT_QUOTES
is set. - ‘<’ (less than) becomes ‘<’
- ‘>’ (greater than) becomes ‘>’
5. strip_tags()
这个函数可以去除字符串中所有的HTML,JavaScript和PHP标签,当然你也可以通过设置该函数的第二个参数,让一些特定的标签出现。
6. md5()
From a security perspective, the behavior of some developers storing simple passwords in the database is not recommended. The md5() function can generate a 32-character md5 hash of a given string, and this process is irreversible, that is, you cannot get the original string from the result of md5().
Currently this function is not considered secure because open source databases can reverse-check the plaintext of a hash value. You can find a list of MD5 hash databases here
7. sha1()
This function is similar to md5(), but it uses a different algorithm to produce a 40-character SHA-1 hash (md5 produces a 32-character hash). Don't rely on this function for absolute safety, otherwise there will be unexpected results.
8. intval()
Don’t laugh, I know this function has nothing to do with security. The intval() function converts variables into integer types. You can use this function to make your PHP code safer, especially when you are parsing data such as ID and age.
Next article: Making a bubble prompt box with pure CSS

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

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

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

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

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

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

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
