Effective way to implement the number of views on images - PHP
<p>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.  </p>

<p>Now my idea on the implementation is this:  </p>

<p>Store the ipaddress of the visitor in a session, fe:  </p>

<pre><code>$key = "view_img_".$img-&gt;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;  
}  
</code></pre>

<p>Now my problem is this:  </p>

<p>I'd like it to clear a visitor's ipaddress inside the $_SESSION[$key] variable after a certain timelimit to clear resources.  </p>

<p>The thing is:<br>
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.  </p>

<p>Is there a way to define a lifespan for a specific $_SESSION variable so it resets itself? (equal to $_SESSION[$key] = null)  </p>

<p>Or would there be a better way to implement this?</p>
