Browse Source

registracni formular

main
Ivan Pomykacz 1 month ago
parent
commit
552e9b7a49
  1. 2
      README.md
  2. 41
      actions/registration.php
  3. BIN
      data/db.sqlite
  4. 5
      include/db.php
  5. 3
      include/footer.php
  6. 8
      include/header.php
  7. 26
      include/registration.php
  8. 14
      index.php

2
README.md

@ -1,2 +1,2 @@
# chatari
# PHP project template

41
actions/registration.php

@ -0,0 +1,41 @@
<?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');

BIN
data/db.sqlite

Binary file not shown.

5
include/db.php

@ -0,0 +1,5 @@
<?php
// connect to database sqlite database using PDO
$pdo = new PDO('sqlite:../data/db.sqlite');

3
include/footer.php

@ -0,0 +1,3 @@
</body>
</html>

8
include/header.php

@ -0,0 +1,8 @@
<!DOCTYPE html>
<html lang="cs">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo "$title"; ?></title>
</head>
<body>

26
include/registration.php

@ -0,0 +1,26 @@
<form action="/actions/registration.php" method="post">
<label for="login">Login</label>
<input type="text" name="login" id="login" class="form-control" required>
<br>
<label for="nickname">Nickname</label>
<input type="nickname" name="nickname" id="nickname" class="form-control" required>
<br>
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" required>
<br>
<?php
// if error code is set, show error message
if (isset($_GET['error'])) {
$error = $_GET['error'];
echo "<div class=\"alert alert-danger\" role=\"alert\">";
if ($error == 101) {
echo 'User already exists';
}
echo "</div>";
}
?>
<button type="submit" class="btn btn-primary">Register</button>
</form>

14
index.php

@ -0,0 +1,14 @@
<?php
$title = "Šablona";
include "include/header.php";
// echo "php project template";
include "include/registration.php";
include "include/footer.php";
?>
Loading…
Cancel
Save