search
HomeBackend DevelopmentPHP Tutorial关于提交内容转义的有关问题

关于提交内容转义的问题
请教一下 一般写PHP程序的时候
当textarea的数据提交到数据库之前一般是否都需要执行转义处理
因为如果textarea提交内容中有php代码的时候 可能会执行里面的PHP代码
如果要转义 一般用什么函数

------解决方案--------------------
外部POST的数据只会被当作字符串处理,不会被当作代码执行。对用户输入过滤是个好习惯,它可以防止SQL注入,在这方面,这两函数都可以。
htmlspecialchars相比较addslashes而言,还可以帮助你阻止XSS攻击。因此推荐前者
------解决方案--------------------

探讨

执行PHP代码? 胡说八道。

入数据库addslashes就行了。

------解决方案--------------------
防SQL注入的核心是转义单双引号
htmlspecialchars 会将其转义为HTML 实体
addslashes 则会在引号前添加转义符 \ .这样Mysql就会认为这是一个普通的引号,而非字符串的边界标记。
------解决方案--------------------
htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体。
预定义的字符是:
& (和号) 成为 &
" (双引号) 成为 "
' (单引号) 成为 '

addslashes() 函数在指定的预定义字符前添加反斜杠。
这些预定义字符是:
单引号 (')
双引号 (")
反斜杠 (\)
NULL

比较一下你就知道自己应该选什么咯,textarea还用考虑嘛...


探讨

今天在群里问别人 别人这样说的
------------
htmlspecialchars这个只是处理HTML中的一些符号,使用addslashes或者mysql_escape_string转义了就不会有问题了
------------

有点晕- -

------解决方案--------------------
htmlspecialchars 将与、单双引号、大于和小于号化成HTML格式 
&转成& 
"转成"
' 转成'

htmlentities() 所有字符都转成HTML格式 除上面htmlspecialchars字符外,还包括双字节字符显示成编码等。 

addslashes 单双引号、反斜线及NULL加上反斜线转义 
被改的字符包括单引号 (')、双引号 (")、反斜线 backslash (\) 以及空字符NULL。 

stripslashes 去掉反斜线字符 去掉字符串中的反斜线字符。若是连续二个反斜线,则去掉一个,留下一个。若只有一个反斜线,就直接去掉。 

quotemeta 加入引用符号 将字符串中含有 . \\ + * ? [ ^ ] ( $ ) 等字符的前面加入反斜线 "\" 符号。 
nl2br() 将换行字符转成


strip_tags 去掉HTML及PHP标记 去掉字符串中任何 HTML标记和PHP标记,包括标记封堵之间的内容。注意如果字符串HTML及PHP标签存在错误,也会返回错误。 

mysql_real_escape_string 转义SQL字符串中的特殊字符 转义 \x00 \n \r 空格 \ ' " \x1a,针对多字节字符处理很有效。mysql_real_escape_string会判断字符集,mysql_escape_string则不用考虑。 

------解决方案--------------------
无论是传入的还是程序生成的数据,在入库时都有可能需要转义其中的特殊字符“'”
其理由很简单:
php 向数据库传递的是一个字符串,数据库解析这个字符串来完成相应的操作
数据库与使用者有一个约定:字符类型的值用单引号括起。
于是当数据中出现单引号时,数据库就不能准确的知道这个单引号是单引号这个字符还是字符串结束记号了
于是就有了转义一说

至于入库时是否还需要做其他处理,那就仁者见仁智者见智了。
我认为不需要,数据库只是仓库而已,放进去什么取出来还是什么?
取出来用的时候再做必要的处理,才能最大限度的保持数据的完整性

你当然对入库的数据做过滤处理,不过那天你想看看原始的数据时,那就绝无可能了
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

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

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software