Database conn, users in progress

This commit is contained in:
Dr3amFury
2025-07-17 01:05:46 +02:00
parent d274fa033b
commit ef76dc973a
9 changed files with 149 additions and 8 deletions

22
db.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
function getConnection() {
$host = getenv('DB_HOST') ?: 'localhost';
$db = getenv('DB_NAME') ?: 'your_db_name';
$user = getenv('DB_USER') ?: 'your_username';
$pass = getenv('DB_PASS') ?: 'your_password';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
return new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
}
?>

View File

@@ -1,4 +1,7 @@
<?php include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/header.php');?> <?php include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/header.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/db.php');
$conn = getConnection();?>
<body> <body>
<div class="container"> <div class="container">
<h1>AI Email Generator</h1> <h1>AI Email Generator</h1>

8
inc/php/auth.php Normal file
View File

@@ -0,0 +1,8 @@
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: /landing.php");
exit();
}
?>

View File

@@ -1,7 +1,11 @@
<?php <?php
include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/header.php'); include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/header.php');
include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/resources.php'); include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/resources.php');
$languages = getLanguages();
require_once($_SERVER['DOCUMENT_ROOT'] . '/db.php');
$conn = getConnection();
$languages = getLanguages();
?> ?>
<body> <body>
<div class="container"> <div class="container">
@@ -19,13 +23,15 @@
<label for="language">Select Language:</label> <label for="language">Select Language:</label>
<select name="language" id="language" required> <select name="language" id="language" required>
<?php foreach($languages as $language) :?> <?php foreach ($languages as $language): ?>
<option value="<?=$language;?>"><?=$language;?></option> <option value="<?= htmlspecialchars($language) ?>"><?= htmlspecialchars($language) ?></option>
<?php endforeach;?> <?php endforeach; ?>
</select> </select>
<button type="submit">Generate Email</button> <button type="submit">Generate Email</button>
<button id="darkModeBtn" type="button">Toggle Dark Mode</button> <button id="darkModeBtn" type="button">Toggle Dark Mode</button>
</form> </form>
</div> </div>
<?php include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/footer.php');
<script src="/inc/js/theme.js"></script>
<?php include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/footer.php'); ?>

28
landing.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
session_start();
if (isset($_SESSION['user_id'])) {
// Already logged in, redirect to email generator
header("Location: /home.php");
exit();
}
include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/header.php');
?>
<body>
<div class="container" style="text-align: center;">
<h1>Welcome to AI Email Generator</h1>
<p style="margin-bottom: 20px;">Craft professional emails in seconds. Please login or register to get started.</p>
<div style="display: flex; justify-content: center; gap: 20px;">
<a href="/inc/php/login.php"><button style="padding: 10px 20px;">Login</button></a>
<a href="/inc/php/register.php"><button style="padding: 10px 20px;">Register</button></a>
</div>
<div style="margin-top: 30px;">
<button id="darkModeBtn">Toggle Dark Mode</button>
</div>
</div>
<script src="/inc/js/theme.js"></script>
<?php include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/footer.php'); ?>

29
login.php Normal file
View File

@@ -0,0 +1,29 @@
<?php
session_start();
require_once 'db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = trim($_POST['email']);
$password = $_POST['password'];
$conn = getConnection();
$stmt = $conn->prepare("SELECT id, username, password FROM Users WHERE email = ?");
$stmt->execute([$email]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
header('Location: home.php');
exit;
} else {
echo "Invalid credentials.";
}
}
?>
<form method="POST">
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Login</button>
</form>

6
logout.php Normal file
View File

@@ -0,0 +1,6 @@
<?php
session_start();
session_unset();
session_destroy();
header('Location: login.php');
exit;

View File

@@ -1,6 +1,9 @@
<?php <?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/config.php'); require_once($_SERVER['DOCUMENT_ROOT'] . '/config.php');
include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/header.php'); include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/header.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/db.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/inc/php/auth.php');
$conn = getConnection();
?> ?>
<body> <body>

36
register.php Normal file
View File

@@ -0,0 +1,36 @@
<?php
require_once 'db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$password = $_POST['password'];
if (!$username || !$email || !$password) {
die('All fields are required.');
}
$conn = getConnection();
// Check if user exists
$stmt = $conn->prepare("SELECT id FROM Users WHERE email = ?");
$stmt->execute([$email]);
if ($stmt->fetch()) {
die('Email already registered.');
}
$hashed = password_hash($password, PASSWORD_DEFAULT);
$stmt = $conn->prepare("INSERT INTO Users (username, email, password) VALUES (?, ?, ?)");
$stmt->execute([$username, $email, $hashed]);
header('Location: login.php');
exit;
}
?>
<form method="POST">
<input type="text" name="username" placeholder="Username" required>
<input type="email" name="email" placeholder="Email" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Register</button>
</form>