database
This commit is contained in:
parent
391fd84e69
commit
11336c0d33
62
dashboard.php
Normal file
62
dashboard.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
|
||||
header("location: login.php");
|
||||
exit;
|
||||
}
|
||||
|
||||
require_once 'db/config.php';
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Dashboard - E-Waste Reclaimer</title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
||||
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<div class="container">
|
||||
<div class="logo">
|
||||
<h1><a href="index.php">E-Waste Reclaimer</a></h1>
|
||||
</div>
|
||||
<nav>
|
||||
<ul>
|
||||
<li><a href="index.php">Find a Center</a></li>
|
||||
<li><a href="dashboard.php">Dashboard</a></li>
|
||||
<li><a href="logout.php">Logout</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main class="container page-main">
|
||||
<section>
|
||||
<h2>Welcome, <?php echo htmlspecialchars($_SESSION['user_name']); ?>!</h2>
|
||||
<p>This is your dashboard. From here you will be able to submit e-waste, track your points, and see your submission history.</p>
|
||||
|
||||
<div class="dashboard-stats">
|
||||
<div class="stat-card">
|
||||
<h3>Your Points</h3>
|
||||
<p class="points-total">0</p>
|
||||
<p>Keep recycling to earn more!</p>
|
||||
</div>
|
||||
<div class="stat-card">
|
||||
<h3>Submissions</h3>
|
||||
<p class="submissions-total">0</p>
|
||||
<a href="#" class="btn btn-secondary">Submit New E-Waste</a>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<div class="container">
|
||||
<p>© <?php echo date("Y"); ?> E-Waste Reclaimer. All Rights Reserved.</p>
|
||||
</div>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
@ -6,18 +6,41 @@ define('DB_USER', 'app_31009');
|
||||
define('DB_PASS', '2c66b530-2a65-423a-a106-6760b49ad1a2');
|
||||
|
||||
function db() {
|
||||
static $pdo;
|
||||
if (!$pdo) {
|
||||
$pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS, [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
]);
|
||||
$pdo->exec('CREATE TABLE IF NOT EXISTS centers (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
address VARCHAR(255) NOT NULL,
|
||||
contact VARCHAR(255) NOT NULL
|
||||
);');
|
||||
}
|
||||
return $pdo;
|
||||
static $pdo;
|
||||
if (!$pdo) {
|
||||
try {
|
||||
// Connect without specifying a database
|
||||
$pdo = new PDO('mysql:host='.DB_HOST.';charset=utf8mb4', DB_USER, DB_PASS, [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
]);
|
||||
|
||||
// Create the database if it doesn't exist
|
||||
$pdo->exec('CREATE DATABASE IF NOT EXISTS '.DB_NAME);
|
||||
// Select the database
|
||||
$pdo->exec('USE '.DB_NAME);
|
||||
|
||||
$pdo->exec('CREATE TABLE IF NOT EXISTS centers (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
address VARCHAR(255) NOT NULL,
|
||||
contact VARCHAR(255) NOT NULL
|
||||
);');
|
||||
|
||||
$pdo->exec("CREATE TABLE IF NOT EXISTS users (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
email VARCHAR(255) NOT NULL UNIQUE,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
role ENUM('guest', 'user', 'center_staff', 'admin') NOT NULL DEFAULT 'user',
|
||||
points INT DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);");
|
||||
} catch (PDOException $e) {
|
||||
error_log('Database setup failed: ' . $e->getMessage());
|
||||
// You could display a generic error page here instead of dying
|
||||
die('Database setup failed. Please check the logs.');
|
||||
}
|
||||
}
|
||||
return $pdo;
|
||||
}
|
||||
|
||||
133
index.php
133
index.php
@ -1,84 +1,93 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once 'db/config.php';
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>E-Waste Reclaimer</title>
|
||||
<meta name="description" content="Built with Flatlogic Generator">
|
||||
<meta name="keywords" content="e-waste, recycling, sustainability, electronics disposal, green technology, responsible consumption, eco-friendly, circular economy, Built with Flatlogic Generator">
|
||||
<meta property="og:title" content="E-Waste Reclaimer">
|
||||
<meta property="og:description" content="Built with Flatlogic Generator">
|
||||
<meta property="og:image" content="">
|
||||
<meta name="twitter:card" content="summary_large_image">
|
||||
<meta name="twitter:image" content="">
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
|
||||
<link rel="stylesheet" href="assets/css/custom.css">
|
||||
<meta name="description" content="A platform to help you recycle your e-waste responsibly.">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
||||
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||||
</head>
|
||||
<body class="bg-light">
|
||||
|
||||
<header class="bg-white shadow-sm">
|
||||
<div class="container d-flex justify-content-between align-items-center py-3">
|
||||
<a href="/" class="h4 text-success text-decoration-none">
|
||||
<i class="fas fa-recycle me-2"></i>E-Waste Reclaimer
|
||||
</a>
|
||||
<div>
|
||||
<a href="#" class="btn btn-outline-secondary me-2">Login</a>
|
||||
<a href="#" class="btn btn-success">Register</a>
|
||||
<body>
|
||||
<header>
|
||||
<div class="container">
|
||||
<div class="logo">
|
||||
<h1><a href="index.php">E-Waste Reclaimer</a></h1>
|
||||
</div>
|
||||
<nav>
|
||||
<ul>
|
||||
<li><a href="index.php">Find a Center</a></li>
|
||||
<?php if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true): ?>
|
||||
<li><a href="dashboard.php">Dashboard</a></li>
|
||||
<li><a href="logout.php">Logout</a></li>
|
||||
<?php else: ?>
|
||||
<li><a href="login.php">Login</a></li>
|
||||
<li><a href="register.php">Register</a></li>
|
||||
<?php endif; ?>
|
||||
</ul>
|
||||
</nav>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main class="container my-5">
|
||||
<div class="text-center mb-5">
|
||||
<h1 class="display-4">Find a Recycling Center</h1>
|
||||
<p class="lead">Help the planet by responsibly disposing of your electronic waste.</p>
|
||||
</div>
|
||||
<main class="container page-main">
|
||||
<section class="hero">
|
||||
<h2>Welcome to E-Waste Reclaimer</h2>
|
||||
<p>Your partner in responsible electronics recycling. Find a center near you and help us build a sustainable future.</p>
|
||||
<a href="#centers-list" class="btn btn-primary">Find a Center</a>
|
||||
</section>
|
||||
|
||||
<div class="row">
|
||||
<section id="centers-list" class="centers-section">
|
||||
<h2>Recycling Centers</h2>
|
||||
<div class="card-container">
|
||||
<?php
|
||||
$centers = [
|
||||
[
|
||||
'name' => 'GreenTech Recyclers',
|
||||
'address' => '123 Eco Lane, Green City, 12345',
|
||||
'contact' => 'contact@greentech.com'
|
||||
],
|
||||
[
|
||||
'name' => 'Circuit Savers',
|
||||
'address' => '456 Recycle Ave, Tech Town, 67890',
|
||||
'contact' => 'info@circuitsavers.com'
|
||||
],
|
||||
[
|
||||
'name' => 'Eco-Warriors',
|
||||
'address' => '789 Planet Blvd, Nature Village, 11223',
|
||||
'contact' => 'support@ecowarriors.org'
|
||||
]
|
||||
];
|
||||
try {
|
||||
$pdo = db();
|
||||
|
||||
foreach ($centers as $center): ?>
|
||||
<div class="col-md-4 mb-4">
|
||||
<div class="card h-100 shadow-sm border-0">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title text-success"><?php echo htmlspecialchars($center['name']); ?></h5>
|
||||
<p class="card-text">
|
||||
<i class="fas fa-map-marker-alt me-2 text-secondary"></i><?php echo htmlspecialchars($center['address']); ?>
|
||||
</p>
|
||||
<p class="card-text">
|
||||
<i class="fas fa-envelope me-2 text-secondary"></i><?php echo htmlspecialchars($center['contact']); ?>
|
||||
</p>
|
||||
</div>
|
||||
$stmt = $pdo->query("SELECT COUNT(*) FROM centers");
|
||||
if ($stmt->fetchColumn() == 0) {
|
||||
$sample_centers = [
|
||||
['name' => 'GreenTech Recyclers', 'address' => '123 Eco Lane, Green City', 'contact' => 'contact@greentech.com'],
|
||||
['name' => 'Circuit Savers', 'address' => '456 Recycle Ave, Tech Town', 'contact' => 'info@circuitsavers.com'],
|
||||
['name' => 'Eco-Warriors', 'address' => '789 Planet Blvd, Nature Village', 'contact' => 'support@ecowarriors.org']
|
||||
];
|
||||
|
||||
$insert_stmt = $pdo->prepare("INSERT INTO centers (name, address, contact) VALUES (:name, :address, :contact)");
|
||||
foreach ($sample_centers as $center) {
|
||||
$insert_stmt->execute($center);
|
||||
}
|
||||
}
|
||||
|
||||
$centers = $pdo->query("SELECT * FROM centers ORDER BY name")->fetchAll();
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$centers = [];
|
||||
error_log("Database error: " . $e->getMessage());
|
||||
}
|
||||
|
||||
if (!empty($centers)):
|
||||
foreach ($centers as $center): ?>
|
||||
<div class="card">
|
||||
<div class="card-icon"><i class="fas fa-recycle"></i></div>
|
||||
<h3><?php echo htmlspecialchars($center['name']); ?></h3>
|
||||
<p><i class="fas fa-map-marker-alt"></i> <?php echo htmlspecialchars($center['address']); ?></p>
|
||||
<p><i class="fas fa-envelope"></i> <?php echo htmlspecialchars($center['contact']); ?></p>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endforeach;
|
||||
else: ?>
|
||||
<div class="alert alert-info">No recycling centers found. Please check back later.</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<footer class="bg-white text-center py-4 mt-5">
|
||||
<footer>
|
||||
<div class="container">
|
||||
<p class="mb-0">© <?php echo date("Y"); ?> E-Waste Reclaimer. All Rights Reserved.</p>
|
||||
<p>© <?php echo date("Y"); ?> E-Waste Reclaimer. All Rights Reserved.</p>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
112
login.php
Normal file
112
login.php
Normal file
@ -0,0 +1,112 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once 'db/config.php';
|
||||
|
||||
$errors = [];
|
||||
$success_msg = '';
|
||||
|
||||
if (isset($_GET['registered']) && $_GET['registered'] == 'true') {
|
||||
$success_msg = 'Registration successful! Please log in.';
|
||||
}
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
$email = trim($_POST['email']);
|
||||
$password = $_POST['password'];
|
||||
|
||||
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
$errors[] = 'A valid email is required.';
|
||||
}
|
||||
if (empty($password)) {
|
||||
$errors[] = 'Password is required.';
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
try {
|
||||
$pdo = db();
|
||||
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
|
||||
$stmt->execute([$email]);
|
||||
$user = $stmt->fetch();
|
||||
|
||||
if ($user && password_verify($password, $user['password'])) {
|
||||
$_SESSION['loggedin'] = true;
|
||||
$_SESSION['user_id'] = $user['id'];
|
||||
$_SESSION['user_name'] = $user['name'];
|
||||
$_SESSION['user_role'] = $user['role'];
|
||||
header("Location: dashboard.php");
|
||||
exit;
|
||||
} else {
|
||||
$errors[] = 'Invalid email or password.';
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
$errors[] = "Database error: " . $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Login - E-Waste Reclaimer</title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
||||
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<div class="container">
|
||||
<div class="logo">
|
||||
<h1><a href="index.php">E-Waste Reclaimer</a></h1>
|
||||
</div>
|
||||
<nav>
|
||||
<ul>
|
||||
<li><a href="index.php">Find a Center</a></li>
|
||||
<li><a href="login.php">Login</a></li>
|
||||
<li><a href="register.php">Register</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main class="container page-main">
|
||||
<section class="form-section">
|
||||
<h2>Login to Your Account</h2>
|
||||
<p>Access your dashboard to track your e-waste submissions.</p>
|
||||
|
||||
<?php if (!empty($errors)): ?>
|
||||
<div class="alert alert-danger">
|
||||
<?php foreach ($errors as $error): ?>
|
||||
<p><?php echo htmlspecialchars($error); ?></p>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($success_msg): ?>
|
||||
<div class="alert alert-success">
|
||||
<p><?php echo htmlspecialchars($success_msg); ?></p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<form action="login.php" method="post">
|
||||
<div class="form-group">
|
||||
<label for="email">Email Address</label>
|
||||
<input type="email" id="email" name="email" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="password">Password</label>
|
||||
<input type="password" id="password" name="password" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button type="submit" class="btn btn-primary">Login</button>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<div class="container">
|
||||
<p>© <?php echo date("Y"); ?> E-Waste Reclaimer. All Rights Reserved.</p>
|
||||
</div>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
21
logout.php
Normal file
21
logout.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
// Unset all of the session variables
|
||||
$_SESSION = [];
|
||||
|
||||
// Destroy the session
|
||||
if (ini_get("session.use_cookies")) {
|
||||
$params = session_get_cookie_params();
|
||||
setcookie(session_name(), '', time() - 42000,
|
||||
$params["path"], $params["domain"],
|
||||
$params["secure"], $params["httponly"]
|
||||
);
|
||||
}
|
||||
|
||||
session_destroy();
|
||||
|
||||
// Redirect to home page
|
||||
header("Location: index.php");
|
||||
exit;
|
||||
?>
|
||||
107
register.php
Normal file
107
register.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once 'db/config.php';
|
||||
|
||||
$errors = [];
|
||||
$success = '';
|
||||
|
||||
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||
$name = trim($_POST['name']);
|
||||
$email = trim($_POST['email']);
|
||||
$password = $_POST['password'];
|
||||
|
||||
if (empty($name)) {
|
||||
$errors[] = 'Name is required.';
|
||||
}
|
||||
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
$errors[] = 'A valid email is required.';
|
||||
}
|
||||
if (empty($password) || strlen($password) < 6) {
|
||||
$errors[] = 'Password must be at least 6 characters long.';
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
try {
|
||||
$pdo = db();
|
||||
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
|
||||
$stmt->execute([$email]);
|
||||
if ($stmt->fetch()) {
|
||||
$errors[] = 'Email address is already registered.';
|
||||
} else {
|
||||
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
||||
$stmt = $pdo->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
|
||||
$stmt->execute([$name, $email, $hashed_password]);
|
||||
header("Location: login.php?registered=true");
|
||||
exit;
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
$errors[] = "Database error: " . $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Register - E-Waste Reclaimer</title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
||||
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<div class="container">
|
||||
<div class="logo">
|
||||
<h1><a href="index.php">E-Waste Reclaimer</a></h1>
|
||||
</div>
|
||||
<nav>
|
||||
<ul>
|
||||
<li><a href="index.php">Find a Center</a></li>
|
||||
<li><a href="login.php">Login</a></li>
|
||||
<li><a href="register.php">Register</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main class="container page-main">
|
||||
<section class="form-section">
|
||||
<h2>Create an Account</h2>
|
||||
<p>Join our community to start recycling and earning points.</p>
|
||||
|
||||
<?php if (!empty($errors)): ?>
|
||||
<div class="alert alert-danger">
|
||||
<?php foreach ($errors as $error): ?>
|
||||
<p><?php echo htmlspecialchars($error); ?></p>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<form action="register.php" method="post">
|
||||
<div class="form-group">
|
||||
<label for="name">Full Name</label>
|
||||
<input type="text" id="name" name="name" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="email">Email Address</label>
|
||||
<input type="email" id="email" name="email" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="password">Password</label>
|
||||
<input type="password" id="password" name="password" required minlength="6">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button type="submit" class="btn btn-primary">Register</button>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<div class="container">
|
||||
<p>© <?php echo date("Y"); ?> E-Waste Reclaimer. All Rights Reserved.</p>
|
||||
</div>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
x
Reference in New Issue
Block a user