strife/php/login.php

34 lines
918 B
PHP
Raw Normal View History

2024-11-10 07:13:49 +00:00
<?php
session_start();
include 'db.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'];
$password = $_POST['password'];
if (isset($conn)) {
$sql = "SELECT * FROM users WHERE username=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$user = $result->fetch_assoc();
if (password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
header("Location: home.php");
exit;
} else {
echo "Invalid credentials!";
}
} else {
echo "User not found!";
}
} else {
echo "Database connection error!";
}
}