Database conn, users in progress
This commit is contained in:
22
db.php
Normal file
22
db.php
Normal 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());
|
||||
}
|
||||
}
|
||||
?>
|
||||
5
home.php
5
home.php
@@ -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>
|
||||
<div class="container">
|
||||
<h1>AI Email Generator</h1>
|
||||
|
||||
8
inc/php/auth.php
Normal file
8
inc/php/auth.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
header("Location: /landing.php");
|
||||
exit();
|
||||
}
|
||||
?>
|
||||
10
index.php
10
index.php
@@ -1,6 +1,10 @@
|
||||
<?php
|
||||
include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/header.php');
|
||||
include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/resources.php');
|
||||
|
||||
require_once($_SERVER['DOCUMENT_ROOT'] . '/db.php');
|
||||
$conn = getConnection();
|
||||
|
||||
$languages = getLanguages();
|
||||
?>
|
||||
<body>
|
||||
@@ -20,7 +24,7 @@
|
||||
<label for="language">Select Language:</label>
|
||||
<select name="language" id="language" required>
|
||||
<?php foreach ($languages as $language): ?>
|
||||
<option value="<?=$language;?>"><?=$language;?></option>
|
||||
<option value="<?= htmlspecialchars($language) ?>"><?= htmlspecialchars($language) ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
|
||||
@@ -28,4 +32,6 @@
|
||||
<button id="darkModeBtn" type="button">Toggle Dark Mode</button>
|
||||
</form>
|
||||
</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
28
landing.php
Normal 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
29
login.php
Normal 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
6
logout.php
Normal file
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
session_start();
|
||||
session_unset();
|
||||
session_destroy();
|
||||
header('Location: login.php');
|
||||
exit;
|
||||
@@ -1,6 +1,9 @@
|
||||
<?php
|
||||
require_once($_SERVER['DOCUMENT_ROOT'] . '/config.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>
|
||||
|
||||
36
register.php
Normal file
36
register.php
Normal 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>
|
||||
Reference in New Issue
Block a user