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
https://forums.phpfreaks.com/topic/118886-unique-ip-views/
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
https://forums.phpfreaks.com/topic/118886-unique-ip-views/#findComment-612214
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
https://forums.phpfreaks.com/topic/118886-unique-ip-views/#findComment-612224
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.