
<?php
require_once('template_header.php');
?>

<script type="text/javascript">
    $(document).ready( function() {
            $.getJSON('https://eden.imt-lille-douai.fr/~liang.wu/API_LOGIN/identite/read.php',
            function(json){
                        // console.log(json);
                        var table = <?php echo json_encode($users); ?>;
                        table.php('');

                        // cf. https://api.jquery.com/jQuery.getJSON/
                        $.each( json, function( key, val ) {
                            table.append(val['login']+'=>'+val['password']+',');
                        });
                    }
            )
        });
</script>

<?php

$users = array();

$login = "anonymous";
$errorText = "";
$successfullyLogged = false;

if(isset($_GET['login']) && isset($_GET['password'])){
    $tryLogin=$_GET['login'];
    $tryPwd=$_GET['password'];

        //si le mdp est correct
        if(array_key_exists($tryLogin,$users) && $users[$tryLogin]==$tryPwd){
            $successfullyLogged = true;
            $login = $tryLogin;
        } else
        $errorText = "Erreur de login/password1";

} else
    $errorText = "Merci d'utiliser le formulaire de login";
        
if(!$successfullyLogged) {
    echo $errorText;
} else {
        session_start();
        $_SESSION['login']=$login;
        $_SESSION['password']=$password;
        header('Location: home.php');


}

?>
