<?php

if (!empty($_SESSION['id_user'])) {

	if (empty($_SESSION['loja'])) {
		
		$id_user = $_SESSION['id_user'];

		$query_sql = "SELECT * FROM user WHERE id = $id_user AND loja_id IS NOT NULL AND loja_id <> 0";

		$query_user = mysqli_fetch_object(mysqli_query($conn, $query_sql));

		if ($query_user) {
			
			$_SESSION['loja'] = $query_user->loja_id;

		} else {

			$query_sql = "SELECT * FROM user WHERE id = $id_user";
			$query_user = mysqli_fetch_object(mysqli_query($conn, $query_sql));

			if ($query_user->user_geral == 'sim') {
				
				$_SESSION['loja'] = 'todas';

			} else {

				header('Location: exit.php');
			}
		}
		
	}

} else {
	header("Location: /sistema");
}

?>