Jump to content

Help needed with "view counts"


ozone1

Recommended Posts

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

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

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

 

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
}
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.