Script 12.8 Login with sessions

Output

Login

For testing purposes:
Email address = test@mail.com
Password = 1234

Source
<?php # Script 12.8 - login using sessions
// This page processes the login form submission.
// The script now uses sessions.

if($_SERVER['REQUEST_METHOD'] == 'POST') {
	// For processing the login:
	require (CHAPTER_PATH.'/'.$chapter.'/includes/12.2.php');

	// Need the database connection:
	require (CONNECT);
		
	// Check the login:
	list ($check, $data) = check_login($link, $_POST['email'], $_POST['pass']);
	require(DISCONNECT); // Close the database connection.

	if ($check) { // OK!

		// Set the session data:
		$_SESSION['user_id'] = $data['user_id'];
		$_SESSION['first_name'] = $data['first_name'];
		$_SESSION['last_name'] = $data['last_name'];

		if(isset($_SESSION['errors'])) {
			unset($_SESSION['errors']);
			unset($_SESSION['email']); 
		}
		// Redirect user to login confirmation page
		redirect_user('index.php?chapter=12&path=using+sessions&script=12.9');
			
	} else { // Unsuccessful -redirect user to same page storing any errors in a session to be processed in the form
		// Assign $data to $errors for login_page.inc.php:
		$errors = $data;
		$_SESSION['errors'] = $data;
		$_SESSION['email'] = $_POST['email'];
		redirect_user('index.php?chapter=12&path=using+sessions&script=12.8');
		}
} // End of the main submit conditional.
// Create the page:

include (CHAPTER_PATH.'/'.$chapter.'/includes/12.1.php');
?>