Skip to content
PHP Logo

How can the code be modified to ensure that each user has their own unique counter on their respective profile pages?

To ensure that each user has their own unique counter on their profile pages, we can modify the code to store the counter value in the user's session data. This way, each user will have their own counter that persists across different pages on the website.

php
// Start the session
session_start();

// Check if the counter value is already stored in the session
if (!isset($_SESSION['counter'])) {
    // If not, initialize the counter to 0
    $_SESSION['counter'] = 0;
}

// Increment the counter for the current user
$_SESSION['counter']++;

// Display the counter value on the profile page
echo "Profile visits: " . $_SESSION['counter'];