Create a simple login page and put sql code and php file with different file
- User
Create a simple login page and put sql code and php file with different file
Assistant
Create a simple login page and put sql code and php file with different file
Here is a minimal, educational example that shows:
login.html)login.php)schema.sql) that creates the database, table, and one sample user⚠️ Security warning:
This code is intentionally basic. In production you must:
• Use prepared statements (PDO / mysqli with bound parameters)
• Hash passwords (e.g., password_hash)
• Escape all output
• Use HTTPS, CSRF tokens, rate-limiting, etc.
────────────────────────────
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Login</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
label { display: block; margin: 8px 0 4px; }
input[type=text], input[type=password] { width: 220px; padding: 6px; }
button { margin-top: 12px; padding: 8px 18px; }
</style>
</head>
<body>
<h2>Simple Login</h2>
<form action="login.php" method="post">
<label>Username</label>
<input type="text" name="username" required>
<label>Password</label>
<input type="password" name="password" required>
<button type="submit">Log in</button>
</form>
</body>
</html>
──────────────────────────── 2. schema.sql
-- Create database and user table
CREATE DATABASE IF NOT EXISTS demo_app;
USE demo_app;
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
);
-- Insert one sample user: username = admin password = secret
INSERT INTO users (username, password)
VALUES ('admin', 'secret');
Run this SQL script in MySQL (CLI, phpMyAdmin, or any client).
──────────────────────────── 3. login.php
<?php
/* Very basic example – NOT production-ready */
$host = 'localhost';
$db = 'demo_app';
$user = 'root'; // change if needed
$pass = ''; // change if needed
$charset = 'utf8mb4';
/* 1. Connect to MySQL */
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (PDOException $e) {
exit('Database connection failed: ' . $e->getMessage());
}
/* 2. Grab POST data */
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
/* 3. Look up user */
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = ?');
$stmt->execute([$username]);
$userRow = $stmt->fetch();
/* 4. Very naive check (plain-text password) */
if ($userRow && $userRow['password'] === $password) {
echo "Welcome, " . htmlspecialchars($username) . "!";
} else {
echo "Invalid credentials.";
}
?>
──────────────────────────── How to test quickly
login.html and login.php in your web root (e.g., htdocs/ or www/).schema.sql into MySQL.http://localhost/login.html, enter admin / secret, and submit.That’s it—three separate files for a bare-bones login flow.