/
_header.php
78 lines (72 loc) · 2.4 KB
/
_header.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
<?php
session_start();
function currentUrl() {
$pageURL = 'http';
if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["SCRIPT_NAME"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["SCRIPT_NAME"];
}
// FYI: You could also append the query string if that was important to you
return $pageURL;
}
function checkAuth($redirectIfNeeded) {
// is the user already logged in?
if (isset($_SESSION["uid"]) && $_SESSION["uid"] != "") {
// yes, already logged in
return $_SESSION["uid"];
} else if ($redirectIfNeeded) {
// user is not logged in and needs to do so
// send to the login page
// pass the current URL so that we can come back here after login
$currentUrl = currentUrl();
// rawurlencode converts the string so it's safe to pass as a URL GET parameter
$urlOfLogin = "login.php?sendBackTo=".rawurlencode($currentUrl)."&cb=".microtime(true);
// use a JavaScript redirect; FYI, there's also an http header (Location:) that
// can be used to redirect, but that MUST be sent before any HTML, and this
// function (checkAuth) might be called after some HTML is sent
echo "<script>location.replace('$urlOfLogin');</script>";
return "";
} else {
// user is not logged in, but whoever called this function doesn't care
return "";
}
}
?>
<head>
<link rel="stylesheet" type="text/css" href="CSS/newstyle.css">
<script src="JS/jquery-1.12.0.js"></script>
<script src="JS/jquery-ui-1.11.4/jquery-ui.js"></script>
</head>
<body>
<header>
<img src="Images/WS_Alpha.png" id="logo"></img>
<nav>
<ul id="menu">
<li><a href="index.php">Home</a>
<?php
if (checkAuth(false) == "") {
?>
<li><a href="add_user.php">Register</a>
<li><a href="login.php">Login</a>
<li><a href="leaderboard.php">Leaderboard</a>
<?php
}
else {
?>
<li><a href="logout.php?cb=<?= microtime(true) ?>?sendBackTo=<? $currentUrl ?>">Logout</a>
<li><a href="leaderboard.php">Leaderboard</a>
<li><a href="add_image.php">Upload</a>
<?php
}
?>
</ul>
</nav>
</header>
<main>
<?php
ini_set('display_errors', 'On');
$mysqli = new mysqli("oniddb.cws.oregonstate.edu", "leonl-db", "mCvXbcy9WsvzmzJ9", "leonl-db");
?>