Page 1 of 1

client IP Address

Posted: Mon Dec 10, 2007 7:44 am
by mrtblt
On the beginning of my index.php file, i inserted the following code to track the visitors who visit my website. But obviously, anytime when they return to the index page, the following code makes a recurring record. So how may i avoid this recurring recording each time when the visitor returns to the index page?

Code: Select all

$clientip= $_SERVER['REMOTE_ADDR'];
$tarih = date('Y.m.d');
mysql_query("INSERT INTO visitors VALUES('$clientip','$tarih')") or die(mysql_error());

Posted: Mon Dec 10, 2007 8:15 am
by jmut

Posted: Mon Dec 10, 2007 9:19 am
by John Cartwright
To elaborate, check if the session exists, if not add the record.

Posted: Mon Dec 10, 2007 10:22 am
by RobertGonzalez
Also keep in mind that IP addresses are not the best just of identity for a client machine. IP can, and in many cases do, change frequently.

Posted: Mon Dec 10, 2007 10:45 am
by mrtblt
Dear "Jcart",

Thanks for the attention. I am okay with handling matters regarding database issues but i am totally a newbie on those kind of subject.
Would you be more specific and if possible by providing an example

regards.

Posted: Mon Dec 10, 2007 11:28 am
by John Cartwright
I recommend you read the manual regarding sessions first if don't understand how they are used, which jmut kindly linked.

As for an example, here

Code: Select all

session_start();
if (!isset($_SESSION['userflag'])) {
   mysql_query(...) or die(mysql_error());
   $_SESSION['userflag'] = true;
}
When the page is loaded for the first time the $_SESSION['userflag'] won't exist, which will allow you to execute any code that only should be run once.