Current Location: Home> Latest Articles> Practical PHP: Easily Implement User Access Logging and Behavior Analysis

Practical PHP: Easily Implement User Access Logging and Behavior Analysis

gitbox 2025-07-17

Overview

In website development, recording user behavior is a crucial step to enhance user experience and optimize performance. By analyzing user access logs and behavior data, you can gain insights into user needs and habits, guiding site improvements. This article demonstrates how to use PHP to implement user access logging and behavior analysis.

Implementing User Access Logs

Understanding User Access Logs

User access logs refer to information automatically recorded by the server when users visit a website, including access time, IP address, and visited pages. Analyzing these logs helps understand user traffic and supports site performance optimization.

Recording User Access Logs

The following example shows how to use PHP to log user access information. The example file is named log.php:

<?php
$ip = $_SERVER['REMOTE_ADDR'];  // Get user IP
$referer = $_SERVER['HTTP_REFERER'] ?? '-';  // Get referring page or use "-" if none
$page = $_SERVER['REQUEST_URI'];  // Current accessed page
$time = date('Y-m-d H:i:s', time());  // Access time
// Compose log entry
$log = "{$ip} {$referer} {$page} {$time}\n";
// Append to log file
file_put_contents('access.log', $log, FILE_APPEND);
?>

This code collects the user's IP, referrer, accessed path, and time, then appends the data to a log file. Including this script in your site pages will enable automatic access logging.

Implementing User Behavior Analysis

Understanding User Behavior Analysis

User behavior analysis processes access log data to analyze visit counts, paths, time periods, and other metrics. This helps site operators discover user preferences and patterns to improve user experience and conversion rates. Key aspects include:

  • Statistics of visit counts and paths
  • Analysis of visit time periods and depth
  • Identification of popular pages and traffic sources
  • Analysis of user geography and device types

Implementing Behavior Analysis

Using the access logs from before, the following sample code (in analysis.php) demonstrates data aggregation:

<?php
// Read log file
$log = file_get_contents('access.log');
$logs = explode("\n", $log);
$pages = [];
// Count visits per page
foreach ($logs as $log) {
    if ($log) {
        $items = explode(" ", $log);
        $page = $items[2];
        if (!isset($pages[$page])) {
            $pages[$page] = 0;
        }
        $pages[$page]++;
    }
}
arsort($pages); // Sort descending
// Output page visit counts
foreach ($pages as $page => $count) {
    echo "Page {$page} was visited {$count} times.<br>";
}

// Count visits per hour
$hours = array_fill(0, 24, 0);
foreach ($logs as $log) {
    if ($log) {
        $items = explode(" ", $log);
        $timeStr = $items[3];
        $hour = substr($timeStr, 11, 2);
        $hours[(int)$hour]++;
    }
}
// Output visit counts by hour
foreach ($hours as $hour => $count) {
    echo "Hour {$hour} has {$count} visits.<br>";
}
?>

This code first counts visits per page and then counts visits per hour, providing insights into user behavior and traffic patterns that can guide website optimization.

Summary

Logging user access and performing behavior analysis are fundamental for website improvement. Using simple PHP scripts, developers can collect valuable data and analyze it to better understand user needs, thereby enhancing overall website performance and user experience.