How can PHP sessions be utilized to store user-selected CSS styles for a website?

To store user-selected CSS styles for a website using PHP sessions, you can create a form where users can select their preferred styles and save them in session variables. Then, you can apply these styles dynamically by checking the session variables and outputting corresponding CSS code.

<?php
session_start();

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Save user-selected styles in session variables
    $_SESSION['text_color'] = $_POST['text_color'];
    $_SESSION['background_color'] = $_POST['background_color'];
}

// Apply user-selected styles
echo '<style>';
if (isset($_SESSION['text_color'])) {
    echo 'body { color: ' . $_SESSION['text_color'] . '; }';
}
if (isset($_SESSION['background_color'])) {
    echo 'body { background-color: ' . $_SESSION['background_color'] . '; }';
}
echo '</style>';
?>

<form method="post">
    <label for="text_color">Text Color:</label>
    <input type="color" id="text_color" name="text_color">

    <label for="background_color">Background Color:</label>
    <input type="color" id="background_color" name="background_color">

    <input type="submit" value="Save Styles">
</form>