/** * Functions for checking if user exists */ function checkingIfUserExists() { include_once 'validate.php'; include_once 'functions.php'; if (isset($_POST['email']) && isset($_POST['password'])) { $email = cleanInput($_POST['email']); $password = cleanInput($_POST['password']); } if (empty($email) || empty($password)) { echo "All fields are required. Please fill in all the fields."; exit; } else { /*checking correctness of form input*/ $email = filter_var($email, FILTER_SANITIZE_EMAIL); if (validateEmail($email) == false) { echo "E-mail should be in the format of name@example.com"; exit; } if (validateLength($password, 6) == false) { echo "Password should contain not less than 6 symbols"; exit; } //$password_hash=password_hash($password, PASSWORD_DEFAULT); //PHP 5 >= 5.5.0 $password_hash = md5($password); /*checking if user already exists*/ if (checkLoggedUserInFile($email, $password_hash) == true) { //echo "Hello! You have logged in as ".$_SESSION['user_name']."."; header('Location:products.php'); exit; } else { echo "No such user, or wrong password.<br>"; //exit; } } }
function validateForm(&$errors) { global $firstname, $surname, $birthdate; ## firstname ## if (!validateRequired($firstname)) { $errors['firstname'][] = 'First name is required.'; } else { if (!validateLength($firstname, 2)) { $errors['firstname'][] = 'Minimum length is 2.'; } } ## surname ## if (!validateRequired($surname)) { $errors['surname'][] = 'Surname is required.'; } else { if (!validateLength($surname, 2)) { $errors['surname'][] = 'Minimum length is 2.'; } } ## date ## if (!validateRequired($birthdate)) { $errors['birthdate'][] = 'Date of Birth is required.'; } else { if (!validateDate($birthdate)) { $errors['birthdate'][] = 'Wrong date.'; } } ## return ## return empty($errors) ? true : false; }
function validateForm(&$errors) { global $username, $password, $rep_password; if (!validateRequiredField($username)) { $errors['username'][] = 'Username required.'; } else { if (!validateLength($username, 4)) { $errors['username'][] = 'Username must be 4 characters long.'; } } if (!validateRequiredField($password)) { $errors['password'][] = 'Password required.'; } else { if (!validateLength($password, 6)) { $errors['password'][] = 'Password must be 6 characters long.'; } } if (!validateRequiredField($rep_password)) { $errors['rep_password'][] = 'Repeated password required.'; } else { if (!validateLength($rep_password, 6)) { $errors['rep_password'][] = 'Repeated password must be 6 characters long.'; } } return !empty($errors); }
function validateNumInput($input, $maxLength) { $input = validateLength($input, $maxLength); if (!is_numeric($input)) { return 0; } return $input; }
function validateString($name, $str, $min_length, $max_length) { $str = trim($str); if (get_magic_quotes_gpc()) { $str = stripslashes($str); } $result = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); //$result = filter_var($result, FILTER_SANITIZE_FULL_SPECIAL_CHARS); if (validateLength($result, $min_length, $max_length)) { return $result; } else { throw new Exception('Pole ' . $name . ' jest za krótkie/długie.', 400); } }
function validateForm(&$errors) { global $username, $password1, $password2; ## username ## if (!validateRequired($username)) { $errors['username'][] = 'Username is required.'; } else { if (!validateLength($username, 4)) { $errors['username'][] = 'Minimum username length is 4.'; } } ## password1 ## if (!validateRequired($password1)) { $errors['password1'][] = 'Password is required.'; } else { if (!validateLength($password1, 6)) { $errors['password1'][] = 'Minimum password length is 6.'; } else { if (!validateNonAlphaNum($password1)) { $errors['password1'][] = 'Password must contain at least 1 non-alphanumeric character.'; } else { if (!validateIdentical($password1, $password2)) { $errors['password1'][] = 'Passwords don\'t match.'; } } } } ## password2 ## if (!validateRequired($password2)) { $errors['password2'][] = 'Password is required.'; } else { if (!validateLength($password2, 6)) { $errors['password2'][] = 'Minimum password length is 6.'; } else { if (!validateNonAlphaNum($password2)) { $errors['password2'][] = 'Password must contain at least 1 non-alphanumeric character.'; } else { if (!validateIdentical($password1, $password2)) { $errors['password2'][] = 'Passwords don\'t match.'; } } } } ## return ## return empty($errors) ? true : false; }
/** * Functions for checking & validating form */ function checkingFormAndSaveNewUser() { include_once 'validate.php'; if (isset($_POST['username']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['confirm_password']) && isset($_POST['agree'])) { $username = cleanInput($_POST['username']); $email = cleanInput($_POST['email']); $password = cleanInput($_POST['password']); $confirm_password = cleanInput($_POST['confirm_password']); $agree = $_POST['agree']; if (validateUsername($username) == false) { echo "Name should contain capitals and lower case, not less than 2 symbols"; exit; } $email = filter_var($email, FILTER_SANITIZE_EMAIL); if (validateEmail($email) == false) { echo "E-mail should be in the format of name@example.com"; exit; } if (validateLength($password, 6) == false) { echo "Password should contain not less than 6 symbols"; exit; } if (validateConfirm($password, $confirm_password) == false) { echo "Passwords do not match"; exit; } //$password_hash=password_hash($password, PASSWORD_DEFAULT); //PHP 5 >= 5.5.0 $password_hash = md5($password); $dir_for_saved_users = "./user/"; if (!is_dir($dir_for_saved_users)) { mkdir($dir_for_saved_users, 0777, true); } chmod('./user/', 0777); $filename = $dir_for_saved_users . "user_info"; $new_user_info = $username . ":" . $email . ":" . $password_hash . "\n"; file_put_contents($filename, $new_user_info, FILE_APPEND); //$_SESSION['name'] = $username; echo "You have signed up successfully! <a href='index.php'>Log in</a>"; } else { echo "All fields are required. Please fill in all the fields."; exit; } }
//| Check the field length //+----------------------------------------------------------------+ function validateLength($fieldValue, $minLength) { return strlen(trim($fieldValue)) > $minLength; } //+----------------------------------------------------------------+ //| Get the posted field values & validate them //+----------------------------------------------------------------+ $name = $_POST["name"]; $email = $_POST["email"]; $phone = $_POST["tel"]; $message = $_POST["message"]; $errorExists = false; // Name if (!validateLength($name, 2)) { $errorExists = true; } if (preg_match($expLettersOnly, $name) !== 1) { $errorExists = true; } // Email if (preg_match($expEmail, $email) !== 1) { $errorExists = true; } //+----------------------------------------------------------------+ //| Submit the form (if there is no errors) //+----------------------------------------------------------------+ if ($errorExists == true) { //ture echo '<div class="alert alert-danger alert-dismissible wow fadeInUp" role="alert">
function checkVar($target, $untrusted_value, $awaited_type, $min, $max, $default_value, $label, $array_return, $die_on_fail) { $value_accepted = true; $error = ""; // 1. filter value according to target (web page or database) // converts to correct charset, removes unwanted values, encodes special chars // does nothing if not $target = "" $untrusted_value = filterValue($target, $untrusted_value); // 2. checks var content against awaited type if ($awaited_type != "") { $value_accepted = validateType($target, $untrusted_value, $awaited_type); if ($value_accepted == 0) { $error .= "bad type, " . $awaited_type . " awaited."; } } else { // sets var type if not specified, for next check against bounds if (is_numeric($untrusted_value)) { $awaited_type = "float"; } else { $awaited_type = "string"; } } // 3. checks var content against bounds if ($value_accepted) { // numeric : checks var content against values bounds if ($awaited_type == "int" || $awaited_type == "float" || $awaited_type == "hex") { echo $awaited_type . "<br>"; $value_accepted = validateValue($untrusted_value, $min, $max); if (!$value_accepted) { $error .= "bad value, " . $min . " to " . $max . " expected."; } } // string : checks var content against length bounds if ($awaited_type == "string" || $awaited_type == "date" || $awaited_type == "url" || $awaited_type == "email") { $value_accepted = validateLength($untrusted_value, $min, $max); if (!$value_accepted) { $error .= "bad length, " . $min . " to " . $max . " chars expected."; } } } if ($value_accepted) { switch ($array_return) { case 0: // returns a single value without feedback return $untrusted_value; break; case 1: // returns an array with filtered value or default value with error feedback if validation fails (useful for form validation) return array("ok" => true, "value" => $untrusted_value, "error" => ""); } } else { if ($die_on_fail) { exit("Fatal error :: bad var value detected"); if ($debug_mode == "on") { echo "<br>'" . $label . "' " . $error; } } switch ($array_return) { case 0: // returns a single value without feedback return $default_value; break; case 1: // returns an array with filtered value or default value with error feedback if validation fails (useful for form validation) return array("ok" => false, "value" => $default_value, "error" => "'" . $label . "' " . $error); } } }
$gender = "Male"; } else { if ($gender === "2") { $gender = "Female"; } else { $errorExists = true; $errors .= "<li>Please select a gender!</li>"; } } // Email if (preg_match($expEmail, $email) !== 1) { $errorExists = true; $errors .= "<li>The email address format is invalid!</li>"; } // Password if (!validateLength($password, 5)) { $errorExists = true; $errors .= "<li>The password is too short!</li>"; } if (preg_match($expLettersNumbers, $password) !== 1) { $errorExists = true; $errors .= "<li>The password can only contain alphanumeric characters!</li>"; } // Confirm Password if ($confirmPassword !== $password) { $errorExists = true; $errors .= "<li>The passwords don't match!</li>"; } // If no errors, echo the results if (!$errorExists) { echo "<h3>Success! The form has been submitted!</h3>" . "<p>Details:</p>" . "<ul>" . "<li>Name: {$name}</li>" . "<li>Usernname: {$username}</li>" . "<li>Gender: {$gender}</li>" . "<li>Email: {$email}</li>" . "<li>Subscribe to newsletter: {$subscribe}</li>" . "</ul>";
function validateForm($username, $password1, $password2) { global $errors; global $password; if (validateRequired($username)) { if (!validateString($username)) { $errors['username'][] = 'Username needs to be String!'; } if (!validateAlphabethUsed($username)) { $errors['username'][] = 'Username needs to be in latin characters!'; } if (!validateLength($username, 5)) { $errors['username'][] = 'Username needs to be at least 5 characters long!'; } } else { $errors['username'][] = 'Username needs to be filled!'; } if (validateRequired($password1)) { if (!validateLength($password1, 5)) { $errors['password1'][] = 'Password needs to be at least 5 characters long!'; } if (!validatePassword($password1)) { $errors['password1'][] = "Password need to consist of one of the three of Capital, \nSmall letters, Digits and Special characters"; } } else { $errors['password1'][] = 'Password needs to be filled!'; } if (validateRequired($password2)) { if (!validateLength($password2, 5)) { $errors['password2'][] = 'Password needs to be at least 5 characters long!'; } if (!validatePassword($password2)) { $errors['password2'][] = "Password need to consist of one of the three of Capital, \nSmall letters, Digits and Special characters"; } if (validateRequired($password1)) { if (!validateMatchingPasswords($password1, $password2)) { $errors['password2'][] = "Password do not match!"; } else { $password = md5($password1); } } } else { $errors['password2'][] = 'Password needs to be filled!'; } }