ozone1 Posted November 28, 2008 Share Posted November 28, 2008 Hi guys i have a video site and there are "view counts" for each video but i want to restrict it per ip address. Currently if i click a particular video 10 times the view count increases by 10 but i want it to show 1 view per ip address no matter how many times i click. could anyone please help me with this? this is the code i have in my video page mysql_query("UPDATE videos SET view = view+1 WHERE id = '$id'"); Any help really appreciated Thanks !!! Link to comment https://forums.phpfreaks.com/topic/134621-help-needed-with-view-counts/ Share on other sites More sharing options...
blueman378 Posted November 28, 2008 Share Posted November 28, 2008 you would need a new table something like ip|videoid|timestamp when a user watches the video insert the details into the table before you load it do a query selecting the users ip where videoid = the videos id (timestamp is optional) if the query returns 1 result or more dont show the video Link to comment https://forums.phpfreaks.com/topic/134621-help-needed-with-view-counts/#findComment-700922 Share on other sites More sharing options...
blueman378 Posted November 28, 2008 Share Posted November 28, 2008 the timestamp i added so you could say if you wanted users to be able to view the video once an hour set the query to select ip from tablename where videoid = the vid id AND timestamp = Time()-3600 (i think thats right) Link to comment https://forums.phpfreaks.com/topic/134621-help-needed-with-view-counts/#findComment-700923 Share on other sites More sharing options...
ozone1 Posted November 28, 2008 Author Share Posted November 28, 2008 I dont get you i will explain bit more clear basically there are "view counts" ( ie, number of times a video is viewed) on my video page im having problem with some of my members they purposely click the video they submitted to increase the popularity of the video ( say after submission he clicked the video 100 times so that it will come top in the list ) so i want to restrict it if he watch it with same ip address 100 or 10000 times it should show only 1 view count (from him) on my video page. im not good in php can you please tell me step by step what to do thanks Link to comment https://forums.phpfreaks.com/topic/134621-help-needed-with-view-counts/#findComment-700930 Share on other sites More sharing options...
blueman378 Posted November 29, 2008 Share Posted November 29, 2008 right, well as i said youll need to create a new table: Set it up with these settings NAME: || TYPE: || LENGTH: IP: || varchar || 15 VideoId: || int || (same length as the column in your video table( else set as 9) Time: || int || 11 IP: the users ip address VideoId: this could be called something else (its however you uniquely identify the videos) Time: as i said in my above post this is optional however i would suggest using it as you could set the timeframe to eg a day, if a user watched a video again after a day it would technically mean the video is popular. anyway now that you have that setup lets say you have called the table popwatch, and lets assume your url looks something like www.myvideosite.com/watch.php?vidid=12 so you would add something like this to the top of your page: <?php $q = "DELETE FROM popwatch WHERE TIME <= Time()-86400"); //delete all rows where time has expired $result = mysql_query($q); $IP = $_SERVER["REMOTE_ADDR"]; //the users ip $videoid = $_GET["vidid"]; //the video id if(is_numeric($videoid)) //avoid sql injection { $q = "SELECT Time FROM popwatch WHERE IP='$IP' AND VideoId='$videoid'"; $result = mysql_query($q); $noway = mysql_num_rows($result); if($noway >=1) { echo "you have already viewed this video!"; } else { $time = Time(); $q = "INSERT INTO popwatch VALUES($IP, $videoid, $time)"; $result = mysql_query($q); // set the current user to watched video //CODE TO ADD ONE VIEW HERE } ?> Link to comment https://forums.phpfreaks.com/topic/134621-help-needed-with-view-counts/#findComment-701516 Share on other sites More sharing options...
cooldude832 Posted November 29, 2008 Share Posted November 29, 2008 Simplify this 2 fields VideoID IP Every View Insert After every insert delete duplicates Or Every View check if IP+VideoID in table If not Add it Then just use select COUNT from this new table and you done takes 5 minutes 1 Int Field 1 Varchar 15 field Link to comment https://forums.phpfreaks.com/topic/134621-help-needed-with-view-counts/#findComment-701523 Share on other sites More sharing options...
blueman378 Posted November 29, 2008 Share Posted November 29, 2008 lol with 4,000 more posts than me id expect you to be able to write things simpler for people ;P Link to comment https://forums.phpfreaks.com/topic/134621-help-needed-with-view-counts/#findComment-701541 Share on other sites More sharing options...
corbin Posted November 29, 2008 Share Posted November 29, 2008 A column of the INET_ATON data type would have a smaller index. Edit: (Well actually an unsigned int column that uses inet_aton values ;p) Link to comment https://forums.phpfreaks.com/topic/134621-help-needed-with-view-counts/#findComment-701543 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.