This may get marked as duplicate, but in my defense, I've been searching around for a while, and a lot of the information I find is in relation to mysql
or mysqli
at best, or is incomplete. I want a thorough, up-to-date answer that factors in using PDO
and prepared statements.
What is the proper way to handle data as it moves through an application.
Is the following theoretical flow of data adequate, and if not, what improvements would you recommend?
- proper form validation client side.
- Use of
$_POST
rather than$_GET
- In PHP, using
$variable = htmlentities($_POST['variable']);
just before database insertion. - using
PDO
and prepared statements like:bindValue(':variable', $variable)
; - On output, using
echo htmlspecialchars($variable);
to prevent XSS attacks.
Two related questions:
- Lets say you're using
htmlentities()
on data before database insertion. How can you also remove all the garbage that is inserted if a user entered say<p>my input value</p>
. This writes:<tr><p>my input value</p>&l
to the database. - If your php is returning a JSON array handled by AJAX, how do you handle output in that scenario? This doesn't work in PHP:
htmlspecialchars($JSON_Array)
Thanks in advance for your help on this.