<?php 
include '../backend/db/db.php';
session_start();

/****************************** Guest User **********************************/

if (isset($_SESSION['temp_user_cey'])) {
	$tmp_id = htmlspecialchars(strip_tags(trim($_SESSION['temp_user_cey'])), ENT_QUOTES, 'UTF-8');

	if (!empty($tmp_id)) {
		$sql_invo = mysqli_query($conn, "SELECT inv_id FROM live_cart WHERE tmp_id	= $tmp_id");

		if(mysqli_num_rows($sql_invo) == 0) {
			if (isset($_SESSION['file'])) {
				$file = strip_tags(trim($_SESSION['file']));
				$_SESSION['guest_user_cey'] = $tmp_id;

			} else {
				$file = '';
			}

			$redirectUrl = "../$file";
			echo "<script type=\"text/javascript\">";
			echo "window.location.href = '$redirectUrl'";
			echo "</script>";

		} else {
			$row_invo = mysqli_fetch_assoc($sql_invo);
			$inv = $row_invo['inv_id'];
			
			$_SESSION['guest_user_cey'] = $inv;
			$sql_up = "UPDATE live_cart SET User_ID='G$inv' WHERE tmp_id = $tmp_id";
			
			if (mysqli_query($conn, $sql_up)) {
				$redirectUrl = "../checkout";
				echo "<script type=\"text/javascript\">";
				echo "window.location.href = '$redirectUrl'";
				echo "</script>";
			}
		}

	} else {
		$redirectUrl = "../";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";
	}

} else {
	$redirectUrl = "../";
	echo "<script type=\"text/javascript\">";
	echo "window.location.href = '$redirectUrl'";
	echo "</script>";
}
?>