Code has been added to clipboard!

User Validation in PHP

Example
<?php
  // Defining variables
  $nickname_error = $email_error = $website_error = "";
  $nickname = $email = $feedback = $website = "";
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
      $name_error = "Nickname missing.";
    }
  } else {
    $name = proc_input($_POST["name"]);
    // check if name doesn't contain unwanted characters
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $name_error = "Letters and spaces only.";
    }
  }
  if (empty($_POST["email"])) {
    $email_error = "Email missing.";
  } else {
    $email = proc_input($_POST["email"]);
    // check e-mail pattern
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $email_error = "Email invalid."; 
    } 
  }
  if (empty($_POST["website"])) {
    $website = "";
  } else {
    $website = proc_input($_POST["website"]);
    // check syntax of the URL (dashes allowed)
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
      $website_error = "URL Invalid.";
    }
  }
  if (empty($_POST["feedback"])) {
      $feedback = "";
  } else {
    $feedback = proc_input($_POST["feedback"]);
  }
?>