suchen
Heimphp教程php手册php stripslashes和addslashes的区别介绍

php stripslashes和addslashes的区别介绍

Jun 13, 2016 am 09:43 AM
phpstriphauptsächlich介绍区别Artikelvon

 这篇文章主要介绍了php stripslashes和addslashes的区别,特分享下方便需要的朋友。

我们在向mysql写入数据时,比如: 代码如下: mysql_query("update table set `title`='kuhanzhu's blog'");     那就会出错。同asp时一样,数据库都会对单引号过敏。而addslashes在这个时候就最长面子了,跟asp的replace(”‘”,”””,”kuhanzhu's blog”)功能一样。    PHP为了安全性,所以引入了个magic_quotes_gpc = On的功能,可以不需要做任何处理就能直接把单引号插入数据库中,那么对于Off时,则需要考虑单引号的问题了,而不是一味地信任运行环境。   当magic_quotes_gpc = On时,使用了addslashes()处理后的数据在数据库中将以\'形式保存,如果此时直接输出的话,就会发现比自己期待的内容多了个\,因此stripslashes()出场了,它能把\去掉(区别于str_replace(”\”, “”,$Str))。   当magic_quotes_gpc = Off时,使用了addslashes()处理后的数据在数据库中将以'形式保存,没有上面说的有\的问题,addslashes()起到插入数据不出错的作用,如果此时直接输出的话,数据正常。不需要再用stripslashes()。   addslashes()和stripslashes()正好是相反的,直接记忆:addslashes()加个\,stripslashes()去个\   那么什么时候用呢?   简单说:   当magic_quotes_gpc = On时,系统会自动处理单引号等问题,用不用addslashes()和stripslashes()都没关系,但是如果添加数据时用了addslashes(),那么显示数据时必须要stripslashes()   当magic_quotes_gpc = Off时,系统不会处理单引号等问题,所以插入数据时必须要使用addslashes(),显示数据时则不需要使用stripslashes()。   既然有了分析,做程序时要怎么办呢?根据以上两种情况,可得:   不管magic_quotes_gpc是On还是Off,咱添加数据时都用addslashes(),当On时,必须使用stripslashes(),Off时则不能用stripslashes()。   如何判断On还是Off呢?用get_magic_quotes_gpc()。   最后举例:   代码如下: 代码  //提交数据,或者变量准备: $Content=addslashes(”这里面是数据,不管有没单引号或者还是变量”); //插入数据到数据库,代码省略 //开始显示数据 $Content=”从数据库读取的数据”; if(get_magic_quotes_gpc()){   $Content=stripslashes($Content);  } echo $Content;  
Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heißer Artikel

Nordhold: Fusionssystem, erklärt
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌
Mandragora: Flüstern des Hexenbaum
3 Wochen vorBy尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

SAP NetWeaver Server-Adapter für Eclipse

SAP NetWeaver Server-Adapter für Eclipse

Integrieren Sie Eclipse mit dem SAP NetWeaver-Anwendungsserver.

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

EditPlus chinesische Crack-Version

EditPlus chinesische Crack-Version

Geringe Größe, Syntaxhervorhebung, unterstützt keine Code-Eingabeaufforderungsfunktion

MinGW – Minimalistisches GNU für Windows

MinGW – Minimalistisches GNU für Windows

Dieses Projekt wird derzeit auf osdn.net/projects/mingw migriert. Sie können uns dort weiterhin folgen. MinGW: Eine native Windows-Portierung der GNU Compiler Collection (GCC), frei verteilbare Importbibliotheken und Header-Dateien zum Erstellen nativer Windows-Anwendungen, einschließlich Erweiterungen der MSVC-Laufzeit zur Unterstützung der C99-Funktionalität. Die gesamte MinGW-Software kann auf 64-Bit-Windows-Plattformen ausgeführt werden.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Leistungsstarke integrierte PHP-Entwicklungsumgebung