-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
126 lines (118 loc) · 4.09 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
session_start();
if(isset($_GET['signout'])){
session_destroy();
header("location:login.php");
}else if(isset($_SESSION['userId'])){
header("location:index.php");
}else if(isset($_POST['submit'])){
$userName = $_POST['username'];
$password = $_POST['password'];
require 'php-parts/db-connection.php';
$sql= "SELECT u.user_id,r.role FROM users AS u,roles as r WHERE r.role_id=u.role_id AND user_name='$userName' AND password='$password'";
$stmt = $conn->prepare($sql);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$_SESSION['userId'] = $row['user_id'];
$_SESSION['userRole'] = $row['role'];
if($_SESSION['userRole']=="admin"){
header("location:ad-index.php");
}else{header("location:index.php");}
}
if(!isset($_SESSION['userId'])){
$msg ="!! Felaktig användarnamn eller lösenord. !!";}
}
else if(isset($_POST['registrate'])){
$regmessages = "";
$customerRole = 'customer';
$firstName = $_POST['firstname'];
$lastName = $_POST['lastname'];
$email = $_POST['email'];
$userName = $_POST['username'];
$password = $_POST['password'];
$repeatPassword = $_POST['repeatpassword'];
if ($password == $repeatPassword) {
require 'php-parts/db-connection.php';
$sql= "SELECT create_user('$userName', '$password', '$firstName', '$lastName', '$email', '$customerRole') ";
$stmt = $conn->prepare($sql);
try{
$stmt->execute();
header("location:login.php");
$regmessages = "Registration successful";}
catch(Exception $e){
$regmessages = "The username or email is already registrated in our system";
}
}
else {
$regmessages = "You have not entered matching passwords";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="shortcut icon" href="img/icon.png" type="image/x-icon">
<link href="https://fonts.googleapis.com/css?family=Fugaz+One|Lato|Nova+Flat|Sofia&display=swap" rel="stylesheet">
<link rel="stylesheet" href="css/login.css">
<title>Inloggning</title>
</head>
<body>
<form id="login_form" method="POST">
<img src="img/logo.png">
<input type="text" name="username" placeholder="Användarnamn" required autocomplete="off">
<input type="password" name="password" placeholder="Lösenord" required autocomplete="off">
<button name="submit" type="submit" value="login">Logga in</button>
<?php
if(isset($msg)){
echo "<h5 id='msg'>".$msg."</h5>";
}
?>
<span onclick="show('reg')">Skapa ett konto</span>
</form>
<form id="reg_form" method="POST">
<img src="img/logo.png">
<div>
<div>
<label for="firstname"><b>Förnamn</b></label>
<input type="text" name="firstname" required>
</div>
<div>
<label for="lastname"><b>Efternamn</b></label>
<input type="text" name="lastname" required>
</div>
<div>
<label for="email"><b>Email</b></label>
<input type="email" name="email" required>
</div>
<div>
<label for="username"><b>Användarnamn</b></label>
<input type="text" name="username" required>
</div>
<div>
<label for="password"><b>Lösenord</b></label>
<input type="password" name="password" required>
</div>
<div>
<label for="repeatpassword"><b>Repetera lösenord</b></label>
<input type="password" name="repeatpassword" required>
</div>
</div>
<button name="registrate" type="submit" value="registrate">Registrera</button>
<?php
if(isset($regmessages)&& $regmessages!=""){
echo "<h5 id='msg'>".$regmessages."</h5>";
}
?>
<span onclick='show("log")'>Logga in</span>
</form>
</body>
<script src="js/login.js"></script>
<?php
if(isset($regmessages)){
echo '<script> show("reg"); </script>';
}
?>
</html>