Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.