(PHP 4, PHP 5, PHP 7, PHP 8)
addslashes — Esegue il quoting di una stringa con gli slash
La funzione restituisce una stringa con il carattere backslash anteposto ai caratteri che richiedono l'escape. Questi caratteri sono:
'
)"
)\
)Un caso d'uso di addslashes() è fare l'escape dei suddetti caratteri in una stringa che deve essere eseguita come script da PHP:
<?php
$str = "O'Reilly?";
eval("echo '" . addslashes($str) . "';");
?>
Prima di PHP 5.4.0, la direttiva PHP magic_quotes_gpc
era on
per impostazione predefinita ed essenzialmente eseguiva addslashes()
su tutti i dati GET, POST e COOKIE.
addslashes() non deve essere usato su stringhe su cui è già stato
effettuato l'escape con magic_quotes_gpc,
poiché sulle stringhe verrebbe fatto un doppio escape. get_magic_quotes_gpc() può essere usata per controllare
se magic_quotes_gpc è on
.
A volte addslashes() è erroneamente utilizzato per provare a prevenire SQL Injection. Invece, dovrebbero essere usate funzioni di escape specifiche per il database e/o prepared statement.
str
La stringa su cui fare l'escape.
Restituisce la stringa trasformata dopo l'escape.
Example #1 Un esempio di addslashes()
<?php
$str = "Is your name O'Reilly?";
// Outputs: Is your name O\'Reilly?
echo addslashes($str);
?>