Jump to content

insert ip into database if it doesn't exist 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 the code below once there is a line in it, it adds  67.220.153.249 home  everytime i click home, not just once. but i have the code saying if the database row ip is not equal to the current users ip and page to add the row in the database else it should not add a row.

 

I have been changing the code all around for awhile and now i just cant figure it out.

<?php
//get users ip address//start//
    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'];
    }
//get users ip address//end//
$pg = $_GET['node'];
$ippage = $ip.' '.$pg;
$ipp = mysql_query("SELECT * FROM ips WHERE ip='$ip'");
$ipf = mysql_fetch_assoc($ipp);
if ($ipf['id']!==$ippage)
$query = mysql_query("INSERT INTO ips SET ip='$ippage'");
?>

$ipp = mysql_query("SELECT * FROM ips WHERE ip='$ip'");
$ipf = mysql_fetch_assoc($ipp);
if ($ipf['id']!==$ippage)
$query = mysql_query("INSERT INTO ips SET ip='$ippage'");

 

to

 

$ipp = mysql_query("SELECT * FROM ips WHERE ip='$ippage'");
$ipnum = mysql_num_rows($ipp);
if (!$ipnum)
   $query = mysql_query("INSERT INTO ips VALUES('$ippage')");

 

$ipp = mysql_query("SELECT * FROM ips WHERE ip='$ip'");
$ipf = mysql_fetch_assoc($ipp);
if ($ipf['id']!==$ippage)
$query = mysql_query("INSERT INTO ips SET ip='$ippage'");

 

to

 

$ipp = mysql_query("SELECT * FROM ips WHERE ip='$ippage'");
$ipnum = mysql_num_rows($ipp);
if (!$ipnum)
   $query = mysql_query("INSERT INTO ips VALUES('$ippage')");

 

Thanks for the reply, I never thought about doing it by num_rows, but i did get everything working now doing it a little differently. But thank you.

 

I did it:

 

    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'];
    }
$pg = $_GET['node'];
if ($_GET['node']=="product"){
$itm = $_GET['id'];
$ippage = $ip.' '.$pg.' '.$itm;
}
else
$ippage = $ip.' '.$pg;
$ipp = mysql_query("SELECT * FROM ips WHERE ip='$ippage'");
$ipf = mysql_fetch_assoc($ipp);
if ($ipf['ip']==$ippage)
{}
else
mysql_query("INSERT INTO ips SET ip='$ippage'");

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.