<?php

class User
{
    public $username;
    protected $authenticated = False;
    protected $logfile;

    function __construct($username)
    {
        $this->username = $username;
        $this->logfile = "/tmp/logs/user_". md5(random_bytes(20));

        $ufp = fopen($this->logfile, "a");
        $message = "[*] User (" . $this->username . ") created.\n";
        fwrite($ufp, $message);
        fclose($ufp);
    }

    function check_authentication()
    {
        // TODO
    }

    function get_authentication_status()
    {
        if ($this->authenticated)
            return "ok";
        return "ko";
    }

    function __wakeup()
    {
        $ufp = fopen($this->logfile, "a");
        $message = "[*] User (" . $this->username . "), authentication status: " . $this->get_authentication_status() . "\n";
        fwrite($ufp, $message);
        fclose($ufp);
    }

    function __toString()
    {
        return $this->username;
    }

    function __destruct()
    {
        unlink($this->logfile);
    }
}

$u = unserialize(base64_decode($_COOKIE["user"]));
if ($u == false)
{
    $u = new User("test_0");
}

sleep(2);

echo "Hello " . $u;

sleep(2);

?>