Skip to content

Commit f590dd3

Browse files
committed
Better structure
1 parent 779fbd1 commit f590dd3

File tree

6 files changed

+58
-75
lines changed

6 files changed

+58
-75
lines changed

database/database.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ CREATE TABLE IF NOT EXISTS users (
99
);
1010

1111
INSERT INTO users (username, password) VALUES
12-
('test', 'secret'),
1312
('user', 'password'),
13+
('test', 'secret-password'),
1414
('admin', 'admin');
1515

1616

src/home.php

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,23 @@
1-
<?php
2-
session_start();
1+
<?php include 'includes/header.php'; ?>
32

4-
// Controlla se l'utente è autenticato
3+
<?php
4+
// Verifica se l'utente ha effettuato l'accesso correttamente
55
if (!isset($_SESSION['success'])) {
66
header("Location: index.php");
77
exit();
88
}
99
?>
1010

11-
<!DOCTYPE html>
12-
<html lang="en">
13-
<head>
14-
<meta charset="UTF-8">
15-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
16-
<title>SQLi Homework</title>
17-
18-
<!-- Bootstrap CSS -->
19-
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
20-
21-
<!-- Custom CSS -->
22-
<link rel="stylesheet" href="assets/styles.css">
23-
</head>
24-
<body>
25-
<div class="login-container">
26-
<div class="login-box">
27-
<h1 class="text-center">Welcome</h1>
28-
<div class="text-center mt-4 mb-2">
29-
<p>You have successfully logged in</p>
30-
</div>
31-
<div class="d-grid mt-4">
32-
<a href="logout.php" class="btn btn-danger btn-block">Logout</a>
33-
</div>
11+
<div class="login-container">
12+
<div class="login-box">
13+
<h1 class="text-center">Welcome</h1>
14+
<div class="text-center mt-4 mb-2">
15+
<p>You have successfully logged in</p>
16+
</div>
17+
<div class="d-grid mt-4">
18+
<a href="logout.php" class="btn btn-danger btn-block">Logout</a>
3419
</div>
3520
</div>
21+
</div>
3622

37-
</body>
38-
</html>
23+
<?php include 'includes/footer.php'; ?>

src/includes/footer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
</body>
2+
</html>

src/includes/header.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
session_start();
3+
?>
4+
<!DOCTYPE html>
5+
<html lang="en">
6+
<head>
7+
<meta charset="UTF-8">
8+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
9+
<title>SQLi Homework</title>
10+
11+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
12+
13+
<link rel="stylesheet" href="assets/styles.css">
14+
</head>
15+
<body>

src/index.php

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,29 @@
1-
<?php
2-
session_start();
3-
?>
4-
<!DOCTYPE html>
5-
<html lang="en">
6-
<head>
7-
<meta charset="UTF-8">
8-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
9-
<title>SQLi Homework</title>
1+
<?php include 'includes/header.php'; ?>
2+
<div class="login-container">
3+
<div class="login-box">
4+
<h2 class="text-center">Login</h2>
105

11-
<!-- Bootstrap CSS -->
12-
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
13-
14-
<!-- Custom CSS -->
15-
<link rel="stylesheet" href="assets/styles.css">
16-
</head>
17-
<body>
18-
<div class="login-container">
19-
<div class="login-box">
20-
<h2 class="text-center">Login</h2>
21-
22-
<form method="POST" action="login.php" class="mt-4">
23-
<?php if (isset($_SESSION['error'])): ?>
24-
<div class="alert alert-danger" role="alert">
25-
<?php
26-
echo $_SESSION['error'];
27-
unset($_SESSION['error']);
28-
?>
29-
</div>
30-
<?php endif ?>
31-
<div class="form-group mb-3">
32-
<label for="username">Username</label>
33-
<input type="text" class="form-control" name="username" placeholder="Enter username" autocomplete="off">
34-
</div>
35-
<div class="form-group mb-4">
36-
<label for="password">Password</label>
37-
<input type="password" class="form-control" name="password" placeholder="Enter password" autocomplete="off">
38-
</div>
39-
<div class="d-grid mt-4">
40-
<button type="submit" class="btn btn-primary btn-block">Continue</button>
6+
<form method="POST" action="login.php" class="mt-4">
7+
<?php if (isset($_SESSION['error'])): ?>
8+
<div class="alert alert-danger" role="alert">
9+
<?php
10+
echo $_SESSION['error'];
11+
unset($_SESSION['error']);
12+
?>
4113
</div>
42-
</form>
43-
</div>
14+
<?php endif ?>
15+
<div class="form-group mb-3">
16+
<label for="username">Username</label>
17+
<input type="text" class="form-control" name="username" placeholder="Enter username" autocomplete="off">
18+
</div>
19+
<div class="form-group mb-4">
20+
<label for="password">Password</label>
21+
<input type="password" class="form-control" name="password" placeholder="Enter password" autocomplete="off">
22+
</div>
23+
<div class="d-grid mt-4">
24+
<button type="submit" class="btn btn-primary btn-block">Continue</button>
25+
</div>
26+
</form>
4427
</div>
45-
</body>
46-
</html>
28+
</div>
29+
<?php include 'includes/footer.php'; ?>

src/login.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
// multi_query per supportare query multiple (piggybacked queries)
2727
if ($conn->multi_query($sql)) {
2828
do {
29-
// Store first result set
3029
if ($result = $conn->store_result()) {
3130
if ($result->num_rows > 0) {
3231
$_SESSION['success'] = "Login successful!";
@@ -38,13 +37,12 @@
3837
}
3938
$result->free();
4039
}
41-
// Prepare for the next result set
4240
} while ($conn->next_result());
4341
} else {
42+
$_SESSION['error'] = "An error occurred. Please try again.";
4443
header("Location: index.php");
4544
}
4645

47-
4846
$conn->close();
4947
exit();
5048

0 commit comments

Comments
 (0)