<?php
session_start();
include("script/condb.php");

  // Now we check if the data from the login form was submitted, isset() will check if the data exists.
if ( !isset($_POST['userN'], $_POST['pswd']) ) {
	// Could not get the data that should have been sent.
	exit('Please fill both the username and password fields!');
}
// Prepare our SQL, preparing the SQL statement will prevent SQL injection.
if ($stmt = $con->prepare('SELECT person_id, password FROM upline_app_users WHERE email = ?')) {
	// Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
	$stmt->bind_param('s', $_POST['userN']);
	$stmt->execute();
	// Store the result so we can check if the account exists in the database.
	$stmt->store_result();
//--------------------------------------------------------
	if ($stmt->num_rows > 0) {
	$stmt->bind_result($id, $password);
	$stmt->fetch();
	// Account exists, now we verify the password.
	// Note: remember to use password_hash in your registration file to store the hashed passwords.
	if ($_POST['pswd'] === $password) {
		// Verification success! User has logged-in!
		// Create sessions, so we know the user is logged in, they basically act like cookies but remember the data on the server.
		session_regenerate_id();
		$_SESSION['loggedin'] = TRUE;
		$_SESSION['username'] = $_POST['userN'];
		$_SESSION['person_id'] = $id;
		header('Location: lander.php');
		
		$date_added =date("Y-m-d H:i:s");
		
	$sql1="UPDATE upline_app_users SET lastactivity ='$date_added' WHERE person_id =$id";
		$check=mysqli_query($con, $sql1);
	
		//echo 'Welcome back, ' . htmlspecialchars($_SESSION['name'], ENT_QUOTES) . '!';
	}
		
		else {
		// Incorrect password
		echo 'Incorrect username and/or password!';
	}
} else {
	// Incorrect username
	echo 'Incorrect username and/or password!';
}
	
	

	$stmt->close();
}



?>
