PHP Security

I build a php for a registration form for my website that i developed. Somehow security is one of my top priorities in protecting people information
I read an article about filtering but i’m so confused because i’m so new at php. HEre is the original php code fragment.


<?php
$to = "whoever email address";
$subject = "Join CCF";
 
$userName = $_POST['visitor_name'];
$userEmail =  $_POST['visitor_email'];
$userPhone = $_POST['visitor_phone'];
 
$headers = "From: ". $userName . "<" . $userEmail. ">
";
$headers .= "Reply-To: " . $userEmail . "
";
$headers .= "Return-path: ". $userEmail;
 
$message = "Name: " . $userName;
$message .= "
Email: " . $userEmail;
$message .= "

Phone: " . $userPhone; 
 
if (mail($to,$subject,$message,$headers)) {
  echo "output=Thanks for joining. We look forward of having you as a member";
} else {
  echo "output=error";
}

 

?>

Here is the code from the article



Before submit:

$emailPattern = '/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i';
    if (!preg_match($emailPattern, $emailFieldToTest)){
        print 'Please review the email address you entered. There seems to be a problem';
    }

After Submit:

function safe( $name ) {
   return( preg_replace(array( "\r", "
", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:" ), "", $name ) );
}

any ideas?