-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignup.php
59 lines (55 loc) · 1.6 KB
/
signup.php
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
<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" href="style.css">
<?php include "./header.shtml" ?>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
Sign Up
<form action="/signup.php" method="post" id="signup" class="forms">
<input type="text" name="username" minlength="3" placeholder="Username">
<input type="password" minlength="5" name="password">
<input type="submit" value="Submit">
<div id="signupres"></div>
</form>
</body>
</html>
<?php
/**
* @var mysqli $conn
*/
include 'db.php';
function validateParams($username, $password) {
if(strlen($_POST["username"]) < 3) {
echo "Username too short";
exit();
}
if(strlen($_POST["username"]) > 24) {
echo "Username too long";
exit();
}
if(strlen($_POST["password"]) < 5) {
echo "Password too short";
exit();
}
}
if(isset($_POST["username"]) && isset($_POST["password"])) {
validateParams($_POST["username"], $_POST["password"]);
$username = $_POST["username"];
$password = $_POST["password"];
$stmt = $conn->prepare("SELECT COUNT(*) FROM `users` WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$results = $stmt->get_result();
if($results->fetch_row()[0] != 0) {
echo "Username already exists";
} else {
$hashedPass = password_hash($password, PASSWORD_DEFAULT);
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
$stmt->bind_param("ss", $username, $hashedPass);
$stmt->execute();
echo "Successfully created account";
}
}