l202/page2.php
<?php
// SQLite database setup (create a file named "database.db" in the same directory)
$database = new SQLite3('database.db');
// Create a users table if it doesn't exist
$database->exec('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, Username TEXT UNIQUE, password TEXT)');
// Initialize variables for error handling
$UsernameError = $passwordError = $loginError = $resetError = "";
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if the reset button is clicked
if (isset($_POST['reset'])) {
// Handle the code for resetting accounts
// For simplicity, you can add your logic here to reset accounts based on a specific code
// For example, you might generate a random code and store it in the database
// Users would need to enter the correct code to initiate a password reset
// For demonstration, let's assume the reset code is "12345"
$resetCode = $_POST['reset_code'];
if ($resetCode === '071206') {
// Perform the reset logic here (e.g., reset passwords, clear data, etc.)
$database->exec('DELETE FROM users');
echo "Accounts reset successfully.";
} else {
$resetError = "Invalid reset code. Please try again.";
}
} else {
// Continue with login logic
// Get user input
$enteredUsername = $_POST["Username"];
$enteredPassword = $_POST["password"];
// Validate Username
if (empty($enteredUsername)) {
$UsernameError = "Please enter your Username.";
}
// Validate password
if (empty($enteredPassword)) {
$passwordError = "Please enter your password.";
}
// If Username and password are entered, attempt login
if (!empty($enteredUsername) && !empty($enteredPassword)) {
// Check if the entered credentials are valid
$query = $database->prepare('SELECT * FROM users WHERE Username = :Username');
$query->bindValue(':Username', $enteredUsername, SQLITE3_TEXT);
$result = $query->execute();
$user = $result->fetchArray();
if ($user && password_verify($enteredPassword, $user['password'])) {
// Redirect to a success page or perform other actions
session_start();
// Assuming you have a username stored in a variable
// Store the username in the session
$_SESSION['username'] = $enteredUsername;
header("Location: home_page.php");
exit();
} else {
// Display login error
$loginError = "Invalid Username or password. Please try again.";
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login with SQLite Database</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
.container {
max-width: 400px;
margin: 50px auto;
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
color: #333;
}
label {
display: block;
margin-bottom: 8px;
color: #333;
}
input {
width: 100%;
padding: 10px;
margin-bottom: 15px;
box-sizing: border-box;
}
.password-container {
position: relative;
}
.view-password {
position: absolute;
top: 40%;
right: 25px;
transform: translateY(-50%);
cursor: pointer;
color: #007bff;
font-weight: bold;
}
input[type="submit"] {
background-color: #4caf50;
color: #fff;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
.error {
color: red;
margin-top: -10px;
margin-bottom: 15px;
}
p {
text-align: center;
}
a {
text-decoration: none;
color: #007bff;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h2>Login</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="Username">Username:</label>
<input type="text" name="Username" id="Username" value="<?php echo isset($_POST['Username']) ? $_POST['Username'] : ''; ?>">
<span class="error"><?php echo $UsernameError; ?></span>
<label for="password">Password:</label>
<div class="password-container">
<input type="password" name="password" id="password">
<div class="view-password" onclick="togglePasswordVisibility()">
View
</div>
</div>
<span class="error"><?php echo $passwordError; ?></span>
<span class="error"><?php echo $loginError; ?></span>
<input type="submit" value="Login">
</form>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<hr>
<p>
<input type="submit" name="reset" value="Reset Accounts">
</p>
<p>
<label for="reset_code">Reset Code:</label>
<input type="password" name="reset_code" id="reset_code" maxlength="6" pattern="\d{1,9}">
<span class="error"><?php echo $resetError; ?></span>
</p>
<p>
<a href="page1.php">Sign Up</a>
</p>
</form>
</div>
<script>
function togglePasswordVisibility() {
var passwordInput = document.getElementById('password');
passwordInput.type = passwordInput.type === 'password' ? 'text' : 'password';
}
</script>
</body>
</html>
Resultaat
Made by Thijs Aarnoudse