-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_functions.php
88 lines (79 loc) · 2.77 KB
/
common_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
// Vraca ID korisnika na temelju korisnickog imena
function getUserIdByUsername($username) {
global $database;
try {
$statement = $database->prepare(
'SELECT id
FROM dz2_users
WHERE BINARY username = :username'
);
$statement->execute(
array(
'username' => $username
)
);
} catch (PDOException $e) {
unset($_SESSION['username']);
$_SESSION['error_message'] = $e->getMessage();
header('Location: login.php');
exit();
}
$row = $statement->fetch();
if ($row === false) {
$_SESSION['error_message'] = 'User @' . $_POST['username-input'] . ' does not exist.';
header('Location: following.php');
exit();
} else {
return $row['id'];
}
}
// Prikazuje quackove na temelju danog statementa
function displayQuacks($statement) {
foreach ($statement->fetchAll() as $row) {
$date = DateTime::createFromFormat('Y-m-d H:i:s', $row['date']);
$formattedDate = $date->format('F d, Y \a\t H:i:s');
$row['quack'] = preg_replace('/#(\w+)/', '<a href="https://rp2.studenti.math.hr/~kustufil/dz2/search.php?tag=$1">$0</a>', $row['quack']);
echo '<p>';
echo '<span>@' . $row['username'] . ' - ' . $formattedDate . '</span>';
echo '<br>';
echo $row['quack'];
echo '</p>';
}
}
function initial_commands() {
if (isset($_POST['logout'])) {
session_unset();
session_destroy();
header('Location: login.php');
exit();
}
if (!isset($_SESSION['username'])) {
header('Location: login.php');
exit();
}
}
function display_header_and_nav() {
?>
<header>
<div>
Quack!
</div>
<div>
<span>@<?php echo $_SESSION['username']; ?></span>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" value="logout" name="logout">
</form>
</div>
</header>
<nav>
<img src="quack.jpg" alt="Duck">
<a href="my_quacks.php">My quacks</a>
<a href="following.php">Following</a>
<a href="followers.php">Followers</a>
<a href="mentions.php">quacks @<?php echo $_SESSION['username']; ?></a>
<a href="search.php">#search</a>
</nav>
<?php
}
?>