hi,
okay i have a counter script which records the useragent datetime and ip to the DB and also keeps a counter,
Also using a txt file with bot names in its supposed to check the useragent if its a bot set the type to bot not user and increment the bot total not the user total.
However it dont seem to work the only time i have seen it work and set one as a bot is on ask jeeves.
okay so the script:
PHP Code:
<?php
/*function count_rows_where($type)
{
$row_count_res = mysql_query("SELECT * FROM counter WHERE type='$type'");
$num_of_rows = mysql_num_rows($row_count_res);
if($type=='bot')
{
mysql_query("UPDATE counter_total SET bot_total='$num_of_rows' WHERE id='2'");
}
else
{
mysql_query("UPDATE counter_total SET total='$num_of_rows' WHERE id='2'");
}
}*/
$ip = $_SERVER['REMOTE_ADDR'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$datetime = date('Y-m-d H:i:s');
$result = mysql_query("SELECT * FROM counter WHERE ip='$ip' ORDER BY visit_time DESC LIMIT 1")
or die("ERROR select check if exist".mysql_error());
$user_count = mysql_fetch_array($result);
$row_num = mysql_num_rows($result);
$yesturday = date('U') - 60*60*24;
if($row_num == 1 && strtotime($user_count['visit_time']) > $yesturday)
{
$result_2 = mysql_query("SELECT total FROM counter_total WHERE id='1'");
$count = mysql_fetch_array($result_2);
echo $count['total'];
$result_id = mysql_query("SELECT * FROM counter WHERE ip='$ip' AND user_agent='$user_agent' ORDER BY visit_time DESC LIMIT 1") or die("Error retriveing ID".mysql_error());
$counter_id = mysql_fetch_array($result_id);
echo '<!-- COUNTER ID: '.$counter_id['id'].'-->';
// count_rows_where('bot');
// count_rows_where('user');
}
else {
$user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
$spider_flag = false;
/* if (!empty($user_agent))
{*/
$spiders = file(DOC_ROOT.'/includes/spiders.txt');
for ($i = 0; $i < count($spiders); $i++)
{
if (!empty($spiders[$i]))
{
if (is_integer(strpos($user_agent, trim($spiders[$i]))))
{
$spider_flag = true;
break;
}
else
{
$spider_flag = false;
break;
}
}// End if not empty spider
}// End FOR()
if($spider_flag == false)
{
//is normal user
mysql_query("INSERT INTO counter SET ip='$ip',visit_time='$datetime',user_id='0',user_agent='$user_agent',type='user'")
or die("ERROR inserting".mysql_error());
mysql_query("UPDATE counter_total SET total=total + 1 WHERE id='1'")
or die("ERROR adding to counter total".mysql_error());
// count_rows_where('bot');
// count_rows_where('user');
}
else {
// Is Spider/Bot
mysql_query("INSERT INTO counter SET ip='$ip',visit_time='$datetime',user_id='0',user_agent='$user_agent',type='bot'")
or die("ERROR inserting".mysql_error());
mysql_query("UPDATE counter_total SET bot_total=bot_total + 1 WHERE id='1'")
or die("ERROR adding to counter total".mysql_error());
// count_rows_where('bot');
// count_rows_where('user');
} // End ELSE spider flag false
$ip = $_SERVER['REMOTE_ADDR'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$result_2 = mysql_query("SELECT total FROM counter_total WHERE id='1'");
$count = mysql_fetch_array($result_2);
echo $count['total'];
$result_id = mysql_query("SELECT id FROM counter WHERE ip='$ip' ORDER BY visit_time DESC LIMIT 1");
$counter_id = mysql_fetch_array($result_id);
echo '<!-- COUNTER ID: '.$counter_id['id'].'-->';
}
?>
heres a example of the DB
59274.6.22.1522008-01-27 00:45:390mozilla/5.0 (compatible; yahoo! slurp/3.0;
http://...user 59374.6.25.282008-01-27 01:11:530mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 59474.6.25.2222008-01-27 01:15:120mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 59574.86.171.22008-01-27 01:19:250mozilla/4.0 (compatible; msie 7.0; windows nt 6.0)user 59674.6.8.1182008-01-27 03:05:390mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 59774.6.26.142008-01-27 03:30:520mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 59874.6.28.442008-01-27 03:36:070mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 59974.6.8.762008-01-27 04:19:360mozilla/5.0 (compatible; yahoo! slurp/3.0;
http://...user 60074.6.22.2292008-01-27 04:20:320mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 60172.29.233.1882008-01-27 05:32:300dragonfly(ebingbong#playstarmusic.com)use r 602202.69.178.752008-01-27 05:44:150mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;...user 60365.214.45.1002008-01-27 06:04:420mozilla/5.0 (compatible; ask jeeves/teoma; +http:/...bot 60474.6.20.2142008-01-27 06:14:330mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 60574.6.23.242008-01-27 06:29:480mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 60674.6.23.1442008-01-27 06:43:280mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 60774.6.8.1132008-01-27 07:25:000mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 60874.6.22.1022008-01-27 07:33:520mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 60974.6.20.2222008-01-27 08:24:160mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 61074.6.24.1492008-01-27 09:08:190mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 61174.6.25.1212008-01-27 09:08:520mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 61274.6.24.182008-01-27 09:12:160mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 61374.6.25.2172008-01-27 10:13:310mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 61474.6.24.1712008-01-27 11:51:430mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 61574.6.20.702008-01-27 12:26:530mozilla/5.0 (compatible; yahoo! slurp;
http://help...user 616195.93.21.102008-01-27 13:38:090mozilla/4.0 (compatible; msie 7.0; aol 9.0; window...user 617195.93.21.1362008-01-27 13:38:230mozilla/4.0 (compatible; msie 7.0; aol 9.0; window...user 618195.93.21.62008-01-27 13:38:290mozilla/4.0 (compatible; msie 7.0; aol 9.0; window...user 619195.93.21.362008-01-27 13:38:360mozilla/4.0 (compatible; msie 7.0; aol 9.0; window...user 620202.160.180.1342008-01-27 14:27:080mozilla/5.0 (compatible; yahoo! slurp china; http:...user 62186.21.65.2262008-01-27 16:10:260mozilla/4.0 (compatible; msie 7.0; windows nt 6.0;...user
as u can see the only one which says bot is ask,
the spiders.txt file:
Code:
ask jeeves
googlebot
yahoo! slurp
yahoo-mmcrawler
So any answers would be apprieciated and as usualt TP for good and helpful answers!