@ 2024-07-21T10:08:18Z <?php
require 'vendor/autoload.php';
use Dotenv\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
$host = $_ENV['DB_HOST'];
$name = $_ENV['DB_NAME'];
$db = pg_connect("host=$host dbname=$name");
if (!$db) {
echo "Error connection to database";
}
echo "Connected to $name successfully \n";
$sql = file_get_contents('init_db.sql');
pg_query($db, $sql);
?>
This is the .sql file that gets loaded and populates the database
-- init_db.sql
DROP TABLE IF EXISTS users;
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
surname VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
phone VARCHAR(20) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Insert initial data
INSERT INTO users (name, surname, password, email, phone) VALUES
('admin', 'admin', '$2y$10$oEpwSflquB9WyTSVkHj8HupkxygaepInmricYtoNTTAGh23iPOK3.%', 'admin@example.com', '123123123'),
('John', 'Doe', '$2y$10$oEpwSflquB9WyTSVkHj8HupkxygaepInmricYtoNTTAGh23iPOK3.%', 'user@example.com', '123123123');
The password hash was gotten by using "password_hash('123', PASSWORD_DEFAULT);" and copying the output
// The login page that reads the DB password and checks it
<?php
require_once '../vendor/autoload.php';
use Dotenv\Dotenv;
// NOTE: Maybe there is a better way to do this but I don't know about it.
$dotenv = Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->load();
$dbName = $_ENV['DB_NAME'];
session_start();
if (isset($_POST) && isset($_POST['email']) && isset($_POST['password'])) {
$email = $_POST['email'];
$db = pg_connect("dbname=$dbName");
if (!$db) {
die('Connection to DB failed: ' . pg_last_error());
}
$res = pg_query_params($db, "SELECT u.password, u.id, u.email FROM users u WHERE u.email=$1", [$email]);
if (!$res) {
die('Query failed: ' . pg_last_error());
}
$row = pg_fetch_assoc($res);
if ($row) {
$password = $_POST['password'];
if (password_verify($password, $row['password'])) {
$_SESSION['loggedin'] = true;
$_SESSION['id'] = $row['id'];
$_SESSION['email'] = $row['email'];
header('Location: dashboard.php');
} else {
echo 'Wrong password';
}
} else {
echo 'User not found';
}
pg_free_result($res);
pg_close($db);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
</head>
<body>
<form action="login.php" method="POST">
<label for="email">E-mail:</label>
<input type="email" id="email" name="email">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<button type="submit">login</button>
</form>
</body>
</html>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.3.9 0.040 0.006 26.77 8.3.8 0.035 0.003 26.77 8.3.7 0.023 0.010 26.77 8.3.6 0.037 0.009 26.77 8.3.5 0.036 0.011 26.77 8.3.4 0.036 0.010 26.77 8.3.3 0.030 0.007 26.77 8.3.2 0.027 0.013 26.77 8.3.1 0.036 0.009 26.77 8.3.0 0.033 0.010 26.77 8.2.21 0.019 0.019 26.77 8.2.20 0.031 0.006 26.77 8.2.19 0.027 0.005 26.77 8.2.18 0.028 0.003 26.77 8.2.17 0.030 0.013 26.77 8.2.16 0.034 0.008 26.77 8.2.15 0.032 0.011 26.77 8.2.14 0.033 0.011 26.77 8.2.13 0.038 0.003 26.77 8.2.12 0.032 0.010 26.77 8.2.11 0.035 0.010 26.77 8.2.10 0.043 0.000 26.77 8.2.9 0.042 0.000 26.77 8.2.8 0.029 0.013 26.77 8.2.7 0.030 0.014 26.77 8.2.6 0.043 0.000 26.77 8.2.5 0.035 0.007 26.77 8.2.4 0.037 0.003 26.77 8.2.3 0.026 0.013 26.77 8.2.2 0.032 0.003 26.77 8.2.1 0.023 0.008 26.77 8.2.0 0.022 0.003 26.77 8.1.29 0.018 0.004 26.77 8.1.28 0.021 0.007 26.77 8.1.27 0.035 0.006 26.77 8.1.26 0.027 0.015 26.77 8.1.25 0.028 0.012 26.77 8.1.24 0.037 0.004 26.77 8.1.23 0.035 0.003 26.77 8.1.22 0.028 0.006 26.77 8.1.21 0.030 0.008 26.77 8.1.20 0.028 0.010 26.77 8.1.19 0.026 0.011 26.77 8.1.18 0.021 0.016 26.77 8.1.17 0.029 0.008 26.77 8.1.16 0.026 0.011 26.77 8.1.15 0.033 0.004 26.77 8.1.14 0.019 0.015 26.77 8.1.13 0.024 0.007 26.77 8.1.12 0.032 0.007 26.77 8.1.11 0.028 0.014 26.77 8.1.10 0.030 0.009 26.77 8.1.9 0.031 0.007 26.77 8.1.8 0.033 0.004 26.77 8.1.7 0.025 0.013 26.77 8.1.6 0.035 0.005 26.77 8.1.5 0.040 0.000 26.77 8.1.4 0.024 0.018 26.77 8.1.3 0.029 0.013 26.77 8.1.2 0.035 0.006 26.77 8.1.1 0.023 0.019 26.77 8.1.0 0.034 0.007 26.77
preferences:dark mode live preview
138.97 ms | 1421 KiB | 7 Q