Advertisement
failfail

asdadsadassad

Apr 29th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.27 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. function validateEmail($email, &$errors = [],&$fields = [])
  5. {
  6.  
  7.     if (empty($email)) {
  8.         $errors[] = "Введите эмайл";
  9.         $fields['email']='email';
  10.         return;
  11.     }
  12.  
  13.     if (!preg_match("/^([a-z,0-9])+@([a-z,0-9])+(.([a-z,0-9])+)+$/", $email)) {
  14.         $errors[] = "Эмайл введен не правильно";
  15.         $fields['email']='email';
  16.     }
  17.     if (strlen($email) < "7") {
  18.         $errors[] = "Эмайл не может быть меньше 7 символов";
  19.         $fields['email']='email';
  20.     }
  21.     if (strlen($email) > "100") {
  22.         $errors[] = "Эмайл не может быть больше 100 символов";
  23.         $fields['email']='email';
  24.     }
  25.  
  26.  
  27. }
  28.  
  29. function validateUsername($username, &$errors = [],&$fields = [])
  30. {
  31.     if (empty($username)) {
  32.         $errors[] = "Введите логин";
  33.         $fields['log']='log';
  34.         return;
  35.     }
  36.     if (!stripslashes($username) && htmlspecialchars($username) && trim($username)) {
  37.         $errors[] = "Логин введён неверно!";
  38.         $fields['log']='log';
  39.     }
  40.     if (strlen($username) < "7") {
  41.         $errors[] = "Логин не может быть короче 7-ми символов";
  42.         $fields['log']='log';
  43.     }
  44.     if (strlen($username) > "100") {
  45.         $errors[] = "Логин длинее 100 символов";
  46.         $fields['log']='log';
  47.     }
  48.    
  49.        $mysqli = new mysqli("localhost", "root", "", "userlistdb");
  50.  
  51.     $q = "SELECT * FROM usertbl WHERE username='".$username."'";
  52.     $result = $mysqli->query($q);
  53.     $myrow = $result->fetch_array();
  54.     if($myrow['username'] == $username ){
  55.         $errors[] = 'Юзер с таким именем уже есть';
  56.         $fields['log']='log';
  57.  
  58. }
  59.  
  60.  
  61. }
  62.  
  63. function validatePassword($pass, $pass_r,  &$errors = [],&$fields = [])
  64. {
  65.     if (empty($pass)) {
  66.         $errors[] = "Введите пароль";
  67.         $fields['pass']='pass';
  68.         return;
  69.     }
  70.     if (!stripslashes($pass) && htmlspecialchars($pass) && trim($pass)) {
  71.         $errors[] = "Пароль введён неверно!";
  72.         $fields['pass']='pass';
  73.     }
  74.     if (strlen($pass) < "7") {
  75.         $errors[] = "Пароль не может быть короче 7-ми символов";
  76.         $fields['pass']='pass';
  77.     }
  78.     if (strlen($pass) > "100") {
  79.         $errors[] = "Пароль длинее 100 символов";
  80.         $fields['pass']='pass';
  81.     }
  82.  
  83.        if($pass !== $pass_r){
  84.         $errors[] = "Пароли не совпадают";
  85.         $fields['pass'] !=='pass';
  86. }
  87.  
  88. }
  89.  
  90.  
  91. function writeErrors(array $errors)
  92. {
  93.     echo implode("<br>", $errors);
  94. }
  95.  
  96. //Input
  97. $email = isset($_POST['email']) ? $_POST['email'] : "";
  98. $login = isset($_POST['log']) ? $_POST['log'] : "";
  99. $password = isset($_POST['pass']) ? $_POST['pass'] : "";
  100.  $pass_r = isset($_POST['passs']) ? $_POST['passs'] : "";
  101.  
  102. function isValidInput($email, $login, $password, $pass_r,  array &$errors, array &$fields)
  103. {
  104.  
  105.  
  106.     validateEmail($email, $errors,$fields);
  107.     validateUsername($login, $errors,$fields);
  108.     validatePassword($password, $pass_r,  $errors,$fields);
  109.  
  110.     $isValid = count($errors) === 0;
  111.  
  112.  
  113.     return $isValid;
  114.  
  115.  
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122. $errors = [];
  123. $response = [];
  124. $fields = [];
  125.  
  126. if (isset($_POST['register']) && isValidInput($email, $login, $password, $errors,$fields)) {    
  127.     // include_once 'connection.php';
  128.     $password = password_hash($password, PASSWORD_DEFAULT);
  129.     $mysqli = new mysqli("localhost", "root", "", "userlistdb");
  130.  
  131.     $sql = "INSERT INTO usertbl(
  132.                    username,
  133.                    password,
  134.                    email
  135.                ) VALUES(
  136.                    '$login',
  137.                    '$password',
  138.                    '$email'              
  139.                )";
  140.     $result = $mysqli->query($sql) or die(mysql_error());
  141.  
  142.     $response = [
  143.         'status' => 'OK',
  144.         'messages' => ['Registered successful']
  145.     ];
  146.    
  147.  
  148. } else {
  149.     $response = [
  150.         'status' => 'FAILED',
  151.         'messages' => $errors,
  152.         'fields' => $fields
  153.     ];
  154. }
  155.  
  156.  
  157. header('Content-Type:application/json;charset=utf-8');
  158. echo json_encode($response);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement