-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.php
87 lines (74 loc) · 2.59 KB
/
login.php
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php require("layout.php"); ?>
<?php render("header", ["title"=>"Login"]); ?>
<?php
// if someone is already logged in then redirect
if (!empty($_SESSION['id'])){
flash_message('OOPS!','Already logged in');
echo "<script>location.href='index.php';</script>";
}
// for the form's data processing
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// ensure user co-operated
if(!empty($_POST['username'])&&!empty($_POST['password']))
{
/** ensure user is there in database */
require("dbconfig.php");
// Create connection
$conn = new mysqli($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// for any creepy thing https://www.w3schools.com/php/php_form_validation.asp
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$username = test_input($_POST["username"]);
// Querying the database
$username = $username;
$password = crypt($_POST["password"], 50);
$sql = 'SELECT * FROM users where uname = "'.$username. '" AND hash = "'.$password.'"';
$result = $conn->query($sql);
// if the data is fake
$data = $result->fetch_assoc();
if (empty($data)){
echo '<script language="javascript">';
echo 'window.alert("Invalid Username Or Password")';
echo '</script>';
}
// all goes well then
$_SESSION['id'] = $data['id'];
$_SESSION['name'] = $data['uname'];
echo "<script>location.href='index.php';</script>";
}
// if something goes invalid
else
{
echo '<script language="javascript">';
echo 'window.alert("Invalid Username Or Password")';
echo '</script>';
}
}
?>
<!-- html for the login form -->
<form action="<?= htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<fieldset>
<div class="form-group">
<input autocomplete="off" autofocus class="form-control" name="username" placeholder="Username" type="text"/>
</div>
<div class="form-group">
<input class="form-control" name="password" placeholder="Password" type="password"/>
</div>
<div class="form-group">
<button class="btn btn-default" name="login" type="submit">Log In</button>
</div>
<div class="form-group">
<br> or a new user <a href="register.php">Register</a>?
</div>
</fieldset>
</form>
<?php render("footer"); ?>