Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.55 KB | None | 0 0
  1. <?php
  2.  
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5. error_reporting(E_ALL);
  6.  
  7.     // Insertion
  8.     require_once('../include/db_config.php');
  9.  
  10.     //Variable d'erreurs
  11.     $i = NULL;
  12.     $email_error = NULL;
  13.     $username_error = NULL;
  14.     $password_error = NULL;
  15.     $location_error = NULL;
  16.     $sql_error = NULL;
  17.  
  18.     //Variable formulaire
  19.     $username = $_POST['username'];
  20.     $email = $_POST['email'];
  21.     $password = $_POST['password'];
  22.     $confirm = $_POST['confirm'];
  23.     $created_at = time();
  24.     $location = $_POST['location'];
  25.     $last_ip = $_SERVER['REMOTE_ADDR'];
  26.  
  27.     if(empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  28.         $email_error = "Votre adresse E-Mail n'a pas un format valide";
  29.         $i++;
  30.     }
  31.     else {
  32.  
  33.         $req = "SELECT * FROM user WHERE email = ?";
  34.         $sql = $db->prepare($req);
  35.         $sql->execute(array($email));
  36.  
  37.         $emailCount = $sql->rowCount();
  38.  
  39.         if($emailCount > 0) {
  40.             $email_error = "Adress e-mail déja utilisée";
  41.             $i++;
  42.         }
  43.  
  44.     }
  45.    
  46.     if (strlen($username) < 3 || strlen($username) > 15)
  47.     {
  48.         $username_error = "Votre pseudo est soit trop grand, soit trop petit";
  49.         $i++;
  50.     }
  51.     else {
  52.  
  53.         $req = "SELECT * FROM user WHERE username = ?";
  54.         $sql = $db->prepare($req);
  55.         $sql->execute(array($username));
  56.  
  57.         $usernameCount = $sql->rowCount();
  58.  
  59.         if($usernameCount > 0) {
  60.             $username_error = "Nom d'utilisateur déja utilisé";
  61.             $i++;
  62.         }
  63.  
  64.     }
  65.  
  66.     //Vérification du mdp
  67.     if (empty($password) || empty($confirm) || $password != $confirm)
  68.     {
  69.         $password_error = "Votre mot de passe et votre confirmation sont diffèrents, ou vides";
  70.         $i++;
  71.     }
  72.  
  73.     //Vérification du code postal
  74.     if (strlen($location) != 5)
  75.     {
  76.         $location_error = "Votre code postal n'est pas correct";
  77.         $i++;
  78.     }
  79.  
  80.     if($i == 0)
  81.     {
  82.  
  83.         $options = [
  84.             'cost' => 12,
  85.         ];
  86.         $pass_hash = password_hash($password, PASSWORD_BCRYPT, $options);
  87.  
  88.         if($pass_hash) {
  89.  
  90.             $lenghtKey = 12;
  91.             $confirm_key = "";
  92.             for($n=1; $n < $lenghtKey;$n++) {
  93.                 $confirm_key .= mt_rand(0, 9);
  94.             }
  95.  
  96.             //fonction avec char
  97.             //envoi token seulement et SELECT * FROM user WHERE confirm_key = token
  98.  
  99.             $req = "INSERT INTO user(username, password, email, created_at) VALUES (?, ?, ?, ?)";
  100.  
  101.             $sql = $db->prepare($req);
  102.  
  103.             $sql = $sql->execute(array($username, $pass_hash, $email, $created_at));
  104.  
  105.  
  106.             $req = 'SELECT id FROM user WHERE username = ?';
  107.  
  108.             $sql = $db->prepare($req);
  109.  
  110.             $sql->execute(array($username));
  111.  
  112.             $user_data = $sql->fetch();
  113.  
  114.             $user_id = $user_data['id'];
  115.  
  116.  
  117.             $req = "INSERT INTO confirm(user_id, confirm_key, created_at, last_ip) VALUES (?, ?, ?, ?)";
  118.            
  119.             $sql = $db->prepare($req);
  120.  
  121.             $successSql = $sql->execute(array($user_id, $confirm_key, $created_at, $last_ip));
  122.  
  123.  
  124.             if($successSql) {
  125.                 echo 'Vous avez bien été inscrit ! Regardez vos mails pour confirmer votre inscription.';
  126.  
  127.                 $header="MIME-Version: 1.0\r\n";
  128.                 $header.='From:"Dyguil.test"<guillaumeansseau@gmail.com'."\n";
  129.                 $header.='Content-Type:text/html; charset="utf-8"' . "\n";
  130.                 $header.='Content-Transfer-Encoding: 8bit';
  131.  
  132.                 $text='
  133.                <html>
  134.                    <body>
  135.                        <a href="http://dyguil.test/confirm.php?username='.urlencode($username).'&confirm_key='.$confirm_key.'">Confirmer votre compte</a>
  136.                    </body>
  137.                </html>
  138.                ';
  139.                 mail("$email", "Confirmation de compte !", $text);
  140.             } else {
  141.                 $sql_error = "Une erreur est survenue veuillez réessayer. sql";
  142.                 $i++;
  143.             }
  144.  
  145.         } else {
  146.             $password_error = "Une erreur est survenue veuillez réessayer. password";
  147.             $i++;
  148.         }
  149.     }
  150.    
  151.     if($i > 0)
  152.     {
  153.         echo 'Vous avez : ' .$i. ' erreur(s).';
  154.         echo '<br>';
  155.         echo $email_error;
  156.         echo '<br>';
  157.         echo $username_error;
  158.         echo '<br>';
  159.         echo $password_error;
  160.         echo '<br>';
  161.         echo $location_error;
  162.         echo '<br>';
  163.         echo $sql_error;
  164.     }
  165. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement