Recently, I encountered several PHP string processing functions in processing the input data of the front-end form. I really don't know what the function is. The API is too vague to understand, please help me to answer the difference and functions: htmlspecialcharsaddslashesstripslashesstrip_tags... recently, I encountered several PHP string processing functions in processing the input data of the front-end form. I really don't know what the function is. The API is too vague to understand, please help me to answer the differences and functions:
-
Htmlspecialchars
-
Addslashes
-
Stripslashes
-
Strip_tags
-
Nl2br
-
Mysql_real_escape_string
I just want to insert the data in textarea into the mysql database. Users may enter html and other messy strings and insert them into mysql. What should I do?
Reply content:
Recently, I encountered several PHP string processing functions in processing the input data of the front-end form. I really don't know what the function is. The API is too vague to understand, please help me to answer the differences and functions:
-
Htmlspecialchars
-
Addslashes
-
Stripslashes
-
Strip_tags
-
Nl2br
-
Mysql_real_escape_string
I just want to insert the data in textarea into the mysql database. Users may enter html and other messy strings and insert them into mysql. What should I do?
Standard writing (please forget the mess of Functions ):
// Check the function MySQLCheck (& $ value) that prevents SQL Injection {// remove the slash (the escape slash given by the server configuration) if (get_magic_quotes_gpc ()) {$ value = stripslashes ($ value);} // if it is not a number, enclose it with quotation marks (professional escape Function) if (! Is_numeric ($ value) {$ value = "'". mysql_real_escape_string ($ value ). "'";} return $ value; // example usage: // $ user = SQLCheck ($ _ POST ['user']); // $ pwd = SQLCheck ($ _ POST ['pwd']); // $ SQL = "SELECT * FROM users WHERE user = $ user AND password = $ pwd ";}
Function source (Open Source Library): https://github.com/MoonLord-LM/MyPHP
In addition, the new version of PHP has abandoned mysql extension, and mysqli extension is recommended. The Code is as follows:
// Check function MySQLCheck (& $ value) {global $ Connect; // remove the slash (Escape slash given by server configuration) if (get_magic_quotes_gpc ()) {$ value = stripslashes ($ value);} // if it is not a number, enclose it with quotation marks (professional escape Function) if (! Is_numeric ($ value) {$ value = "'". mysqli_real_escape_string ($ Connect, $ value ). "'";} return $ value; // example usage: // $ user = SQLCheck ($ _ POST ['user']); // $ pwd = SQLCheck ($ _ POST ['pwd']); // $ SQL = "SELECT * FROM users WHERE user = $ user AND password = $ pwd ";}
The above $ Connect is a database connection.
Note that both functions can be executed only after they are connected to the MySQL database. Otherwise, an error is reported.
In addition, if the front-end is to be displayed, the script and other labels should also be processed. You can use the str_replace function of PHP to replace the labels.
And so on.