summerock Posted August 9, 2008 Share Posted August 9, 2008 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 More sharing options...
secoxxx Posted August 9, 2008 Share Posted August 9, 2008 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 More sharing options...
Jabop Posted August 9, 2008 Share Posted August 9, 2008 add a relational ip table or a column with ips in the viewed table and check against it. Link to comment https://forums.phpfreaks.com/topic/118886-unique-ip-views/#findComment-612223 Share on other sites More sharing options...
Third_Degree Posted August 9, 2008 Share Posted August 9, 2008 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? 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 More sharing options...
secoxxx Posted August 9, 2008 Share Posted August 9, 2008 you ALWAYS have to use mysql_real_escape_string around $_SERVER["REMOTE_ADDR"] (The X-Forwarded-For http header can be altered) Thank you for the info, Ill start doing that, never thought to use it. Link to comment https://forums.phpfreaks.com/topic/118886-unique-ip-views/#findComment-612232 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.