You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

41 lines
1.2 KiB

<?php
// data from $_POST
$login = strtolower($_POST['login']);
$nickname = $_POST['nickname'];
$password = $_POST['password'];
if (empty($login) || empty($nickname) || empty($password)) {
// if any field is empty, redirect to index.php with error code 102
header('Location: /index.php?error=102');
exit;
}
// connect to database sqlite database using PDO
require '../include/db.php';
// check if user already exists (only login, don't care about nickname)
$q = $pdo->prepare('SELECT * FROM users WHERE login = :login');
$q->execute(['login' => $login]);
$users = $q->fetchAll();
if (count($users) > 0) {
// user already exists
header('Location: /index.php?error=101');
exit;
}
$password = hash("sha256", $password);
// insert user into database
$q = $pdo->prepare('INSERT INTO users (login, nickname, password, active, timestamp, ip_addr) VALUES (:login, :nickname, :password, :active, :timestamp, :ip_addr)');
$q->execute([
'login' => $login,
'nickname' => $nickname,
'password' => $password,
'active' => 1,
'timestamp' => time(),
'ip_addr' => $_SERVER['REMOTE_ADDR'],
]);
// redirect to index.php
header('Location: /index.php');