Current location: Hot Scripts Forums » Programming Languages » PHP » Need help with ip unique hits with times control


Need help with ip unique hits with times control

Reply
  #1 (permalink)  
Old 07-15-05, 02:09 PM
postyourweb postyourweb is offline
Newbie Coder
 
Join Date: Jul 2004
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Smile 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");
}
}
Reply With Quote
  #2 (permalink)  
Old 07-18-05, 02:28 AM
xtremenw xtremenw is offline
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.
Reply With Quote
  #3 (permalink)  
Old 07-18-05, 09:47 PM
postyourweb postyourweb is offline
Newbie Coder
 
Join Date: Jul 2004
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Smile 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'");
}
Reply With Quote
  #4 (permalink)  
Old 07-18-05, 10:58 PM
postyourweb postyourweb is offline
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')" );
}
Reply With Quote
  #5 (permalink)  
Old 07-19-05, 07:53 PM
postyourweb postyourweb is offline
Newbie Coder
 
Join Date: Jul 2004
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up 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);
}
}
Reply With Quote
  #6 (permalink)  
Old 07-19-05, 08:07 PM
End User's Avatar
End User End User is offline
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.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Source Control Review denasinc@ ASP.NET 1 04-25-05 02:41 PM
Filling a textbox on a parent web user control from a child web form neilault ASP.NET 1 11-14-04 10:32 PM
Email Submit Problem BMSLOSER HTML/XHTML/XML 1 06-21-04 03:22 PM


All times are GMT -5. The time now is 06:56 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.