Need help with ip unique hits with times control

07-15-05, 02:09 PM
|
|
Newbie Coder
|
|
Join Date: Jul 2004
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Need help with ip unique hits with times control
Hi! I want to record total hits everytime when a user click on a link. It will record the user IP and the timestamp. I don't want to record multiple hits from a user with the same IP address but only record hits if it the timestamp of same user IP past 24 hours. Below is my code I've been working on and I don't know if it's working. Since I'm just a beginner. I need help from an experienced PHP that can point me to the right direction. Please help...Thank you.
Database Table
CREATE TABLE `yp_stats_uniquehits` (
`statsid` int(11) not null auto_increment,
`catid` int(11) not null default '0',
`listid` int(11) not null default '0',
`ip` varchar(255) not null,
`hits` varchar(255) not null default '0',
`timestamp` timestamp(14) not null,
PRIMARY KEY (`statsid`)
) TYPE=MyISAM;
Codes:
$catid = $_GET["catid"];
$listid = $_GET["listid"];
$newip = $_SERVER["REMOTE_ADDR"];
$timespast = time()-84600;
$checkip = mysql_query("SELECT ip FROM yp_stats_uniquehits");
$found = mysql_num_rows($checkip);
if($found < 1) {
$inserthits = mysql_query("INSERT INTO yp_stats_uniquehits (catid,listid,ip,hits) VALUES ('$catid','$listid','$newip','1')");
}
if($found > 0) {
$checkip = mysql_query("SELECT * FROM yp_stats_uniquehits WHERE timestamp > $timespast");
$found = mysql_num_rows($checkip);
if($found == 1) {
$updatehits = mysql_query("UPDATE yp_stats_uniquehits SET hits=hits+1");
}
}
|

07-18-05, 02:28 AM
|
|
Newbie Coder
|
|
Join Date: Dec 2004
Location: Tacoma, WA
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
|
Originally Posted by postyourweb
Hi! I want to record total hits everytime when a user click on a link. It will record the user IP and the timestamp. I don't want to record multiple hits from a user with the same IP address but only record hits if it the timestamp of same user IP past 24 hours. Below is my code I've been working on and I don't know if it's working. Since I'm just a beginner. I need help from an experienced PHP that can point me to the right direction. Please help...Thank you.
Database Table
CREATE TABLE `yp_stats_uniquehits` (
`statsid` int(11) not null auto_increment,
`catid` int(11) not null default '0',
`listid` int(11) not null default '0',
`ip` varchar(255) not null,
`hits` varchar(255) not null default '0',
`timestamp` timestamp(14) not null,
PRIMARY KEY (`statsid`)
) TYPE=MyISAM;
Codes:
$catid = $_GET["catid"];
$listid = $_GET["listid"];
$newip = $_SERVER["REMOTE_ADDR"];
$timespast = time()-84600;
$checkip = mysql_query("SELECT ip FROM yp_stats_uniquehits");
$found = mysql_num_rows($checkip);
if($found < 1) {
$inserthits = mysql_query("INSERT INTO yp_stats_uniquehits (catid,listid,ip,hits) VALUES ('$catid','$listid','$newip','1')");
}
if($found > 0) {
$checkip = mysql_query("SELECT * FROM yp_stats_uniquehits WHERE timestamp > $timespast");
$found = mysql_num_rows($checkip);
if($found == 1) {
$updatehits = mysql_query("UPDATE yp_stats_uniquehits SET hits=hits+1");
}
}
|
The code is ok. I would switch the hits column to an 'int' instead of a varchar. Also, you do not logically need to if statements. Either the query will return < 1 or > 0... So the second 'if' statement can be just an 'else' with nothing else... No pun intended. Other then that, everything else looks good.
|

07-18-05, 09:47 PM
|
|
Newbie Coder
|
|
Join Date: Jul 2004
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank You For Reply
Hi! Thank you very much for helping me out. I still having problem, it still count hits when I refresh it. I don't why? I've fix some codes but couldn't figure out what's the problem is...Can you help me...
Thank You
Huy Truong
$newip = $_SERVER["REMOTE_ADDR"];
$timestamp = time()-84600;
$checkip = mysql_query("SELECT ip FROM yp_stats_pageview timestamp > $timestamp");
$found = mysql_num_rows($checkip);
if($found < 1) {
$inserthits = mysql_query("INSERT INTO yp_stats_pageview (catid,listid,organization,ip,hits,timestamp) VALUES ('$catid','$listid','$organization','$newip','1',N OW())");
} else {
$updatehits = mysql_query("UPDATE yp_stats_pageview SET hits=hits+1 WHERE timestamp > $timestamp AND ip='$newip'");
}
|

07-18-05, 10:58 PM
|
|
Newbie Coder
|
|
Join Date: Jul 2004
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
Codes Change
$newip = $_SERVER["REMOTE_ADDR"];
$timestamp = time()-42300;
$checkip = mysql_query("SELECT ip,timestamp FROM yp_stats_pageview WHERE timestamp > $timestamp");
$found = mysql_num_rows($checkip);
if($found==1) {
$updatehits = mysql_query("UPDATE yp_stats_pageview SET hits=hits+'1'");
} else {
$inserthits = mysql_query("INSERT INTO yp_stats_pageview (catid,listid,organization,ip,hits) VALUES ('$catid','$listid','$organization','$newip','1')" );
}
|

07-19-05, 07:53 PM
|
|
Newbie Coder
|
|
Join Date: Jul 2004
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I figured it out, thanks for helping, here is how the code
using cookie and set time to expire within 24 hours, after cookie expire reset cookie again for the next 24 hours and update hits. It works fine on my server. Hopefully it will be helpful for someone else.
$newip = $_SERVER["REMOTE_ADDR"];
$checkip = mysql_query("SELECT ip FROM yp_stats_pageview WHERE ip='$newip'");
$found = mysql_num_rows($checkip);
if($found < 1) {
$inserthits = mysql_query("INSERT INTO yp_stats_pageview (catid,listid,organization,ip,hits) VALUES ('$catid','$listid','$organization','$newip','1')" );
setcookie("ip", $newip, time()+86400,"/","localpage.us",0);
} else {
if(!$_COOKIE["ip"]) {
setcookie("ip");
$updatehits = mysql_query("UPDATE yp_stats_pageview SET hits=hits+'1' WHERE ip='$newip'");
setcookie("ip", $newip, time()+86400,"/","localpage.us",0);
}
}
|

07-19-05, 08:07 PM
|
 |
Level II Curmudgeon
|
|
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
|
|
Quote:
|
Originally Posted by postyourweb
Hi! I want to record total hits everytime when a user click on a link. It will record the user IP and the timestamp. I don't want to record multiple hits from a user with the same IP address but only record hits if it the timestamp of same user IP past 24 hours. Below is my code I've been working on and I don't know if it's working. Since I'm just a beginner. I need help from an experienced PHP that can point me to the right direction. Please help...Thank you.
|
The simple way to do this would be to set the IP field as a 'unique' field and then delete old entries by watching a timestamp field.
Sine the IP field is 'unique' it will never count the same IP twice because it will fail on insertion. The timestamp field could be swept by a cron job once an hour to delete any row more than 24 hours old.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|