forked from blabla1337/skf-workshop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.php
executable file
·70 lines (61 loc) · 2.15 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
<?php
ob_start();
session_start();
login(); ?>
<?php require_once('includes/menu.php'); ?>
<div id="site_content">
<div class="sidebar">
</div>
<div id="content">
<div style="margin-left:50%;" class="form_settings">
<br/><br/>
<h1>Please login!</h1>
<form method="post">
<h4>username:</h4>
<input type="text" value="<?php if(!empty($_POST['username'])){ echo $_POST['username'];} ?>" name="username"/>
<h4>Password</h4>
<input type="password" name="password"/>
<br/><br/>
<input class="submit" type="submit" value="login" name="contact_submitted">
<br/><br/><br/>
</form>
</div>
</div>
</div>
<?php include('includes/footer.php');
function login(){
$db = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', 'root');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
if(!empty($_POST['contact_submitted'])){
$username = $_POST['username'];
$password = $_POST['password'];
$stmt = $db->prepare("SELECT * FROM users WHERE username=?");
$stmt->execute(array($username));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(!$rows){
echo "<script>alert('wrong username/password');</script>";
}
/*
Than we validate the password, if the validation is true than we set the sessions
For more detailed information on password validation check please look into the
Password storage(salting/stretching/hashing) in the knowledgebase for more information.
*/
foreach($rows as $loginUser){
if($loginUser['password'] == $password)
{
session_start();
$_SESSION['userID'] = $loginUser['userID'];
$_SESSION['access'] = "active";
//The CSRF token is set here by an aproved random number generator
$_SESSION['csrf'] = base64_encode(openssl_random_pseudo_bytes(128));
header("location:loggedin.php?userID=".$loginUser['userID']."");
return $loginUser;
}
else{
echo "<script>alert('wrong password!');</script>";
}
}
}
}
?>