summaryrefslogtreecommitdiff
path: root/signin.php
blob: 287eedab86da70fa3265547e1792b317fd554b2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

include_once 'includes/db_inc.php';
include_once 'header.php';

echo '<section><h2>Sign in</h2>';

if ($_SERVER['REQUEST_METHOD'] != 'POST') {
	echo '
	<form action="" method="post">
		<label for="user_name">Username: </label><br>
		<input type="text" name="user_name"><br>
		<label for="user_pass">Password: </label><br>
		<input type="password" name="user_pass"><br>
		<input type="submit" name="submit">
	</form>
	';
} else {
	$errors = array();

	if (!isset($_POST['user_name'])) {
		$errors[] = 'Please provide a username.';
	}

	if (!isset($_POST['user_pass'])) {
		$errors[] = 'Please provide a password.';
	}

	if (!empty($errors)) {
		echo 'Please check the following problems: <ul>';
		foreach ($errors as $err) {
			echo '<li>' . $err . '</li>';
		}
		echo '</ul>';
	} else {
		$sql = "SELECT user_id, user_name FROM users WHERE user_name = '" . mysqli_real_escape_string($dbc, $_POST['user_name']) . "' AND user_pass = '" . sha1($_POST['user_pass']) ."'";
		$result = mysqli_query($dbc, $sql);

		if (!$result) {
			echo 'An error occurred while signing in.';
			echo mysqli_error($dbc);
		} else {
			if (mysqli_num_rows($result) == 0) {
				echo 'There is no user with that username/password combination! Please try again';
			} else {
				$_SESSION['signed_in'] = true;

				while ($row = mysqli_fetch_assoc($result)) {
					$_SESSION['user_id'] = $row['user_id'];
					$_SESSION['user_name'] = $row['user_name'];
				}

				echo 'You are now signed in as ' . $_SESSION['user_name'];
			}
		}
	}
}

echo '</section>';
include_once 'footer.php';