<?php
// dietetika login
session_start();

$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'plcollage_root';
$DATABASE_PASS = '#Ef5IQ)%15+*';
$DATABASE_NAME = 'plcollage_calendar';

$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) {

	exit('Nem lehet csatlakozni az adatbázishoz!: ' . mysqli_connect_error());
}
if ( !isset($_POST['username'], $_POST['password']) ) {

	exit('Kérjük töltse ki mindkét mezőt!');
}

if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
	$stmt->bind_param('s', $_POST['username']);
	$stmt->execute();
	$stmt->store_result();
  if ($stmt->num_rows > 0) {
  	$stmt->bind_result($id, $password);
  	$stmt->fetch();

  	if ($_POST['password'] === $password) {

  		session_regenerate_id();
  		$_SESSION['loggedin'] = TRUE;
  		$_SESSION['name'] = $_POST['username'];
  		$_SESSION['id'] = $id;
  		header('Location: ./home.php');
  	} else {
		$msg = "Hibás belépési adatok!";
			header('Location: ./index.html');
  	}
    }  else {
	    $msg = "Hibás belépési adatok!";
	        header('Location: ./index.html');
    }
	$stmt->close();
}
?>
