skapade registrering, logga in och logga ut. ändrade lite i auth.php
This commit is contained in:
43
inc/php/login.php
Normal file
43
inc/php/login.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once($_SERVER['DOCUMENT_ROOT'] . '/db.php');
|
||||
|
||||
$errors = [];
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$user = trim($_POST['user'] ?? '');
|
||||
$password = $_POST['password'] ?? '';
|
||||
|
||||
if (empty($user) || empty($password)) {
|
||||
$errors[] = "All fields are required.";
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$conn = getConnection();
|
||||
$stmt = $conn->prepare("SELECT id, password, uniqueid FROM users WHERE email = :user OR username = :user");
|
||||
$stmt->execute(['user' => $user]);
|
||||
$result = $stmt->fetch();
|
||||
|
||||
if ($result && password_verify($password, $result['password'])) {
|
||||
$_SESSION['user_id'] = $result['id'];
|
||||
$_SESSION['uniqueid'] = $result['uniqueid'];
|
||||
header("Location: /home.php");
|
||||
exit();
|
||||
} else {
|
||||
$errors[] = "Invalid credentials.";
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
<!-- Basic form UI -->
|
||||
<h2>Login</h2>
|
||||
<form method="POST">
|
||||
<input name="user" placeholder="Username or Email" required><br>
|
||||
<input name="password" type="password" placeholder="Password" required><br>
|
||||
<button type="submit">Login</button>
|
||||
</form>
|
||||
|
||||
<?php if (!empty($errors)): ?>
|
||||
<ul><?php foreach ($errors as $e) echo "<li>$e</li>"; ?></ul>
|
||||
<?php endif; ?>
|
||||
Reference in New Issue
Block a user