<?

session_name('SESS');

session_start();

$sess=mysql_escape_string(session_id());

$livetime=time()-$set['time_sess'];



mysql_query("DELETE FROM `$mysql[pref]online` WHERE `last_time` < '$livetime';");

//mysql_query("OPTIMIZE TABLE `$mysql[pref]online`;");



$q = mysql_query("SELECT * FROM `$mysql[pref]online` WHERE `last_time` >= '$livetime' AND `sess` = '$sess' LIMIT 1;");

if(mysql_num_rows($q) != 0)$online = mysql_fetch_array($q);

else

{

mysql_query("INSERT INTO 

`$mysql[pref]online` (sess, 

ip, ua, pereh, time1, last_time, 

referrer) values ('$sess', 

'$ip', '$ua', '2', '$time', 

'$time', '".htmlspecialchars(

$_SERVER[HTTP_REFERER]).

"');");

$online['id']='';

$online['sess']=$sess;

$online['ip']=$ip;

$online['ua']=$ua;

$online['pereh']='1';

$online['time1']=$time;

$online['last_time']=$time;

}


/*


было тут


*/


//mysql_query("OPTIMIZE TABLE `$mysql[pref]users`;");

$q = mysql_query("SELECT COUNT(DISTINCT ip, ua) FROM `$mysql[pref]online` WHERE `pereh` > '1' AND `last_time` >= '$livetime';");

$onl['on_guest'] = mysql_result($q, 0);


$q = mysql_query("SELECT COUNT(*) FROM `$mysql[pref]users` WHERE `time` >= '$livetime' AND `sess` <> '';");

$onl['on_users'] = mysql_result($q, 0);


$q = mysql_query("SELECT COUNT(*) FROM `$mysql[pref]users`;");

$onl['users'] = mysql_result($q, 0);


/*


теперь <<добавлено для статистики>> тут


*/

/*старый код ↓

mysql_query("INSERT INTO `$mysql[pref]history` (`online`, `users`, `guest`, `time`)  values ('".($onl['on_users']+$onl['on_guest'])."','".$onl['on_users']."','".$onl['on_guest']."','".time()."');");

$max_online = mysql_fetch_array(mysql_query("SELECT MAX(online) FROM `$mysql[pref]history`;"));

$history = mysql_fetch_array(mysql_query("SELECT * FROM `$mysql[pref]history` WHERE `online` = '$max_online[0]' ORDER BY time DESC LIMIT 1;"));

mysql_query("DELETE FROM `$mysql[pref]history` WHERE `online` < '".$history['online']."';");

mysql_query("OPTIMIZE TABLE `$mysql[pref]history`;");

новый код ниже ↓

*/

mysql_query("INSERT INTO `$mysql[pref]history` (`online`, `users`, `guest`, `time`)  values ('".($onl['on_users']+$onl['on_guest'])."','".$onl['on_users']."','".$onl['on_guest']."','".time()."');");

$max_online = mysql_fetch_array(mysql_query("SELECT MAX(online) FROM `$mysql[pref]history`;"));

$history = mysql_fetch_array(mysql_query("SELECT * FROM `$mysql[pref]history` WHERE `online` = '$max_online[0]' ORDER BY time ASC LIMIT 1;"));

mysql_query("DELETE FROM `$mysql[pref]history` WHERE `id` <> '".$history['id']."';");

mysql_query("OPTIMIZE TABLE `$mysql[pref]history`;");


$cookie_log='';

$cookie_pass='';

if (isset($_COOKIE['nickname']) && isset($_COOKIE['password']))

{

$cookie_log=mysql_escape_string($_COOKIE['nickname']);

$cookie_pass=md5(mysql_escape_string($_COOKIE['password']));

}



$timeuser=$time;

$timesdvig=0;

$q = mysql_query("SELECT * FROM `$mysql[pref]users` WHERE (`time` >= '$livetime' AND `sess` = '$sess' AND `sequrity` = '0') OR (`nickname` = '$cookie_log' AND `password` = '$cookie_pass' AND `sequrity` = '0') OR (`time` >= '$livetime' AND `sess` = '$sess' AND `sequrity` = '1' AND `ip_1` = '$ip_1' AND `ip_2` = '$ip_2' AND `ip_op` = '$ip_op');");

$mail_in_new=0;

if(mysql_num_rows($q)!= 0)

{

$user = mysql_fetch_array($q, 1);

$timeuser=$time+$user['t_sdvig']*3600-17*3600;

$timesdvig=$user['t_sdvig']*3600-17*3600;

$q = mysql_query("SELECT COUNT(*) FROM `$mysql[pref]mail` WHERE `id_in_user` = '$user[id]' AND `read` = '0';");

$mail_in_new = mysql_result($q, 0);

}




$t_d=time()-86400; // 86400 секунд = 1 сутки

mysql_query("DELETE FROM `$mysql[pref]q2` WHERE `time` < '$t_d';");

mysql_query("INSERT INTO `$mysql[pref]q2` (time) values('$time')");


mysql_query("DELETE FROM `$mysql[pref]q` WHERE `time` < '$t_d';");

//mysql_query("OPTIMIZE TABLE `$mysql[pref]q`;");

$q = mysql_query("SELECT COUNT(*) FROM `$mysql[pref]q` WHERE `time` > '$t_d' AND `ip` = '$ip' AND `ua` = '$ua';");

if (mysql_result($q, 0)==0)mysql_query("INSERT INTO `$mysql[pref]q` (ip, time, ua) values('$ip', '$time', '$ua');");

else mysql_query("UPDATE `$mysql[pref]q` SET `time` = '$time' WHERE `ip` = '$ip' and `ua` = '$ua';");



$pereh_t=$online['pereh']+1;

mysql_query("UPDATE `$mysql[pref]online` SET `pereh` = '$pereh_t', `last_time` = '$time' WHERE `sess` = '$sess' and `id` = '$online[id]';");



if (isset($user))

{

$time_after_last = time() - $online['last_time'];

$user['time_all'] += $time_after_last;

mysql_query("UPDATE `$mysql[pref]users` SET `time_all` = '$user[time_all]' WHERE `id` = '$user[id]';");

}



$q = mysql_query("SELECT COUNT(*) FROM `$mysql[pref]q`;");

$counter['host'] = mysql_result($q, 0);

$q = mysql_query("SELECT COUNT(*) FROM `$mysql[pref]q2`;");

$counter['all'] = mysql_result($q, 0);



if ((!isset($user) || $user['level']<3) && $set['work']==2)

{

echo "Ведутся работы на сайте<br />

Приносим свои извинения за доставленные неудобства";

exit;

}


if ($set['work']==2)

{

function work($work){

return str_replace("</div>\n</body>", "САЙТ НЕДОСТУПЕН<br />\n</div>\n</body>", $work);

}

ob_start ("work");

}



function copyright($copyright){

/*

return str_replace("</body>", "<br />\n</div>\n</body>", $copyright);

*/


}

//ob_start ("copyright");


//echo "Расскажи друзьям-yastreba.net<br />";

?>

