View sourcecode

The following files exists in this folder. Click to view.

register.php

60 lines ASCII Windows (CRLF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
require "database.php";
session_start();

$error "";

if (
$_SERVER["REQUEST_METHOD"] === "POST") {
    if (!
$_POST["username"] || !$_POST["password"]) {
        
$error "All fields required";
    } else {
        
$hash password_hash($_POST["password"], PASSWORD_DEFAULT);

        try {
            
$stmt $conn->prepare(
                
"INSERT INTO Players (Username, PasswordHash, Email)
                 VALUES (?, ?, ?)"
            
);
            
$stmt->execute([
                
$_POST["username"],
                
$hash,
                
$_POST["email"]
            ]);

            
header("Location: login.php");
            exit;
        } catch (
PDOException $e) {
            
$error "Username already exists";
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Register</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container mt-5">
    <h2>Register</h2>

    <?php if ($error): ?>
        <div class="alert alert-danger"><?= $error ?></div>
    <?php endif; ?>

    <form method="POST">
        <input class="form-control mb-2" name="username" placeholder="Username" required>
        <input class="form-control mb-2" name="email" placeholder="Email" required>
        <input class="form-control mb-2" type="password" name="password" placeholder="Password" required>

        <button class="btn btn-success">Register</button>
        <a href="index.php" class="btn btn-secondary">Back</a>
    </form>
</div>

</body>
</html>