Guest User

Untitled

a guest
Jul 6th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.39 KB | None | 0 0
  1. <?php
  2. /**
  3. * Register form validation
  4. */
  5.    require_once('lib/startup.php');
  6.  
  7.    $vals = array('subd' => Local::$subd);
  8.    
  9.  
  10.  
  11. if (filter_has_var(INPUT_POST, 'name'))
  12. {
  13.     $errors = array();
  14.        
  15.     $fname = trim($_POST['fname']);
  16.     $surname = trim($_POST['surname']);
  17.     $username = trim($_POST['username']);
  18.     $email = trim($_POST['email']);
  19.     $pass1 = trim(md5($_POST['pass1']));
  20.     $pass2 = trim(md5($_POST['pass2']));
  21.    
  22.     //check if they're empty, if not, continue
  23.     if($fname == ''  || $surname =='' || $email == '' || $pass1 == '' || $pass2 == '' || $username == '')
  24.     {
  25.         $errors[] = 'All fields are required';
  26.     }
  27.     else
  28.     {    
  29.     //validating the data
  30.    
  31.     //check name length
  32.     if (strlen($fname) > 20)
  33.     {
  34.         $errors[] = 'First Name is too long';
  35.     }
  36.    
  37.     if (strlen($fname) <2)
  38.     {
  39.         $errors[] = 'First Name is too short';
  40.     }  
  41.    
  42.     if (strlen($surname) > 20)
  43.     {
  44.         $errors[] = 'Surname is too long';
  45.     }
  46.    
  47.     if (strlen($surname) <2)
  48.     {
  49.         $errors[] = 'Surname is too short';
  50.     }
  51.     //validate email address
  52.     if (filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
  53.     {
  54.         $errors[] = 'Please enter a valid email address';
  55.     }
  56.    
  57.     //validate password length
  58.     if (strlen($pass1) < 6)
  59.     {
  60.         $errors[] = 'Passwords must be 6 or more characters';
  61.     }
  62.    
  63.     //check if passwords match
  64.     if ($pass1 !== $pass2)
  65.     {
  66.         $errors[] = 'Your passwords do not match';
  67.     }
  68.    
  69.     //check if there's a user with this username already
  70.     $checkformembers = R::findOne('users', 'username=?', array($username));
  71.    
  72.     if (isset($checkformembers))
  73.     {
  74.         $errors[] = 'Username is already in use. Please try again.';
  75.     }
  76.    
  77.     }  
  78.     //if the errors array is not empty, display errors and return to register.twig
  79.     if (!empty($errors))
  80.     {
  81.         $vals['errors'] = $errors;
  82.         $tpl = 'register.twig';
  83.     }
  84.     else
  85.     {
  86.         //if it passes all the tests, dispense into the database
  87.         $prs = R::dispense('users');
  88.         $prs ->name = $_POST['fname'];
  89.         $prs ->surname = $_POST['surname'];
  90.         $prs ->username = $_POST['username'];
  91.         $prs ->email = $_POST['email'];
  92.         $prs ->password = md5($_POST['pass1']);
  93.        
  94.         $id = R::store($prs);
  95.         //go to register success page once dispensed.
  96.         $tpl = 'registersuccess.twig';
  97.     }
  98.        
  99.    
  100.        
  101. }
  102.  
  103. $template = $twig->loadTemplate($tpl);
  104.  
  105.     echo $template->render($vals);
  106.  
  107.  
  108.  
  109.  
  110.  
  111. ?>
Add Comment
Please, Sign In to add comment