What is the safest way to accept user inputted programming code in PHP, store it in database and display it back with the HTML pre tag?
I currently convert the input to HTML entities, but I somehow think it wouldn't be that easy...
Any suggestions?
What is the safest way to accept user inputted programming code in PHP, store it in database and display it back with the HTML pre tag?
I currently convert the input to HTML entities, but I somehow think it wouldn't be that easy...
Any suggestions?
Programming code is just text; if it's not executed there can't be any harm done.
This means you need to be concerned about:
Protecting your database from SQL injection. This can be done by escaping the input string (mysql_real_escape_string()) or using prepared statements.
Protecting your users from XSS. This can be done by converting your code to html entities (ie: using htmlspecialchars()), so potentially malicious tags (ie: <script>) get converted to text (eg: <script>).