<!doctype html>
<html lang=fr>



<head>
	<title>Passeport Pro BTS CG</title>
	<meta charset=utf-8> 
	<meta name=viewport content="width=device-width, initial-scale=1">
	<link rel="stylesheet" type="text/css" href="nogstyle.css">

	
</head>
	
<body>
	<header>
		<?php 
			include('includes/header_simple.inc')
		?>
	</header>
	
	<article>
			<h2>Connexion</h2>

			<form class="centre60" action="index.php" method="post">
				<p class="commentaire">
					Bonjour. Entrez votre identifiant et votre mot de passe
				</p>			
				<br/>
				<p><label class="label130d" for="id">Identifiant</label>
					<input id="login" type="text" name="login" size="30" maxlength="30" value="<?php if (isset($_POST['login'])) echo $_POST['login']; ?>" >
				</p>
				<p>
					<label class="label130d" for="psword">Mot de passe</label>
					<input id="psword" type="password" name="psword" size="12" maxlength="12" value="<?php if (isset($_POST['psword'])) echo $_POST['psword']; ?>" >		
				</p>
				<p class="centre60">
					<input type="submit" class="bloc" name="submit" value="Connexion">			
				</p>
			</form>
			<br/>
			<br/>
		<p align="right">
			<a href="temp_dde_insc.php">&nbsp;Inscription&nbsp;</a>
			<br/><br/>
		</p>	
	</article>
	


		
		<footer>
				<?php include ('includes/footer.inc'); ?>
		</footer>
		

		
		<?php 
			$erreur= array();
			// Check if the form has been submitted:
			if ($_SERVER['REQUEST_METHOD'] == 'POST')
			{
				//connect to database
				require ('connect/mysqli_connect.php');
			
				// Validation login
				if (empty($_POST['login']))  // est ce que le login est rempli ?
				{
					$erreur[]='Entrez votre identifiant';
				}
				else      //Est ce que le login existe ?
				{
					$login = mysqli_real_escape_string($dbcon, $_POST['login']);	
					$q = "	SELECT NumAct 
							 	FROM acteur
					 			WHERE LoginAct='$login' ";
					$result = mysqli_query ($dbcon, $q); 
					// Check the result:
					$r=@mysqli_num_rows($result);
					if ($r ==0)
					{
						$erreur[]="Cet identifiant n'existe pas";
					}
					elseif ($r!==1)
						$erreur[]="Identifiants multiples : contacter l\'admin";
				}
				
				// Validation mdp
				if (empty($_POST['psword'])) //Est ce que le mdp est vide ?
				{
					$erreur[]='Entrez votre mot de passe ';
				}
				else //est ce que le mdp est correct ?
				{
					$mdp = mysqli_real_escape_string($dbcon, $_POST['psword']);
					
					//Select numacteur + autorisation à parter du login et mdp
					$q = "SELECT NumAct,Autorisation,PrenomAct,NomAct,SessionExam,CodeEtab,NomEtab
					 FROM acteur,etablissement
					 WHERE (LoginAct='$login' AND MdpAct=('$mdp'))
					 AND acteur.codeEtablissement=etablissement.codeEtab";	
					
					$result = mysqli_query ($dbcon, $q); 
					// Check the result:
					$r=@mysqli_num_rows($result);
					if ($r !== 1)
					{ 
						$erreur[]='Le mot de passe est incorrect';
					}
				}
					
				if (empty($erreur))  //s'il n'y a pas d'erreurs 
				{
					session_start();// Start the session,
					$_SESSION = mysqli_fetch_array ($result, MYSQLI_ASSOC); // fetch the record and insert the three values in an array
					$_SESSION['Autorisation'] = (int) $_SESSION['Autorisation']; // Ensures the user level is an integer.
					//Renvoie à la page correspondant àl' Autorisation de sécurité	
					switch ($_SESSION['Autorisation']) 
						{
						   case 0:
		        				header('location: etudiant_accueil.php');
		        				exit;
		    				case 1:
		        				header('location: prof_accueil.php');
		        				exit;
		    				case 2:
		        				header('location: admin_accueil.php');
		        				exit;
						}
						mysqli_free_result($result);
				}
				else 
				{
					echo '<p class="alert echec "> ERREURS !! Essayez encore. : <br />';
					foreach ($erreur as $msg)
					{ // Echo each error.
						echo " - $msg<br />\n";
					}
					echo'</p>';
				}
				mysqli_close($dbcon);
				// End of SUBMIT conditional
			}
		?>
</body>



</html>