<?php
    include_once('bdd.php');

    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
        header('Location: signup.php');
        exit();
    }
    try {
        $pdo = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD);

    }
    catch(PDOException $e) {
        exit();
    }
    
    if (!isset($_POST['login'], $_POST['passwd'], $_POST['passwdconf'])) {
        header('Location: signup.php');
        exit();
    }
    if (empty($_POST['login']) || empty($_POST['passwd']) || empty($_POST['passwdconf'])) {
        header('Location: signup.php');
        exit();
    }
    
    $login = htmlspecialchars($_POST['login']);
    $passwd = htmlspecialchars($_POST['passwd']);
    $passwdconf = htmlspecialchars($_POST['passwdconf']);
    
    if ($passwd != $passwdconf) {
        header('Location: signup.php');
        exit();
    }
    
    $req = $pdo->prepare('INSERT INTO Users (login, passwd) VALUES (:login, :passwd)');
    $req->bindValue(':login', $login, PDO::PARAM_STR);
    $req->bindValue(':passwd', password_hash($passwd, PASSWORD_DEFAULT), PDO::PARAM_STR);
    if ($req->execute()) {
        header('Location: signin.php');
    } else {
        header('Location: signup.php');
    }
    
    
        
