l202/code.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 = $registrationError = "";
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
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 {
// 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 registration
if (!empty($enteredUsername) && !empty($enteredPassword)) {
// Hash the password before storing it
$hashedPassword = password_hash($enteredPassword, PASSWORD_DEFAULT);
// Insert user data into the database
$query = $database->prepare('INSERT INTO users (Username, password) VALUES (:Username, :password)');
$query->bindValue(':Username', $enteredUsername, SQLITE3_TEXT);
$query->bindValue(':password', $hashedPassword, SQLITE3_TEXT);
$result = $query->execute();
if ($result) {
// Redirect to a success page or perform other actions
header("Location: page2.php");
exit();
} else {
// Display registration error
$registrationError = "Username already exists. Please choose a different Username.";
}
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sign Up 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%; /* Adjusted width to accommodate the show password icon */
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>Sign Up</h2>
<form method="post" action=" echo $_SERVER['PHP_SELF']; ">
<label for="Username">Username:</label>
<input type="text" name="Username" id="Username" value=" echo isset($_POST['Username']) ? $_POST['Username'] : ''; ">
<span class="error"> 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"> echo $passwordError; </span>
<span class="error"> echo $registrationError; </span>
<input type="submit" value="Sign Up">
</form>
<form method="post" action=" 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"> echo $resetError; </span>
</p>
<p>
<a href="page2.php">Login</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