This code should be placed at the very top of the page where the action of your form points to! Let say you started your form like:
<form name="myform" method="post" action="processform.php">
Then the code would be in processform.php. I personally like the action of the form be the same file, that is:
<form name="myform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
This way, you don't have to joggle between multiple files!