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(); } ?>