<html>
    <head>
        <title></title>
    </head>
    <body>
        <h2 align="center">Logovanje</h2>
<?php

if (isset($_POST['submit'])) {
// forma submitovana
// proveri zahtevane vrednosti
    if (empty($_POST['user']) || empty ($_POST['pass'])) {
        die ("GRESKA: Unesite podatke!");
    }

    // ime XML fajla
    $fajl = "nalozi.xml";

    // load file
    $xml = simplexml_load_file($fajl) or die ("Ne mogu da ucitam XML fajl $fajl!");
    
    $login = false;
    foreach ($xml->user as $user) 
        if ($user->username == $_POST['user'] && $user->password == $_POST['pass'] && $user['aktivan']== "1")
        {
            $user->broj_poseta = $user->broj_poseta + 1;
            file_put_contents($fajl, $xml->asXML());
            $login=true;
        }
    // da li je validan user?
    if($login){
        // brisanje i postavljanje cookie-a
        if (isset($_COOKIE['user']) && $_COOKIE['user'] != $_POST['user'] ){
            setcookie("user", NULL, mktime() - 3600);
            setcookie("pass", NULL, mktime() - 3600);
            //postavi nove cookies
            setcookie("user", $_POST['user'], time()+(84600*3));
            if(isset($_POST['rem']))
            {
              setcookie("pass", $_POST['pass'], time()+(84600*3));
            }
        }
        elseif (!isset($_COOKIE['user']))
            setcookie("user", $_POST['user'], time()+(84600*3));
        elseif(isset($_POST['rem']) && !isset($_COOKIE['pass']))
            setcookie("pass", $_POST['pass'], time()+(84600*3));     
        
        session_start();
        $_SESSION['autorizovan']=1;
        $_SESSION['user'] = $_POST['user'];

        echo "<h2>Uspesno logovanje!</h2>";
        echo "<br />";
        echo "";
        echo "<br />";
        echo "<a href='treca.php'>Treca</a><br />";
        echo "<a href='cetvrta.php'>Cetvrta</a>";
    }
    else {
        // autorizacija neuspela
        echo "Greska: Korisnik nije aktiviran!";
    }
}
else {
// prikazi formu za login
    ?>

        <form style='text-align:center' method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
           Username: <input type="text" name="user" value="<?php if (isset($_COOKIE['user'])) echo $_COOKIE['user']; ?>"><br />
           Password: <input type="password" name="pass" value="<?php if (isset($_COOKIE['pass'])) echo $_COOKIE['pass']; ?>"><br />
           Zapamti lozinku <input type="checkbox" name="rem"><br />
           <input type="submit" name="submit" value="Posalji">
        </form>
    </body>
</html>
<?php
}
?>

