PHP代码安全和XSS,SQL注入等对于各类网站的安全非常中用,尤其是UGC(User Generated Content)网站,论坛和电子商务网站,常常是XSS和SQL注入的重灾区。这里简单介绍一些基本编程要点, 相对系统安全来说,php安全防范更多要求编程人员对用户输入的各种参数能更细心.
php编译过程中的安全
建议安装Suhosin补丁,必装安全补丁
php.ini安全设置
register_global = off
magic_quotes_gpc = off
display_error = off
log_error = on
# allow_url_fopen = off
expose_php = off
open_basedir =
safe_mode = on
disable_function = exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,dl,popen,show_source,get_cfg_var
safe_mode_include_dir =
DB SQL预处理
mysql_real_escape_string (很多PHPer仍在依靠addslashes防止SQL注入,但是这种方式对中文编码仍然是有问题的。addslashes的问题在于黑客可以用0xbf27来代替单引号,GBK编码中0xbf27不是一个合法字符,因此addslashes只是将0xbf5c27,成为一个有效的多字节字符,其中的0xbf5c仍会被看作是单引号,具体见这篇文章)。用mysql_real_escape_string函数也需要指定正确的字符集,否则依然可能有问题。
prepare + execute(PDO)
ZendFramework可以用DB类的quote或者quoteInto, 这两个方法是根据各种数据库实施不用方法的,不会像mysql_real_escape_string只能用于mysql
用户输入的处理
无需保留HTML标签的可以用以下方法
strip_tags, 删除string中所有html标签
htmlspecialchars,只对””,”;”,”'”字符进行转义
htmlentities,对所有html进行转义
必须保留HTML标签情况下可以考虑以下工具:
HTML Purifier: HTML Purifier is a standards-compliant HTML filter library written in PHP.
PHP HTML Sanitizer: Remove unsafe tags and attributes from HTML code
htmLawed: PHP code to purify & filter HTML
上传文件
用is_uploaded_file和move_uploaded_file函数,使用HTTP_POST_FILES[]数组。并通过去掉上传目录的PHP解释功能来防止用户上传php脚本。
ZF框架下可以考虑使用File_upload模块
Session,Cookie和Form的安全处理
不要依赖Cookie进行核心验证,重要信息需要加密, Form Post之前对传输数据进行哈希, 例如你发出去的form元素如下:
程序代码
<input type="hidden" name="H[name]" value="<?php echo $Oname?>"> <br><input type="hidden" name="H[age]" value="<?php echo $Oage?>"> <br><?php $sign = md5('name'.$Oname.'age'.$Oage.$secret); ?> <br><input type="hidden" name="hash" value="<?php echo $sign?>"> <br><br><br>POST回来之后对参数进行验证 <br><br>程序代码 <br><div class="codetitle">
<span><a style="CURSOR: pointer" data="18636" class="copybut" id="copybut18636" onclick="doCopy('code18636')"><u>复制代码</u></a></span> 代码如下:</div><div class="codebody" id="code18636"> <br>$str = ""; <br>foreach($_POST['H'] as $key=>$value) { <br>$str .= $key.$value; <br>} <br>if($_POST['hash'] != md5($str.$secret)) { <br>echo "Hidden form data modified"; exit; <br>} <br>
</div> <br><br>PHP安全检测工具(XSS和SQL Insertion) <br>Wapiti - Web application security auditor(Wapiti - 小巧的站点漏洞检测工具) (SQL injection/XSS攻击检查工具) <br>安裝/使用方法: <br>apt-get install libtidy-0.99-0 python-ctypes python-utidylib <br>python wapiti.py http://Your Website URL/ -m GET_XSS <br>Pixy: XSS and SQLI Scanner for PHP( Pixy - PHP 源码缺陷分析工具) <br>安裝: apt-get install default-jdk <br>Remote PHP Vulnerability Scanner(自动化 PHP页面缺陷分析, XSS检测功能较强) <br>PHPIDS - PHP 入侵检测系統

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
