Advertisement
failfail

sadsadsad

Apr 29th, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.19 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,  &$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.  
  84.  
  85. }
  86.  
  87.  
  88. function writeErrors(array $errors)
  89. {
  90.     echo implode("<br>", $errors);
  91. }
  92.  
  93. //Input
  94. $email = isset($_POST['email']) ? $_POST['email'] : "";
  95. $login = isset($_POST['log']) ? $_POST['log'] : "";
  96. $password = isset($_POST['pass']) ? $_POST['pass'] : "";
  97.  
  98.  
  99. function isValidInput($email, $login, $password, array &$errors, array &$fields)
  100. {
  101.  
  102.  
  103.     validateEmail($email, $errors,$fields);
  104.     validateUsername($login, $errors,$fields);
  105.     validatePassword($password, $errors,$fields);
  106.  
  107.     $isValid = count($errors) === 0;
  108.  
  109.  
  110.     return $isValid;
  111.  
  112.  
  113. }
  114.  
  115.    if($password !== $_POST['pass']){
  116.         $errors[] = "Пароли не совпадают";
  117.         $fields['pass'] !=='pass';
  118. }
  119.  
  120. $errors = [];
  121. $response = [];
  122. $fields = [];
  123.  
  124. if (isset($_POST['register']) && isValidInput($email, $login, $password, $errors,$fields)) {    
  125.     // include_once 'connection.php';
  126.     $password = password_hash($password, PASSWORD_DEFAULT);
  127.     $mysqli = new mysqli("localhost", "root", "", "userlistdb");
  128.  
  129.     $sql = "INSERT INTO usertbl(
  130.                    username,
  131.                    password,
  132.                    email
  133.                ) VALUES(
  134.                    '$login',
  135.                    '$password',
  136.                    '$email'              
  137.                )";
  138.     $result = $mysqli->query($sql) or die(mysql_error());
  139.  
  140.     $response = [
  141.         'status' => 'OK',
  142.         'messages' => ['Registered successful']
  143.     ];
  144.    
  145.  
  146. } else {
  147.     $response = [
  148.         'status' => 'FAILED',
  149.         'messages' => $errors,
  150.         'fields' => $fields
  151.     ];
  152. }
  153.  
  154.  
  155. header('Content-Type:application/json;charset=utf-8');
  156. echo json_encode($response);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement