-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.php
More file actions
80 lines (68 loc) · 2.76 KB
/
Copy pathauth.php
File metadata and controls
80 lines (68 loc) · 2.76 KB
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
<?php
session_start();
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'phppoll';
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if (mysqli_connect_errno()) {
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}
if (!isset($_POST['username'], $_POST['password'])) {
$_SESSION['msg'] = "Please fill both the username and password fields!";
header('Location: login.php');
exit;
}
// To prevent sql injection
if ($stmt = $con->prepare('SELECT id, password, ip, name FROM accounts WHERE username = ?')) {
// 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['username']);
$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, $ip, $name);
$stmt->fetch();
// Account exists, now we verify the password.
// Note: remember to use password_hash in your registration file to store the hashed passwords.
// Kalo ga mau di hash pake ini
//if ($_POST['password'] === $password) {
if (password_verify($_POST['password'], $password)) {
// 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['name'] = $name;
$_SESSION['id'] = $id;
// update the ip
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$current_ip = $_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
} else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$current_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$current_ip = $_SERVER['REMOTE_ADDR'];
}
$_SESSION['ip'] = $current_ip;
// update ip in db
if ($current_ip != $ip) {
$stmt = $con->prepare('UPDATE accounts SET ip = ? WHERE id = ?');
$stmt->bind_param('si', $current_ip, $id);
$stmt->execute();
}
if ($_POST['username'] == "eriectan") {
$_SESSION['su'] = true;
} else {
$_SESSION['su'] = false;
}
header('Location: index.php');
} else {
$_SESSION['msg'] = "Incorrect password!";
header('Location: login.php');
}
} else {
$_SESSION['msg'] = "There is no account with that username!";
header('Location: login.php');
}
$stmt->close();
}
?>