-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs.php
58 lines (52 loc) · 1.6 KB
/
funcs.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
<?php
function verify_email($email){
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
function verify_username($user){
return (bool)preg_match("/^[a-zA-Z0-9_ #]*$/",$user);
}
function username_exists($conn,$user){
$query = "SELECT * FROM users WHERE username = ?;";
$stmt = $conn->prepare($query);
$stmt->bind_param("s", $user);
$stmt->execute();
$result = $stmt->get_result();
$x = $result->num_rows > 0;
$stmt->close();
return $x;
}
function email_exists($conn,$mail){
$query = "SELECT * FROM users WHERE email = ?;";
$stmt = $conn->prepare($query);
$stmt->bind_param("s", $mail);
$stmt->execute();
$result = $stmt->get_result();
$x = $result->num_rows > 0;
$stmt->close();
return $x;
}
function Register($conn,$name,$email,$pwd){
$query = "INSERT INTO users(username,email,password) VALUES(?,?,?);";
$stmt = $conn->prepare($query);
$hashed_pwd = password_hash($pwd,PASSWORD_DEFAULT);
$stmt->bind_param("sss", $name,$email,$hashed_pwd);
$stmt->execute();
$stmt->close();
}
function Login($conn,$uname,$pwd){
$query = "SELECT * FROM users WHERE email = ? OR username = ?;";
$stmt = $conn->prepare($query);
$stmt->bind_param("ss", $uname,$uname);
$stmt->execute();
$result = $stmt->get_result();
$row = mysqli_fetch_assoc($result);
$hashed_pwd = ($result->num_rows === 0) ? "" : $row['password'];
$stmt->close();
return ($result->num_rows === 0 || !password_verify($pwd,$hashed_pwd)) ? false : $row['id'];
}
function logout(){
session_destroy();
exit(header("Location: /"));
return -1;
}
?>