Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Register form validation
- */
- require_once('lib/startup.php');
- $vals = array('subd' => Local::$subd);
- if (filter_has_var(INPUT_POST, 'name'))
- {
- $errors = array();
- $fname = trim($_POST['fname']);
- $surname = trim($_POST['surname']);
- $username = trim($_POST['username']);
- $email = trim($_POST['email']);
- $pass1 = trim(md5($_POST['pass1']));
- $pass2 = trim(md5($_POST['pass2']));
- //check if they're empty, if not, continue
- if($fname == '' || $surname =='' || $email == '' || $pass1 == '' || $pass2 == '' || $username == '')
- {
- $errors[] = 'All fields are required';
- }
- else
- {
- //validating the data
- //check name length
- if (strlen($fname) > 20)
- {
- $errors[] = 'First Name is too long';
- }
- if (strlen($fname) <2)
- {
- $errors[] = 'First Name is too short';
- }
- if (strlen($surname) > 20)
- {
- $errors[] = 'Surname is too long';
- }
- if (strlen($surname) <2)
- {
- $errors[] = 'Surname is too short';
- }
- //validate email address
- if (filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
- {
- $errors[] = 'Please enter a valid email address';
- }
- //validate password length
- if (strlen($pass1) < 6)
- {
- $errors[] = 'Passwords must be 6 or more characters';
- }
- //check if passwords match
- if ($pass1 !== $pass2)
- {
- $errors[] = 'Your passwords do not match';
- }
- //check if there's a user with this username already
- $checkformembers = R::findOne('users', 'username=?', array($username));
- if (isset($checkformembers))
- {
- $errors[] = 'Username is already in use. Please try again.';
- }
- }
- //if the errors array is not empty, display errors and return to register.twig
- if (!empty($errors))
- {
- $vals['errors'] = $errors;
- $tpl = 'register.twig';
- }
- else
- {
- //if it passes all the tests, dispense into the database
- $prs = R::dispense('users');
- $prs ->name = $_POST['fname'];
- $prs ->surname = $_POST['surname'];
- $prs ->username = $_POST['username'];
- $prs ->email = $_POST['email'];
- $prs ->password = md5($_POST['pass1']);
- $id = R::store($prs);
- //go to register success page once dispensed.
- $tpl = 'registersuccess.twig';
- }
- }
- $template = $twig->loadTemplate($tpl);
- echo $template->render($vals);
- ?>
Add Comment
Please, Sign In to add comment