Logging In The User - VirajKanse/Student_Authentication GitHub Wiki

Now the code that logs the user in is to be written in the same "server.php" file. So open the "server.php" file and add this code at the end of the file:


// LOGIN USER
	if (isset($_POST['login_user'])) {
		$rl = mysqli_real_escape_string($db, $_POST['rl']);
		$enr = mysqli_real_escape_string($db, $_POST['enr']);

		if (empty($rl)) {
			array_push($errors, "rl is required");
		}
		if (empty($enr)) {
			array_push($errors, "enr is required");
		}

		if (count($errors) == 0) {
			$enr = md5($enr);
			$query = "SELECT * FROM users WHERE rl='$rl' AND enr='$enr'";
			$results = mysqli_query($db, $query);

			if (mysqli_num_rows($results) == 1) {
				$_SESSION['rl'] = $rl;
				$_SESSION['success'] = "You are now logged in";
				header('location: index.php');
			}else {
				array_push($errors, "Wrong rl/enr combination");
			}
		}
	}

?>