I've created a website where users can upload images and i'd like to record the total number of views for an individual image.
Now my idea on the implementation is this:
Store the ipaddress of the visitor in a 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 the livespan of this session
break;
}
}
if(!$found)
{
//do sql update query for the number of views for this image
$sql = mysql_query("UPDATE ") //etc.
//Add this ip to the list in the session
$_SESSION[$key][] = $visitorIpAdress;
}
Now my problem is this:
I'd like it to clear a visitor's ipaddress inside the $_SESSION[$key] variable after a certain timelimit to clear resources.
The thing is:
Most of the visitors might only view the image once, and when a certain image gets old it might not be visited any longer, but the session will still exist.
Is there a way to define a lifespan for a specific $_SESSION variable so it resets itself? (equal to $_SESSION[$key] = null)
Or would there be a better way to implement this?