Jump to content

unique IP views


summerock

Recommended Posts

here is the code

 

function update_viewed ($a)

{

        global $conn;

$query = "UPDATE members SET viewed  = viewed  + 1 WHERE USERID='".mysql_real_escape_string($a)."'";

        $executequery=$conn->execute($query);

}

 

it add 1 to "viewed" everything when that page is viewed. I would like to make it so that it add 1 viewed only if it's a UNIQUE IP.

 

thank help would be greatly appreciated.

Link to comment
Share on other sites

this is how i do it.

i call this with ajax on page load, but you can insert this into the page aswell.

 

just mod it a lil to fit your needs. you dont need the mysql conection just edit it up.

 

table contains,

 

pre_id

pre_ip

pre_times

 

<?php
session_start();
$ip = $_SERVER['REMOTE_ADDR'];
$times = $_SESSION['pre_times'];

if(isset($_SESSION['pre_times']))
    $_SESSION['pre_times'] = $_SESSION['pre_times']+ 1;
else
    $_SESSION['pre_times'] = 1;

$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
  mysql_select_db("database", $con);
  mysql_query("INSERT INTO previews_users (pre_times, pre_ip) VALUES (\"$times\", \"$ip\") ON DUPLICATE KEY UPDATE pre_times='$times'");

mysql_close($con);
?>   

 

 

EDIT: Oh wait, i re read what you want, just unique page views, not how many times a unique viewed a page?

Link to comment
Share on other sites

this is how i do it.

i call this with ajax on page load, but you can insert this into the page aswell.

 

just mod it a lil to fit your needs. you dont need the mysql conection just edit it up.

 

table contains,

 

pre_id

pre_ip

pre_times

 

<?php
session_start();
$ip = $_SERVER['REMOTE_ADDR'];
$times = $_SESSION['pre_times'];

if(isset($_SESSION['pre_times']))
    $_SESSION['pre_times'] = $_SESSION['pre_times']+ 1;
else
    $_SESSION['pre_times'] = 1;

$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
  mysql_select_db("database", $con);
  mysql_query("INSERT INTO previews_users (pre_times, pre_ip) VALUES (\"$times\", \"$ip\") ON DUPLICATE KEY UPDATE pre_times='$times'");

mysql_close($con);
?>   

 

 

EDIT: Oh wait, i re read what you want, just unique page views, not how many times a unique viewed a page?

 

:o you ALWAYS have to use mysql_real_escape_string around $_SERVER["REMOTE_ADDR"] (The X-Forwarded-For http header can be altered)

Link to comment
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.