Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
PHP Show Users on this Page
  • Xin
    Posts: 3,251
    Is there any PHP code that shows the amount of users that visited a certain page in the last 15minutes?

    Thanks
    Xin
  • sangf
    Posts: 203
    said:


    Is there any PHP code that shows the amount of users that visited a certain page in the last 15minutes?

    Thanks



    for this forum, specific php file, or arbitrary page? bit confused.
  • Xin
    Posts: 3,251
    Just a specific page, not to do with this forum
    Xin
  • sangf
    Posts: 203
    i'll test properly tomorrow, but here is an example with no error checking. not completely optimal, it rewrites the file per load.. couldn't think of a better solution this late. fyi- it's unique to ip addresses.


    <?
    $ip = getenv(\"REMOTE_ADDR\");
    $user_db_fname = \"_visited\"; // filename
    $timeout = 15; // minutes

    if (!file_exists($user_db_fname))
    {
    $out_file = fopen($user_db_fname, \"a+\");
    fclose($out_file);
    }

    $file = file($user_db_fname);
    $parsed_file = array();
    foreach ($file as $val)
    {
    $val_arr = explode(\";\", $val);
    $parsed_file[$val_arr[0]] = (int)$val_arr[1];
    }

    $cur_time = time();
    $parsed_file[$ip] = $cur_time;
    $out_file = fopen($user_db_fname, \"w\");
    foreach ($parsed_file as $key => $val) // ip => time
    {
    if (($cur_time - $val) > $timeout * 60)
    {
    unset($parsed_file[$key]); // timed out
    }
    else
    {
    fwrite($out_file, $key . \";\" . $cur_time . \"\r\n\");
    }
    }
    fclose($out_file);

    echo sizeof($parsed_file) . \" users visited in the last \" . $timeout . \" minutes.\";
    ?>