Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('display_errors', 1);
- ini_set('display_startup_errors', 1);
- error_reporting(E_ALL);
- // Insertion
- require_once('../include/db_config.php');
- //Variable d'erreurs
- $i = NULL;
- $email_error = NULL;
- $username_error = NULL;
- $password_error = NULL;
- $location_error = NULL;
- $sql_error = NULL;
- //Variable formulaire
- $username = $_POST['username'];
- $email = $_POST['email'];
- $password = $_POST['password'];
- $confirm = $_POST['confirm'];
- $created_at = time();
- $location = $_POST['location'];
- $last_ip = $_SERVER['REMOTE_ADDR'];
- if(empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $email_error = "Votre adresse E-Mail n'a pas un format valide";
- $i++;
- }
- else {
- $req = "SELECT * FROM user WHERE email = ?";
- $sql = $db->prepare($req);
- $sql->execute(array($email));
- $emailCount = $sql->rowCount();
- if($emailCount > 0) {
- $email_error = "Adress e-mail déja utilisée";
- $i++;
- }
- }
- if (strlen($username) < 3 || strlen($username) > 15)
- {
- $username_error = "Votre pseudo est soit trop grand, soit trop petit";
- $i++;
- }
- else {
- $req = "SELECT * FROM user WHERE username = ?";
- $sql = $db->prepare($req);
- $sql->execute(array($username));
- $usernameCount = $sql->rowCount();
- if($usernameCount > 0) {
- $username_error = "Nom d'utilisateur déja utilisé";
- $i++;
- }
- }
- //Vérification du mdp
- if (empty($password) || empty($confirm) || $password != $confirm)
- {
- $password_error = "Votre mot de passe et votre confirmation sont diffèrents, ou vides";
- $i++;
- }
- //Vérification du code postal
- if (strlen($location) != 5)
- {
- $location_error = "Votre code postal n'est pas correct";
- $i++;
- }
- if($i == 0)
- {
- $options = [
- 'cost' => 12,
- ];
- $pass_hash = password_hash($password, PASSWORD_BCRYPT, $options);
- if($pass_hash) {
- $lenghtKey = 12;
- $confirm_key = "";
- for($n=1; $n < $lenghtKey;$n++) {
- $confirm_key .= mt_rand(0, 9);
- }
- //fonction avec char
- //envoi token seulement et SELECT * FROM user WHERE confirm_key = token
- $req = "INSERT INTO user(username, password, email, created_at) VALUES (?, ?, ?, ?)";
- $sql = $db->prepare($req);
- $sql = $sql->execute(array($username, $pass_hash, $email, $created_at));
- $req = 'SELECT id FROM user WHERE username = ?';
- $sql = $db->prepare($req);
- $sql->execute(array($username));
- $user_data = $sql->fetch();
- $user_id = $user_data['id'];
- $req = "INSERT INTO confirm(user_id, confirm_key, created_at, last_ip) VALUES (?, ?, ?, ?)";
- $sql = $db->prepare($req);
- $successSql = $sql->execute(array($user_id, $confirm_key, $created_at, $last_ip));
- if($successSql) {
- echo 'Vous avez bien été inscrit ! Regardez vos mails pour confirmer votre inscription.';
- $header="MIME-Version: 1.0\r\n";
- $header.='From:"Dyguil.test"<guillaumeansseau@gmail.com'."\n";
- $header.='Content-Type:text/html; charset="utf-8"' . "\n";
- $header.='Content-Transfer-Encoding: 8bit';
- $text='
- <html>
- <body>
- <a href="http://dyguil.test/confirm.php?username='.urlencode($username).'&confirm_key='.$confirm_key.'">Confirmer votre compte</a>
- </body>
- </html>
- ';
- mail("$email", "Confirmation de compte !", $text);
- } else {
- $sql_error = "Une erreur est survenue veuillez réessayer. sql";
- $i++;
- }
- } else {
- $password_error = "Une erreur est survenue veuillez réessayer. password";
- $i++;
- }
- }
- if($i > 0)
- {
- echo 'Vous avez : ' .$i. ' erreur(s).';
- echo '<br>';
- echo $email_error;
- echo '<br>';
- echo $username_error;
- echo '<br>';
- echo $password_error;
- echo '<br>';
- echo $location_error;
- echo '<br>';
- echo $sql_error;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement