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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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>).