
8 changed files with 98 additions and 1 deletions
@ -1,2 +1,2 @@ |
|||||
# chatari |
|
||||
|
# PHP project template |
||||
|
|
||||
|
@ -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'); |
Binary file not shown.
@ -0,0 +1,5 @@ |
|||||
|
<?php |
||||
|
|
||||
|
// connect to database sqlite database using PDO |
||||
|
$pdo = new PDO('sqlite:../data/db.sqlite'); |
||||
|
|
@ -0,0 +1,3 @@ |
|||||
|
|
||||
|
</body> |
||||
|
</html> |
@ -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> |
@ -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> |
@ -0,0 +1,14 @@ |
|||||
|
<?php |
||||
|
|
||||
|
$title = "Šablona"; |
||||
|
|
||||
|
include "include/header.php"; |
||||
|
|
||||
|
// echo "php project template"; |
||||
|
include "include/registration.php"; |
||||
|
|
||||
|
include "include/footer.php"; |
||||
|
|
||||
|
?> |
||||
|
|
||||
|
|
Loading…
Reference in new issue