client IP Address

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrtblt
Forum Newbie
Posts: 5
Joined: Mon Dec 10, 2007 7:40 am

client IP Address

Post 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());
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

To elaborate, check if the session exists, if not add the record.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
mrtblt
Forum Newbie
Posts: 5
Joined: Mon Dec 10, 2007 7:40 am

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
Post Reply