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
$username = $_COOKIE['username'];
$password = $_COOKIE['password'];
function login($username, $password) {
if (!$username) return false;
if (!$password) return false;
return true;
}
function check_login() {
global $username, $password;
if (!login($username, $password)) return false;
return true;
}
require_once "../lib/db.php";
function get_cart_count() {
global $username, $cursor;
$statement = $cursor->prepare("select sum(cart.count) as count from cart join customer on customer.id = cart.customer join product on product.id = cart.product where customer.name = ?");
$statement->bind_param("s", $username);
if (!$statement->execute()) return 0;
$res = $statement->get_result();
if (!mysqli_num_rows($res)) return 0;
$obj = $res->fetch_object();
return $obj->count;
}
$logged_in = check_login();
$cart_count = get_cart_count();
function if_logged_in($is, $redirect, $back = false) {
global $logged_in;
if ($logged_in != $is) return;
if ($back) {
$prev = $_SERVER['HTTP_REFERER'];
$ONE_HOUR = time() + (60 * 60);
setcookie("prev", $prev, $ONE_HOUR, "/");
}
http_response_code(302);
header("Location: ".$redirect);
die();
}
function cookie_redir($username, $password, $url = "") {
$ONE_YEAR = time() + (60 * 60 * 24 * 365);
setcookie("username", $username, $ONE_YEAR, "/");
setcookie("password", $password, $ONE_YEAR, "/"); // TODO: use tokens to login
if (!$url) {
$prev = $_COOKIE['prev'];
if(!$prev) $url = "/";
else $url = $prev;
}
header("Location: ".$url);
die();
}
?>
|