You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
Code4Change/login.php

98 lines
2.6 KiB

<?php
$db_server = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "foodshare";
$conn = mysqli_connect($db_server, $db_user, $db_pass, $db_name);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
session_start();
$error_message = "";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'];
$password = $_POST['password'];
$query = "SELECT *, 'donor' AS user_type FROM donor WHERE email = ? AND password = ?
UNION
SELECT *, 'receiver' AS user_type FROM receiver WHERE email = ? AND password = ?";
$stmt = mysqli_prepare($conn, $query);
if ($stmt) {
mysqli_stmt_bind_param($stmt, "ssss", $email, $password, $email, $password);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if ($result && mysqli_num_rows($result) > 0) {
$user = mysqli_fetch_assoc($result);
$_SESSION['user'] = [
'name' => $user['name'],
'email' => $user['email'],
'user_type' => $user['user_type'],
];
if ($user['user_type'] === 'donor') {
header("Location: donorhome.php");
} elseif ($user['user_type'] === 'receiver') {
header("Location: receiver.php");
}
exit;
} else {
$error_message = "Invalid email or password.";
}
mysqli_stmt_close($stmt);
} else {
$error_message = "Database error: " . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login </title>
<link rel="stylesheet" href="login.css">
</head>
<body>
<div class="container">
<img src="logo.png" alt="" class="logo">
<h1>Login</h1>
<?php if (!empty($error_message)): ?>
<div class="error-message">
<p><?php echo htmlspecialchars($error_message); ?></p>
</div>
<?php endif; ?>
<form action="login.php" method="post" id="login-form">
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Enter your email" required>
<label class="iou" for="password">Password:</label>
<input type="password" id="password" name="password" placeholder="Enter your password" required>
<button type="submit">Login</button>
<p>Don't have an account? <a class="def" href="signup.php">Sign Up</a></p>
</form>
</div>
</body>
</html>