Track visitors using Google Maps
Create a table in your database, like 'visitor_map'. We will have these fields: id, ref, ip, location, longitude and latitude, execute the following code:
CREATE TABLE IF NOT EXISTS `visitor_map` ( `id` int(11) NOT NULL auto_increment, `ref` varchar(255) NOT NULL, `ip` varchar(15) NOT NULL, `location` varchar(32) NOT NULL, `longitude` float NOT NULL, `latitude` float NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
For ease of use, we'll now create a configuration file. Create a file called config.php and insert the following, changing the constant values to suit your database setup and Google Maps API key.
define('DB_HOST', 'localhost'); // Database host define('DB_NAME', 'Database'); // Database being used define('DB_USER', 'user'); // Database user define('DB_PASS', 'password'); // Database user's password /* Your Google Maps API key */ define('API_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); function IPtoCoords($ip) { $dom = new DOMDocument(); $ipcheck = ip2long($ip); if($ipcheck == -1 || $ipcheck === false) trigger_error('Invalid IP, what are you doing? :|', E_USER_ERROR); else $uri = 'http://api.hostip.info/?ip=' . $ip; $dom->load($uri); $location = (strpos($dom->getElementsByTagName('name')->item(1)->nodeValue, 'Unknown') === false) ? $dom->getElementsByTagName('name')->item(1)->nodeValue : $dom->getElementsByTagName('countryAbbrev')->item(0)->nodeValue; if($location == 'XX') return false; else { $dom->load('http://local.yahooapis.com/MapsService/V1/geocode?appid=' . Yahoo_Key . '&location=' . $location); $longitude = $dom->getElementsByTagName('Longitude')->item(0)->nodeValue; $latitude = $dom->getElementsByTagName('Latitude')->item(0)->nodeValue; return array('location' => $location, 'longitude' => $longitude, 'latitude' => $latitude); } }
Now that we can successfully gain the visitor's location, we can use this to build our finished visitor map.
Now paste this code to /templates/your_skin_dir_name/main/main.tpl and index.tpl
{php} require 'config.php'; mysql_connect(DB_HOST, DB_USER, DB_PASS); mysql_select_db(DB_NAME); $ref = $_SERVER["HTTP_REFERER"]; $ip = $_SERVER['REMOTE_ADDR']; $userinfo = IPtoCoords($ip); $user = mysql_query('SELECT `location` FROM `visitor_map` WHERE `location` = '' . $userinfo['location'] . '''); if(!mysql_fetch_row($user) && $userinfo) mysql_query('INSERT INTO `visitor_map` (`ref`, `ip`, `location`, `longitude`, `latitude`) VALUES ('' . mysql_real_escape_string($ref) . '', '' . mysql_real_escape_string($ip) . '', '' . $userinfo['location'] . '', ' . $userinfo['longitude'] . ', ' . $userinfo['latitude'] . ')') or die(mysql_error()); {/php}
06.07.2009. 09:21
Comments
i am using the same code but gettng an error like the fun iptocoords is not valid
Valuable info. Lucky me I found your site by accident, I bookmarked it.
The moment I saw your blog was like wow. Thank you for putting your effort in publishing this site.
Regards for all the great blog posts!
I have many question about your post. I think u should write lot of more details. But Thanks anymore
That is f*ckin' awesome iRealty mods . Very good and interesting article. Thanks for helpful and useful information.
Today I read some very interesting article. Its Ur article. Thanks
First of all I care for the quality of your post. That was interesting to read and helpful article. And Im lookin' forward to see your next thinks.
Good job. Useful and helpful info here. Thanks
and I have been looking for such information
That's useful informative and f*ckin awesome. Great job
Im glad to see this informative article. Thanks for such helpful and useful post.
due recently reinstalled the operating arrangement, and it took a batch of things in identical place
This is the right blog for anyone who wants to find out about this subject.
Darn, I enjoy your selection of layout, it truly is unique in my opinion
It does seem that everybody is into this kind of stuff lately. Appriciate you shedding light into this matter. Keep it up.
I have been examinating out some of your articles and i must say clever stuff. I will surely bookmark your website.
Get Started on Your Own Weight Loss and Health Goals right away with these Simple and Effective Healthy Meal Plans.
great knowledge of carries bettered very much.
i was thinking the other day that there must be a technique with a view my kids to training at digs what they learn in class
i'm simply 15, and i don't know much about the rates and if they're expected to flatten, keep climbing,
Have you thought about adding some differing opinions to your article
Very Nice website. I just finished mine and i was looking for some design ideas and you gave me a few. May i ask you whether you developed the website by youself
I appreciate the info!
i love your website. i'm gonna post a link on my facebook page!
i dont think i'd have the time or even drive to maintain it up to date as you do once again many thanks as well as all the best
thank you as one more great article. where else could anybody reach that agreeable of knowledge inside that a faultless convention of writing
Keep posting stuff like this i really like it
very newsworthy topic , outstanding post.
Thank You For This Post, was added to my bookmarks.
Write a comment
* = required field