<?php
// login.php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = $_POST['email'];
    $password = $_POST['password'];

    // $conn = new mysqli('localhost', 'root', '', 'teacher_portal');
    include '../config.php';
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $stmt = $conn->prepare("SELECT password FROM teachers WHERE email = ?");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($hashed_password);

    if ($stmt->fetch() && password_verify($password, $hashed_password)) {
        
        $extra="profile.php";
		$_SESSION['login']=$num['email'];
		$_SESSION['id']=$num['id'];
		$_SESSION['username']=$num['name'];
		$uip=$_SERVER['REMOTE_ADDR'];
		$status=1;
		// $log=mysqli_query($con,"insert into userlog(userEmail,userip,status) values('".$_SESSION['login']."','$uip','$status')");
		$host=$_SERVER['HTTP_HOST'];
		$uri=rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
		echo "<script type='text/javascript'> document.location = 'https://$host$uri/$extra'; </script>";
	
		exit();
        
        
        
    //       header("Location: ../Dash.php");
    //   $_SESSION['mess'] = 'Login was successful';
    } else {
        //$_SESSION['errmsg'] = "Invalid email or password.";
        
		echo "<script type='text/javascript'> document.location = '../index.php'; </script>";
	
		$_SESSION['errmsg']="Invalid email id or Password";
		exit();
    }

    $stmt->close();
    $conn->close();
}
?>