943,633 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 897
  • PHP RSS
Jan 14th, 2009
0

visitor Details

Expand Post »
hi I had a web site made in php. I need to collect visitor data ( ip address, visiting time) and store in it a database. I wrote the code like this

PHP Syntax (Toggle Plain Text)
  1. date_default_timezone_set('Asia/Calcutta');
  2. $time= date('D,F j, Y, H:i:s A');
  3. $ip=getenv("REMOTE_ADDR");
  4. mysql_query("INSERT into visitordetails (ip,time) VALUES ('$ip','$time')") or die(mysql_error());

and this is works perfectly for me.. Let me know am i need to do anything more in it ????
Similar Threads
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
rajeesh_rsn is offline Offline
265 posts
since Sep 2008
Jan 14th, 2009
0

Re: visitor Details

Dont format the date until you pull the data out of the database
its much quicker to update the table or perform compares when later reporting if the data is timestamp

and
php Syntax (Toggle Plain Text)
  1. <?php
  2. $timestamp = time();
  3. $timeout = $timestamp - 1500;
  4. //Insert User
  5. $insert = mysql_query("INSERT INTO TG_whos_online (timestamp, ip, file) VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."')") or die("Error in who's online insert query!");
  6. //Delete Users
  7. $delete = mysql_query("DELETE FROM TG_whos_online WHERE timestamp<$timeout") or die("Error in who's online delete query!");
  8. //Fetch Users Online
  9. $result = mysql_query("SELECT DISTINCT ip FROM TG_whos_online") or die("Error in who's online result query!");
  10. $users = mysql_num_rows($result);
  11. //Show Who's Online
  12. if($users == 1) {
  13. print("<span>$users Person Online. &nbsp;</span>\n");
  14. } else {
  15. print("<span>$users People Online. &nbsp;</span>\n");
  16. }
  17. ?>
sql Syntax (Toggle Plain Text)
  1. /*
  2. This is the database creation SQL. You will need to create this table in your MySQL database.
  3. */
  4.  
  5. CREATE TABLE `TG_whos_online` (
  6. `id` BIGINT(20) NOT NULL AUTO_INCREMENT,
  7. `TIMESTAMP` INT(15) NOT NULL DEFAULT '0',
  8. `ip` VARCHAR(40) NOT NULL DEFAULT '',
  9. `file` VARCHAR(100) NOT NULL DEFAULT '',
  10. PRIMARY KEY (`id`),
  11. KEY `ip` (`ip`),
  12. KEY `file` (`file`),
  13. KEY `TIMESTAMP` (`TIMESTAMP`)
  14. ) TYPE=MyISAM;

add a visitcount column to the database
php Syntax (Toggle Plain Text)
  1. $query = mysql_query(sprintf("select id from visitordetails where ip='%s';", $_SERVER["REMOTE_ADDR"]));
  2. list($id) = mysql_fetch_row($query);
  3. if (strlen($id))
  4. {
  5. if (!mysql_query(sprintf("UPDATE visitordetails SET visitcount = visitcount + 1 WHERE ip ='%s';", $_SERVER["REMOTE_ADDR"])));
  6. {
  7. return false;
  8. }
  9. return true;
  10. }
and/or update the table with a last_visit column and do the same exist check to post the current timestamp to last_visit
Reputation Points: 562
Solved Threads: 367
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Jan 14th, 2009
0

Re: visitor Details

or install the geoip library
and map the locations from the ip and crate a googlemap mashup of the ip locations
Reputation Points: 562
Solved Threads: 367
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Jan 15th, 2009
0

Re: visitor Details

Click to Expand / Collapse  Quote originally posted by almostbob ...
and/or update the table with a last_visit column and do the same exist check to post the current timestamp to last_visit
php Syntax (Toggle Plain Text)
  1. query = mysql_query(sprintf("select id from visitordetails where ip='%s';", $_SERVER["REMOTE_ADDR"]));
  2. list($id) = mysql_fetch_row($query);
  3. if (strlen($id)) {
  4. if (!mysql_query(sprintf("UPDATE visitordetails SET visitcount = visitcount + 1, lastvisit='%s' WHERE ip ='%s';" ,$timestamp, $_SERVER["REMOTE_ADDR"])));
  5. { return false; }
  6. return true;
Last edited by peter_budo; Jan 17th, 2009 at 6:05 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 562
Solved Threads: 367
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009
Jan 15th, 2009
0

Re: visitor Details

Thank you for the kind support..
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
rajeesh_rsn is offline Offline
265 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How to add amount of items in shopping cart? Need urgent help!
Next Thread in PHP Forum Timeline: please tell me what is wrong with the code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC