Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Enhancing a Simple PHP Application Integrating Validation Errors Escaping Output

Carsten Dollerup
Carsten Dollerup
9,278 Points

Why not just do htmlspecialchars($user_input) in the SERVER_METHOD brackets?

Why not just do htmlspecialchars($user_input) in the SERVER_METHOD brackets?

As in: if ($_SERVER["REQUEST_METHOD"] == "POST") { $message = htmlspecialchars(trim($_POST["message"])); }

I looked this up because I am deathly afraid of malicious code, and the thing is, that it'll seriously mess up a message with html signs. Most of you can probably figure this out by yourselves, whereas I had to look it up :-)

Example: A typed message with html characters like this in the message field: "I "really" don't like how your so called <green> shirt looks when I opened the box it came in! I am returning the order #1234232 immediately!"

Would "return" with the error message like this: "I "really" don't like how your so called <green> shirt looks when I opened the box it came in! I am returning the order #1234232 immediately!" And it would look like this in the e-mail generated as well unless you "roll it back" with another function before sending the mail.

I hope this is of use for someone.