CoolFace
Apppublic

Skullwanderer/cohort-manager

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
authenticate.php53 linesDownload Raw Back to root
1```php2<?php3session_start();4 5// Database connection6$servername = "localhost";7$db_username = "root";8$db_password = "";9$dbname = "markpsystem";10 11$conn = new mysqli($servername, $db_username, $db_password, $dbname);12 13// Check connection14if ($conn->connect_error) {15    die("Connection failed: " . $conn->connect_error);16}17 18if ($_SERVER["REQUEST_METHOD"] == "POST") {19    $input_username = $_POST['username'];20    $input_password = $_POST['password'];21    22    // Prepare statement to prevent SQL injection23    $stmt = $conn->prepare("SELECT username, password, role FROM users WHERE username = ?");24    $stmt->bind_param("s", $input_username);25    $stmt->execute();26    $result = $stmt->get_result();27    28    if ($result->num_rows == 1) {29        $user = $result->fetch_assoc();30        31        // Verify password32        if (password_verify($input_password, $user['password'])) {33            // Set session variables34            $_SESSION['loggedin'] = true;35            $_SESSION['username'] = $user['username'];36            $_SESSION['role'] = $user['role'];37            // Redirect to dashboard38            header("Location: index.php");39            exit;40        }41    }42    // Invalid credentials43    header("Location: login.php?error=invalid");44    exit;45} else {46    // If not POST request, redirect to login47    header("Location: login.php");48    exit;49}50 51$conn->close();52?>53```