Hướng dẫn dùng escape html trong PHP
(PHP 4, PHP 5, PHP 7, PHP 8) addslashes — Quote string with slashes Descriptionaddslashes(string $string): string
A use case of addslashes() is escaping the aforementioned characters in a string that is to be evaluated by PHP: $str = "O'Reilly?"; The addslashes() is sometimes incorrectly used to try to prevent SQL Injection. Instead, database-specific escaping functions and/or prepared statements should be used. ParametersstringThe string to be escaped. Return ValuesReturns the escaped string. ExamplesExample #1 An addslashes() example $str = "Is your name O'Reilly?";// Outputs: Is your name O\'Reilly? See Also
roysimke at microsoftsfirstmailprovider dot com ¶ 12 years ago Never use addslashes function to escape values you are going to send to mysql. use mysql_real_escape_string or pg_escape at least if you are not using prepared queries yet. keep in mind that single quote is not the only special character that can break your sql query. and quotes are the only thing which addslashes care. hoskerr at nukote dot com ¶ 19 years ago Beware of using addslashes() on input to the serialize() function. serialize() stores strings with their length; the length must match the stored string or unserialize() will fail. Such a mismatch can occur if you serialize the result of addslashes() and store it in a database; some databases (definitely including PostgreSQL) automagically strip backslashes from "special" chars in SELECT results, causing the returned string to be shorter than it was when it was serialized. In other words, do this... $string="O'Reilly"; $string="O'Reilly"; [Note to the maintainers: You may, at your option, want to link this note to serialize() as well as to addslashes(). I'll refrain from doing such cross-posting myself...] svenr at selfhtml dot org ¶ 11 years ago To output a PHP variable to Javascript, use json_encode().
$var = "He said \"Hello O'Reilly\" & disappeared.\nNext line...";echo "alert(".json_encode($var).");\n";?> Output: alert("He said \"Hello O'Reilly\" & disappeared.\nNext line...") ; geekcom ¶ 3 years ago For PHP 7.3.* use FILTER_SANITIZE_ADD_SLASHES. $str = "Is your name O'Reilly?"; hybrid at n0spam dot pearlmagik dot com ¶ 21 years ago Remember to slash underscores (_) and percent signs (%), too, if you're going use the LIKE operator on the variable or you'll get some unexpected results. unsafed ¶ 17 years ago addslashes does NOT make your input safe for use in a database query! It only escapes according to what PHP defines, not what your database driver defines. Any use of this function to escape strings for use in a database is likely an error - mysql_real_escape_string, pg_escape_string, etc, should be used depending on your underlying database as each database has different escaping requirements. In particular, MySQL wants \n, \r and \x1a escaped which addslashes does NOT do. Therefore relying on addslashes is not a good idea at all and may make your code vulnerable to security risks. I really don't see what this function is supposed to do. divinity76 at gmail dot com ¶ 5 months ago Addslashes is *never* the right answer, it's (ab)use can lead to security exploits! if you need to escape HTML, it's (unfortunately) if you need to quote a string in xpath it's David Spector ¶ 8 years ago If all you want to do is quote a string as you would normally do in PHP (for example, when returning an Ajax result, inside a json string value, or when building a URL with args), don't use addslashes (you don't want both " and ' escaped at the same time). Instead, just use this function: function
Quote($Str) // Double-quoting only stuart at horuskol dot co dot uk ¶ 13 years ago Be careful on whether you use double or single quotes when creating the string to be escaped: $test = 'This is one line\r\nand this is another\r\nand this line has\ta tab'; echo $test; $test = "This is one line\r\nand this is another\r\nand this line has\ta tab"; echo
$test; Nate from RuggFamily.com ¶ 15 years ago If you want to add slashes to special symbols that would interfere with a regular expression (i.e., . \ + * ? [ ^ ] $ ( ) { } = ! < > | :), you should use the preg_quote() function. Adrian C ¶ 15 years ago What happends when you add addslashes(addslashes($str))? This is not a good thing and it may be fixed: function checkaddslashes($str){ checkaddslashes("aa'bb"); => aa\'bb Hope this will help you Picky ¶ 16 years ago This function is deprecated in PHP 4.0, according to this article: http://www.newsforge.com/article.pl?sid=06/05/23/2141246 Also, it is worth mentioning that PostgreSQL will soon start to block queries involving escaped single quotes using \ as the escape character, for some cases, which depends on the string's encoding. The standard way to escape quotes in SQL (not all SQL databases, mind you) is by changing single quotes into two single quotes (e.g, ' ' ' becomes ' '' ' for queries). You should look into other ways for escaping strings, such as "mysql_real_escape_string" (see the comment below), and other such database specific escape functions. php at slamb dot org ¶ 19 years ago spamdunk at home dot com, your way is dangerous on PostgreSQL (and presumably MySQL). You're quite correct that ANSI SQL specifies using ' to escape, but those databases also support \ for escaping (in violation of the standard, I think). Which means that if they pass in a string that includes a "\'", you expand it to "\'''" (an escaped quote followed by a non-escaped quote. WRONG! Attackers can execute arbitrary SQL to drop your tables, make themselves administrators, whatever they want.) The best way to be safe and correct is to: - don't use magic quotes; this approach is bad. For starters, that's making the assumption that you will be using your input in a database query, which is arbitrary. (Why not escape all "<"s with "<"s instead? Cross-site scripting attacks are quite common as well.) It's better to set up a way that does whatever escaping is correct for you when you use it, as below: - when inserting into the database, use prepared statements with placeholders. For example, when using PEAR DB: $stmt
= $dbh->prepare('update mb_users set password = ? where username = ?'); Plus, if the database supports prepared statements (the soon-to-be-released PostgreSQL 7.3, Oracle, etc), several executes on the same prepare can be faster, since it can reuse the same query plan. If it doesn't (MySQL, etc), this way falls back to quoting code that's specifically written for your database, avoiding the problem I mentioned above. (Pardon my syntax if it's off. I'm not really a PHP programmer; this is something I know from similar things in Java, Perl, PL/SQL, Python, Visual Basic, etc.) luciano at vittoretti dot com dot br ¶ 16 years ago Note, this function wont work with mssql or access queries. function addslashes_mssql($str){ return $str; function stripslashes_mssql($str){ return $str; Lars ¶ 10 years ago Even for simple json string backslash encodings, do not use this function. Some tests may work fine, but in json the single quote (') must not be escaped. joechrz at gmail dot com ¶ 16 years ago Here's an example of a function that prevents double-quoting, I'm surprised noone has put something like this up yet... (also works on arrays) function escape_quotes($receive) { foreach ( array_keys($thearray) as $string) {$thearray[$string] = addslashes($thearray[$string]); $thearray[$string] = preg_replace("/[\\/]+/","/",$thearray[$string]); } if (! is_array($receive))return $thearray[0]; else return $thearray; } ?> DarkHunterj ¶ 13 years ago Based on: baburaj dot ambalam at gmail dot com ¶ 2 years ago escape '$' using backslash '\$' $evalStr = "5 + 3";$sum = 0; $evalStr = " \$sum = ". $evalStr.";"; eval( $evalStr); print ("sum ".$sum);?> |