Просмотр файла inc/online.php

Размер файла: 4.39Kb
<?php

$time_online = 5; //время юзера в онлайне

if ($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']) {
    $soft = htmlspecialchars(stripslashes($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']));
} else {
    $soft = htmlspecialchars(stripslashes($_SERVER['HTTP_USER_AGENT']));
} 

if ($_SERVER['HTTP_X_FORWARDED_FOR']) $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if ($_SERVER['HTTP_CLIENT_IP']) $ip = $_SERVER['HTTP_CLIENT_IP'];
else if ($_SERVER['REMOTE_ADDR']) $ip = $_SERVER['REMOTE_ADDR'];
else {
    $ip = 'Не определён';
} 
$ip = preg_replace("/[^0-9.]/", "", $ip);
$ip = htmlspecialchars(stripslashes($ip));

$day = date("j", time());
$day = (int)$day;

$query = "SELECT * FROM " . $pref . "counter WHERE id=1";
$res00 = mysql_fetch_assoc(mysql_query($query));
// echo $res00['day'];
if ($res00['day'] != $day) {
    $query = 'UPDATE ' . $pref . 'counter SET host=1, hit=1, day=' . $day . ' WHERE id=1';
    mysql_query($query) or die ('ERROR');
    $query = 'TRUNCATE TABLE ' . $pref . "host_hit";
    mysql_query($query) or die ('ERROR');
    $query = 'INSERT INTO ' . $pref . 'host_hit (soft, ip) VALUES ("' . $soft . '", "' . $ip . '");';
    mysql_query($query);
    $hosts = 1;
    $hits = 1;
} else {
    $query = 'SELECT * FROM ' . $pref . 'host_hit WHERE (soft="' . $soft . '" AND ip="' . $ip . '")';
    $res11 = mysql_fetch_assoc(mysql_query($query));
    // echo $res11['soft'];
    if (($res11['soft'] === $soft) && ($res11['ip'] === $ip)) {
        $gaga = $res00['hit'] + 1;
        $query = 'UPDATE ' . $pref . 'counter SET hit=' . $gaga . ', day=' . $day . ' WHERE id=1';
        mysql_query($query) or die ('ERROR');
        $query = "SELECT * FROM " . $pref . "counter WHERE id=1";
        $res00 = mysql_fetch_assoc(mysql_query($query));
        $hosts = (int)$res00['host'];
        $hits = $gaga;
    } else {
        $gaga = $res00['hit'] + 1;
        $gbgb = $res00['host'] + 1;
        $query = 'UPDATE ' . $pref . 'counter SET host=' . $gbgb . ', hit=' . $gaga . ', day=' . $day . ' WHERE id=1';
        mysql_query($query) or die ('ERROR');
        $query = "SELECT * FROM " . $pref . "counter WHERE id=1";
        $res00 = mysql_fetch_assoc(mysql_query($query));
        $query = 'INSERT INTO ' . $pref . 'host_hit (soft, ip) VALUES ("' . $soft . '", "' . $ip . '");';
        mysql_query($query);
        $hosts = $gbgb;
        $hits = $gaga;
    } 
} 

if ($kto == 'user') {
    $idid = (int)$idid;
    $query = "SELECT * FROM " . $pref . "online_user WHERE id_user=" . $idid;
    $res00 = mysql_fetch_assoc(mysql_query($query));

    if ($res00['login_user'] === $login) {
        $query = 'UPDATE ' . $pref . 'online_user SET soft_user="' . $soft . '", ip_user="' . $ip . '", gde_user="' . $_SERVER['SCRIPT_NAME'] . '", data_unix=' . time() . ' WHERE id_user=' . $idid;
        mysql_query($query) or die ('ERROR123');
    } else {
        $query = 'INSERT INTO ' . $pref . 'online_user (id_user, login_user, soft_user, ip_user, gde_user, data_unix) VALUES (' . $idid . ', "' . $login . '", "' . $soft . '", "' . $ip . '", "' . $_SERVER['SCRIPT_NAME'] . '", ' . time() . ');';
        mysql_query($query);
    } 
} else {
    $query = 'SELECT * FROM ' . $pref . 'online_guest WHERE (soft_guest="' . $soft . '" AND ip_guest="' . $ip . '")';
    $res00 = mysql_fetch_assoc(mysql_query($query));
    $flag = (int)$res00['data_unix'];
    if ($flag > 0) {
        $query = 'UPDATE ' . $pref . 'online_guest SET soft_guest="' . $soft . '", ip_guest="' . $ip . '", gde_guest="' . $_SERVER['SCRIPT_NAME'] . '", data_unix=' . time() . ' WHERE (soft_guest="' . $soft . '" AND ip_guest="' . $ip . '")';
        mysql_query($query) or die ('ERROR456');
    } else {
        $query = 'INSERT INTO ' . $pref . 'online_guest (soft_guest, ip_guest, gde_guest, data_unix) VALUES ("' . $soft . '", "' . $ip . '", "' . $_SERVER['SCRIPT_NAME'] . '", ' . time() . ');';
        mysql_query($query);
    } 
} 

$time_del = time()-60 * $time_online;
$query = 'DELETE FROM ' . $pref . 'online_user WHERE data_unix<' . $time_del;
mysql_query($query);
$query = 'DELETE FROM ' . $pref . 'online_guest WHERE data_unix<' . $time_del;
mysql_query($query);

$user_online = mysql_num_rows(mysql_query("SELECT * FROM " . $pref . "online_user"));
$guest_online = mysql_num_rows(mysql_query("SELECT * FROM " . $pref . "online_guest"));
$vsego_online = $user_online + $guest_online;

?>