search
Homephp教程php手册php 一些基本安全处理技巧

php 一些基本安全处理技巧

Jun 13, 2016 am 10:11 AM
phpurlandBasicdeal withSafetyclientcommonSkillTutorialdatabaseofTargetescapeoutput

对于一些常见的输出目标(包括客户端、数据库教程和URL)的转义,PHP 中有内置函数可用。
如果你要写一个自己算法,做到万无一失很重要。需要找到在外系统中特殊字符的可靠和完整的列表,以及它们的表示方式,这样数据是被保留下来而不是转译了。
最常见的输出目标是客户机,使用htmlentities( )在数据发出前进行转义是最好的方法。与其它字符串函数一样,它输入是一个字符串,对其进行加工后进行输出。但是使用htmlentities( )函数的最佳方式是指定它的两个可选参数:引号的转义方式(第二参数)及字符集(第三参数)。引号的转义方式应该指定为ENT_QUOTES,它的目的是同时转义单引号和双引号,这样做是最彻底的,字符集参数必须与该页面所使用的字符集相必配。
为了区分数据是否已转义,我还是建议定义一个命名机制。对于输出到客户机的转义数据,我使用$html 数组进行存储,该数据首先初始化成一个空数组,对所有已过滤和已转义数据进行保存。

$html = array( );
$html['username'] = htmlentities($clean['username'], ENT_QUOTES, 'UTF-8');
echo "

Welcome back, {$html['username']}.

";
?>

小提示
htmlspecialchars( )函数与htmlentities( )函数基本相同,它们的参数定义完全相同,只不过是htmlentities( )的转义更为彻底。通过$html['username']把username 输出到客户端,你就可以确保其中的特殊字符不会被浏览
器所错误解释。如果username 只包含字母和数字的话,实际上转义是没有必要的,但是这体现了深度防范的原则。转义任何的输出是一个非常好的习惯,它可以戏剧性地提高你的软件的安全性。
另外一个常见的输出目标是数据库。如果可能的话,你需要对SQL 语句中的数据使用PHP内建函数进行转义。对于MySQL 用户,最好的转义函数是mysql教程_real_escape_string( )。如果你使用的数据库没有PHP 内建转义函数可用的话,addslashes( )是最后的选择。
下面的例子说明了对于MySQL 数据库的正确的转义技巧:

$mysql = array( );
$mysql['username'] = mysql_real_escape_string($clean['username']);
$sql = "SELECT *
FROM profile
WHERE username = '{$mysql['username']}'";
$result = mysql_query($sql);
?>
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

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 Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool