<?php

   session_start();

   // setcookie("user", "1", time() + 31500000, "");
/*
   if(!isset($_SESSION["WORKMODE"])) {
      header("Location: ../index.php");
   }
*/
   if(!isset($_SESSION["LoggedIn"]))  $_SESSION["LoggedIn"]  = 0;
   if(!isset($_SESSION["LoggedOut"])) $_SESSION["LoggedOut"] = 0;

   if(!isset($_COOKIE["user"])) {
      $_SESSION["user"]      = 0;
      $_SESSION["candidate"] = 0;
      setcookie("user", "0", time() + 31500000, "");      
   }

   $_SESSION["name"] = "";

   error_reporting(E_ALL);
   ini_set('display_errors', 1);

   include "../pdo_connection.php";

   $user = array("mail" => "", "name" => "", "remember" => 1, "autoload" => 1, "hash" => "");

   if(isset($_COOKIE["user"]) && (int)$_COOKIE["user"] > 0) {
      $stmt = $pdo->prepare('SELECT mail, name, remember, autoload, hash FROM users WHERE id = ?');
      $stmt->execute(array($_COOKIE["user"]));
      $user = $stmt->fetch();
      if($user) {
         if($user["hash"] != $defp) $user["hash"] = "";
         $_SESSION["candidate"] = $_COOKIE["user"];
         $_SESSION["name"] = $user["name"];
      } else {
         $_SESSION["user"]      = 0;
         $_SESSION["candidate"] = 0;
         $user = array("mail" => "", "name" => "", "remember" => 1, "autoload" => 1);
      }
   }


?>