This repository was archived by the owner on Feb 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
64 lines (46 loc) · 1.71 KB
/
functions.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
60
61
62
63
64
<?php
include_once ("config.php");
function GenerateToken() { //This will generate an token and store it in the database
$con = mysqli_connect(SERVER_NAME, USER_NAME, PASSWORD, DATABASE_NAME);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 128);
$sql = "INSERT INTO " . TOKEN_TABLE . " (token) VALUES (\"" . $randomString . "\");";
mysqli_query($con, $sql);
mysqli_close($con);
return $randomString;
}
function CheckToken($token = "No token") { //This will check if the token is in the database and if it is used.
$con = mysqli_connect(SERVER_NAME, USER_NAME, PASSWORD, DATABASE_NAME);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
/* This is me learning how to use MYSQLI and prepared statements. */
if ($stmt = $con -> prepare("SELECT * FROM `" . TOKEN_TABLE . "` WHERE token=?")) {
/* bind parameters for markers */
$stmt -> bind_param("s", $token);
/* execute query */
$stmt -> execute();
/* bind result variables */
$stmt -> bind_result($tokens, $used);
/* fetch value */
$stmt -> fetch();
/* close statement */
$stmt -> close();
}
if ($tokens == $token && $used == 0) {
printf("Token: %s <br /> Used: %s <br /> Message: " . $_POST['f'] . "", $tokens, $used);
if ($stmt = $con -> prepare("UPDATE " . TOKEN_TABLE . " SET `used`='1' WHERE `token`=?")) {
/* bind parameters for markers */
$stmt -> bind_param("s", $tokens);
/* execute query */
$stmt -> execute();
/* close statement */
$stmt -> close();
}
} else {
printf("Token invalid or already used.");
}
}
?>