What are some best practices for securely managing and updating the status of a website using PHP and an admin interface?

When managing and updating the status of a website using PHP and an admin interface, it is important to implement proper security measures to prevent unauthorized access and ensure data integrity. One best practice is to use session management and user authentication to control access to the admin interface. Additionally, it is crucial to sanitize user input to prevent SQL injection attacks and validate data before updating the website status.

<?php
session_start();

// Check if user is logged in
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
    header('Location: login.php');
    exit;
}

// Sanitize and validate input
$status = $_POST['status'];
if (!in_array($status, ['active', 'inactive'])) {
    echo 'Invalid status';
    exit;
}

// Update website status in database
// Your database connection and query code here

echo 'Website status updated successfully';
?>