forked from KristianZH/PCHMI-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreg.php
47 lines (39 loc) · 1.03 KB
/
reg.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
<?php
require "config/Database.php";
session_start();
if(isset($_SESSION['username'])){
header("Location: index.html");
}
if($_POST){
registerUser();
}
function registerUser(){
global $conn;
$username = $_POST['username'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
if(isAllreadyRegistered($username)){
http_response_code(400);
die(errorMessageInJSON('Username is taken!'));
}
$sql = "INSERT INTO users VALUES (null,'$username','$password')";
$query = $conn->query($sql) or die('Request unsuccessful');
session_start();
$_SESSION['username'] = $username;
header("Location: index.php");
die();
}
function isAllreadyRegistered($username){
global $conn;
$sql = "SELECT * FROM users WHERE username='$username'";
$query = $conn->query($sql) or die(errorMessageInJSON('Request unsuccessful'));
$row = $query->fetch(PDO::FETCH_ASSOC);
if(!$row){
return false;
}else{
return true;
}
}
function errorMessageInJSON($text){
return json_encode(array('error' => $text), JSON_UNESCAPED_UNICODE);
}
?>