Jump to content

php/mysql unique view counter Problem


shortysbest

Recommended Posts

I am working on unique page view counter however i am running into this problem with my code. What i am trying to do is to create a unique view count per page and i have been attempting to do so a couple of ways. the current way i am doing it gets the users ip and gets the page they are on and inserts it into a single column in the database. table name is ips, column name is ip. inside of the ip column if the user is on the homepage it should insert a value that looks similar to this:  67.220.153.249 home    or if they were on a page called shows it would look like this:  67.220.153.249 shows

 

However with my current code it adds every view, not just unique view. So I need it to not add a view if the ip and page is already in the database, but to add 1 view if it isnt. So. I'm not sure why it isn't working correctly. I'm just getting impatient with it and was hoping i could get some help.

Maybe if someone had an idea and wanted to completely recode it to work how i want that would be great, however if someone could just figure out the problem in my code and point me in how to fix it that would be great too :) Thanks

 

<?php include('connect.php');
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
$page = $_GET['node'];
if ($_GET['node']=="product"){
$itm = $_GET['id'];
$ippage = $ip.' '.$page.' '.$itm;
}
else {
$ippage = $ip.' '.$page;}

$ipp = mysql_query("SELECT * FROM ips WHERE ip='$ippage'");
$ipf = mysql_fetch_assoc($ipp);
if (mysql_num_rows($ipp) == 0)
{}
else 
{
$query = mysql_query("UPDATE counter SET count=count+1 WHERE page='$page'");
if (mysql_affected_rows() == 0)
{
// No records updated, so add it
$query = mysql_query("INSERT INTO counter SET page='$page', count=count+1");
} 
if($_GET['node']=="product")
{
$itm = $_GET['id'];
$return = mysql_query("UPDATE itmcounter SET count=count+1 WHERE itm='$itm'");
if (mysql_affected_rows() == 0)
{
// No records updated, so add it
$return = mysql_query("INSERT INTO itmcounter SET itm='$itm', count=count+1");
}
}}
?>

Link to comment
https://forums.phpfreaks.com/topic/206814-phpmysql-unique-view-counter-problem/
Share on other sites

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.