I am working on some forms and want to get advice on how to properly make a form secure against hackers, spam, etc. Also I want to know how to correctly send this form data in an e-mail (an attachment will also be sent so I am using enctype as multiform/form-data).
For simplicity, let's say I have only 1 field in the form.
<form method="post" enctype="multipart/form-data">
Address: <input type="text" name="user-address" value="<?php echo $_POST['user-address']; ?>" />
<input type="submit" value="Submit" />
</form>
Now, what are the steps I need to do in order to make sure the user input is safe? For example, I know I can do trim() and also check if the 'user-address' (in $_POST) is empty or not. But what else should I do? I've been seeing stuff about htmlentities() and htmlspecialchars() and that has me confused. Should I be using those? What about stripslashes()?
After making sure the input is safe, I want to send an e-mail (using PHPMailer) of what the user inputted in the body. What I have been noticing is that if a user enters a single or double quote, something like " ; shows up in the e-mail body instead of the actual character. Is this because the email is sent as PLAIN TEXT? I want it to be sent as plain text though because that's all it needs to be with the form fields I have. So I used html_entity_decode(..., ENT_QUOTES) on $_POST['user-address'] and that made it work. I am not sure if that is the correct approach or if that poses a security risk of some sort, however.
$msg = "Address: " . html_entity_decode($_POST['user-address'], ENT_QUOTES);
//send e-mail with attachment and $msg as body
MAIN POINTS: How can I make sure the simple form above is secure? and What do I do about single and double quote HTML entities being shown in the e-mail (assuming I want to send plain text e-mails)?
Thanks for the help!