-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
71 lines (65 loc) · 1.61 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
65
66
67
68
69
70
71
<?php
/*
* Common functions used throughout Codejudge
*/
session_start();
// Connects to the database
function connectdb() {
include('dbinfo.php');
$conn = new mysqli($host, $user, $password, $database);
if ($conn->connect_error) {
die('Could not connect to MySQL: ' . $conn->connect_error);
}
return $conn;
}
// Generates a random number
function randomNum($length) {
$rangeMin = pow(36, $length-1);
$rangeMax = pow(36, $length)-1;
$base10Rand = mt_rand($rangeMin, $rangeMax);
$newRand = base_convert($base10Rand, 10, 36);
return $newRand;
}
// Checks if any user is logged in
function loggedin() {
return isset($_SESSION['username']);
}
// Gets the user ID
function getUserid() {
$emailid = $_SESSION['username'];
$conn = connectdb();
$stmt = $conn->prepare("SELECT uid, name FROM users WHERE email = ?");
$stmt->bind_param("s", $emailid);
$stmt->execute();
$stmt->bind_result($uid, $name);
$stmt->fetch();
$stmt->close();
$conn->close();
return $uid;
}
// Gets the user's name
function name() {
$emailid = $_SESSION['username'];
$conn = connectdb();
$stmt = $conn->prepare("SELECT uid, name FROM users WHERE email = ?");
$stmt->bind_param("s", $emailid);
$stmt->execute();
$stmt->bind_result($uid, $name);
$stmt->fetch();
$stmt->close();
$conn->close();
return $name;
}
// Gets the name by user ID
function getName($uid) {
$conn = connectdb();
$stmt = $conn->prepare("SELECT name FROM users WHERE uid = ?");
$stmt->bind_param("i", $uid);
$stmt->execute();
$stmt->bind_result($name);
$stmt->fetch();
$stmt->close();
$conn->close();
return $name;
}
?>