English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article demonstrates the use of php input filtering operations htmlentities and htmlspecialchars. Shared for everyone's reference, as follows:
Filtering input (i.e., any data from the listed data sources) means escaping or deleting unsafe characters. Always filter input data before it reaches the application's storage layer. This is the first line of defense. If the website's comment form receives HTML, by default, visitors can add malicious <script> tags to comments without any hindrance, as shown below:
<p> My test </p> <script>alert(123)</script>
In the above example, if this comment is not filtered, malicious code will be stored in the database and then rendered in the website's tags.
HTML
We can use the htmlentities or htmlspecialchars function to filter HTML, converting special characters to their corresponding HTML entities.
The function htmlentities converts all special characters containing the corresponding 'html entity', such as currency symbols such as the euro and pound, copyright symbols, etc., while htmlspecialchars only escapes certain special characters, & " ' < >
These two functions are quite dumb, by default they do not escape single quotes
$str='<a href="test.html" rel="external nofollow" >\'test page\'</a><script>alert(213)</script>'; // No single quotes are escaped echo $str; echo "<hr/>".PHP_EOL; echo htmlentities($str); echo "<hr/>".PHP_EOL; echo htmlspecialchars($str);
You need to set the second parameter ENT_QUOTES, for details see the php manual
echo htmlentities($str,ENT_QUOTES,'UTF-8'); // single quotes are also escaped echo "<hr/>".PHP_EOL; echo htmlspecialchars($str,ENT_QUOTES,'UTF-8');//Escape anche gli apici singoli
Gli esempi sopra non possono distinguere between htmlentities e htmlspecialchars, quindi usiamo alcuni caratteri speciali, come l'euro, ecc. htmlentities li scapperà, mentre htmlspecialchars no.
echo htmlentities(' <>"').PHP_EOL; echo "<hr/>".PHP_EOL; echo htmlspecialchars(' <>"').PHP_EOL; // Senza escape
Conclusione:Quando si esegue un submit di modulo standard, si può semplicemente utilizzare strip_tags per rimuovere i tag HTML, se si utilizza un editor di testo arricchito è necessario conservare i tag HTML, si può utilizzare htmlspecialchars per filtrare i dati di submit.
Per chi è interessato a ulteriori contenuti su PHP, è possibile consultare le sezioni speciali di questo sito: 'Tutorial di sicurezza della progettazione dei programmi PHP', 'Riassunto delle tecniche di filtraggio di sicurezza PHP', 'Riassunto dell'uso dei operatori e delle espressioni in PHP', 'Riassunto delle tecniche di programmazione di rete PHP', 'Tutorial di base della sintassi PHP', 'Tutorial di progettazione orientata agli oggetti PHP', 'Riassunto dell'uso delle stringhe (string) in PHP', 'Tutorial di base dell'operazione del database MySQL in PHP' e 'Riassunto delle tecniche di operazione dei database comuni in PHP'.
Spero che il presente articolo possa essere utile per la progettazione di programmi PHP.
Dichiarazione: il contenuto di questo articolo è stato tratto da Internet, è di proprietà dei rispettivi autori, il contenuto è stato contribuito e caricato autonomamente dagli utenti di Internet, questo sito non detiene i diritti di proprietà, non è stato editato manualmente e non assume responsabilità legali correlate. Se trovi contenuti sospetti di violazione del copyright, ti preghiamo di inviare una e-mail a: notice#oldtoolbag.com (al momento dell'invio dell'e-mail, sostituisci # con @) per segnalare il problema e fornire prove pertinenti. Una volta verificata, questo sito eliminerà immediatamente il contenuto sospetto di violazione del copyright.