Просмотр поста

.
ДоХтор

В общем не смог я победить повторы пар юзер-айпи. Если читать из одного файла, то можно было бы, а изо всех - у меня не вышло.

код логера (+/-)
<?php

session_start();

$us_sid = session_id();
$textfile = time() . '.txt';
$time_is_up = time() - 300;
$numb_files = 10;

$content = null;
$date = date('j.m.Y - H:i:s');
$user_agent = trim(htmlspecialchars($_SERVER['HTTP_USER_AGENT']));
$user_agent = str_replace('|', '', $user_agent);
$ip = $_SERVER['REMOTE_ADDR'];
$ref = filter_input(INPUT_SERVER, 'HTTP_REFERER', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$ref = !empty($ref) ? '<a href="'. $ref .'">'. $ref .'</a>' : null;
$filing = $us_sid .'|'. $date .'|'. $user_agent .'|'. $ip .'|'. $ref . PHP_EOL;

if (sizeof(glob('*.txt')) == 0) file_put_contents($textfile, $filing);

$array_names = glob('*.txt');
$all_strings = sizeof($array_names);

if ($all_strings > 0 && $all_strings < $numb_files) {
    $basename = (int)basename(end($array_names), '.txt');
    
    if ($time_is_up > $basename) file_put_contents($textfile, $filing);
    
    $array_names = glob('*.txt');
    $end_file = file_get_contents(end($array_names));
    $temp = array();
    
    foreach (explode(PHP_EOL, $end_file) as $string) {
        $array = explode('|', $string);
        $temp[] = $array[0];
    }
    
    if (!in_array($us_sid, $temp)) {
        file_put_contents(end($array_names), $filing, FILE_APPEND | LOCK_EX);
    }
}

if (sizeof(glob('*.txt')) >= $numb_files) {
    $array_names = array_reverse(glob('*.txt'));
    $arr_fnames = array();
    $arr_contents = array();
    
    for ($i = 0; $i < $numb_files; $i++) {
        $arr_fnames[] = $array_names[$i];
        $arr_contents[] = file_get_contents($array_names[$i]);
    }

    foreach (glob('*.txt') as $filename) unlink($filename);

    $basename = (int)basename($arr_fnames[0], '.txt');
    if ($time_is_up > $basename) {
        file_put_contents($textfile, $filing);
        for ($i = 0; $i < ($numb_files - 1); $i++) {
            file_put_contents($arr_fnames[$i], $arr_contents[$i]);
        }
    } else {
        for ($i = 0; $i < $numb_files; $i++) {
            file_put_contents($arr_fnames[$i], $arr_contents[$i]);
        }
    }

    $array_names = glob('*.txt');
    $file = file_get_contents(end($array_names));    
    $temp = array();
    
    foreach (explode(PHP_EOL, $file) as $string) {
        $array = explode('|', $string);
        $temp[] = $array[0];
    }
    
    if (!in_array($us_sid, $temp)) {
        file_put_contents(end($array_names), $filing, FILE_APPEND | LOCK_EX);
    }
}

/** ******************** Вывод в браузер ******************** **/

$array_files = isset($_POST['check']) ? array_reverse(glob('*.txt')) : glob('*.txt');
$multiplicand = isset($_POST['multiplicand']) ? (int)$_POST['multiplicand'] : 30;
$multiplier = isset($_POST['multiplier']) ? (int)$_POST['multiplier'] : 60;
$composition = $multiplicand * $multiplier;
$time = time() - $composition;
$contents = null;
$sort_array = array();

if (!empty($_POST)) {
    foreach ($array_files as $str_fname) {
        if ((int)basename($str_fname, '.txt') >= $time) {
            foreach (explode(PHP_EOL, file_get_contents($str_fname)) as $str) {
                if (strlen($str) == '') continue;
                $sort_array[] = $str;
            }
        }
    }
    
    $content = '<table border="1" style="font-size: 14px;">'. 
                           '<tr><td>Дата</td><td>Браузер</td><td>IP</td><td>Откуда</td></tr>';

    foreach ($sort_array as $sort_str) {
        $arr = explode('|', $sort_str);
        $content .= '<tr><td>'. $arr[1] .'</td><td>'. $arr[2] .'</td>'.
                    '<td>'. $arr[3] .'</td><td>'. $arr[4] .'</td></tr>';
    }

    $content .= '</table>';
}

?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<title>Статистика посещений</title>
</head>
<body>
    <form method="post">
        <p>
            Статистика за период &nbsp;
            <input type="text" name="multiplicand" value="<?=$multiplicand?>" maxlength="2" size="1" />
            <input type="radio" name="multiplier" value="60" checked="checked" /> минут
            <input type="radio" name="multiplier" value="3600" /> часов 
            <input type="radio" name="multiplier" value="86400" /> суток
        </p>
        <p>
            Последние сверху
            <input type="checkbox" name="check" value="1" checked="checked" />
        </p>
        <input type="submit" value="просмотр" /><br />
    </form>
    <p><?=$content?></p>
</body>
</html>

Демо скрипта