Effective way to implement the number of views on images - PHP -
i've created website users can upload images , i'd record total number of views individual image.
now idea on implementation this:
store ipaddress of visitor in session, fe:
$key = "view_img_".$img->id; if(!isset($_session[$key])) { $_session[$key] = array(); } $visitoripaddress = $_server['remote_address']; $found = false; foreach($ip in $_session[$key]) { if($ip == $visitoripadress) { $found = true; //visitor has visited before in livespan of session break; } } if(!$found) { //do sql update query number of views image $sql = mysql_query("update ") //etc. //add ip list in session $_session[$key][] = $visitoripadress; }
now problem this:
i'd clear visitor's ipaddress inside $_session[$key] variable after timelimit clear resources.
the thing is:
of visitors might view image once, , when image gets old might not visited longer, session still exist.
is there way define lifespan specific $_session variable resets itself? (equal $_session[$key] = null)
or there better way implement this?
what counting via sessions approach number of unique views. if want count number of views, don't put brain , run query like:
update counttable set views = views + 1 imageid = 123
populate imageid , you're go. kiss.
Comments
Post a Comment