In PHP, how can the accessed page, duration of visit, and other relevant metrics be tracked and analyzed without relying on JavaScript?

Tracking user activity and metrics in PHP without relying on JavaScript can be achieved by using server-side techniques such as logging page accesses and durations in a database or file. One way to do this is by creating a PHP script that records the page access time, duration of visit, and any other relevant metrics when a user accesses a page on the website.

// Start session to track user activity
session_start();

// Record page access time
$page_access_time = time();

// Calculate duration of visit
if(isset($_SESSION['last_page_access_time'])){
    $duration_of_visit = $page_access_time - $_SESSION['last_page_access_time'];
} else {
    $duration_of_visit = 0;
}

// Log page access and duration in a database or file
// For example, save to a text file
$log_entry = "Page accessed at: " . date('Y-m-d H:i:s', $page_access_time) . " | Duration of visit: " . $duration_of_visit . " seconds\n";
file_put_contents('user_activity.log', $log_entry, FILE_APPEND);

// Update last page access time
$_SESSION['last_page_access_time'] = $page_access_time;