Getting visitor location in PHP.

I had been trying to track the location of visitors based on their IP addresses. I ran into problems earlier while trying to acheive that and gave up. Today, I Finally managed to do that. Thanks to the geodb availablehere. I hacked up a function that would accept an IP address as a parameter and return an array with location info.

The code for the function is:


function getIpInfo($ip){
$ipa=explode(".",$ip);
$ipnum=256*256*256*$ipa[0]+256*256*$ipa[1]+256*$ipa[2]+$ipa[3];
 //download the Geo DB file from http://software77.net/geoip-software.htm
$handle = fopen("IpToCountry.txt", "r");
$userinfo=NULL;
while (!feof($handle)){
 list ($start, $finish, $a1,$a2,$a3,$a4,$a5) = explode("t",fgets($handle));
   if(($start<=$ipnum)&&($ipnum<=$finish)) {$userinfo=array($a3,$a4,$a5); break; }
}
fclose($handle);
if(!$userinfo) $userinfo=array('Cannot Trace the Ip address','Cannot Trace the Ip address','Cannot Trace the Ip address');
return $userinfo;
}

One just needs to change the name of the file (in my case 'IpToCountry.txt') to the one that they have downloaded.The array returns ('2 Digit Country Code', '3 Digit Country Code', 'Full Country Name'), and, you can use it to display location or use it in your script as a variable. Make sure you do ip validation befor passing the IP address (ip2long()). A demo of it is available here. For a list of Other PHP/Javascript Demos please go to here.

news.ycombinator search

I am a regular reader of news.yc and like most people using that site, I wanted to be able to search the site. Since the site does not have an inbuilt search functionality, I created yc.news search here.

Just like in SansJ, one can use the search plugin (FF and IE7 only) and search from their browsers search bar. Also, existing users of SansJ will be able to login with their current SansJ usernames and passsword.

Scriptaculous Draggable in Action.

(If you are in a hurry, go here, that is what the post is about.)

It’s finally over!!

Relief followed by pride-both preceded by an Idea- pretty much summarizes my feeling now.

Recently I got a HTC Touch for my dad, which, I played around with for a while.I somehow feel, that, the Input devices (Mouse and Keyboard) that I use majority of the time is less fun than using a Touch Sensitive Interface. Maybe fun is not the right word, but, nevertheless the feeling is fun as you move your fingers across the Screen.

I have read in many places that the Market for Mobile Phones is increasing at a rapid rate and hence, I infer that the market for applications designed for such phones should be growing as well. The point is I would like to develop apps that are at the cutting/leading edge for mobile handsets. But, the problem as I see it is the size, yes, the size of the Screens, until we have some sort of a low-cost foldable-screen introduced in the market, I will stick with PC’s.

This is where Scriptaculous Draggables, caught my attention. The way one could drag an element (divs, tables, etc..) on the screen is cool and yes, it is fun.I had been working on an idea for a few weeks and when I hit a bump, I left the code alone for a week and came back to it yesterday. At the end of it all, I learned a few things in Javascript and CSS. I am hoping that this knowledge and showcase would be implemented in one of my other Projects:SansJ.

Anyway, checkout the Demo here: http://play.trk7.com/drag.html

I used a few Icons from the very nice Sweetie Icon Set available here: http://sweetie.sublink.ca/

and if you wish to look at my demos, you can go here: http://play.trk7.com (just one Demo is linked at the moment, I am planning on a regular update.)

Online Spell Checker…

This can be viewed online on my Social Search site Sansj.com. try searching for Yahoi Search.
It is a good thing have a spell checker facility in a Search Engine, people are not always correct with their spelling and yeah sometimes typos do occur. I read somewhere about using a statistical model to develop a spell checker, and, I was amazed. But I had neither the time or the brain to implement/optimize it,so I went for the easier option, I used yahho spell checker api available here.The code I came up with (with some help from my previous work and also a few other work available on the internet) is presented here:

//COPY PASTING WILL NOT WORK, UNLESS, YOU ARE WILLING TO CHANGE THE QUOTES
$req_spcheck=’http://search.yahooapis.com/WebSearchService/V1/spellingSuggestion?appid=YOURIDGOESHERE&query=YOURQUERY&results=3&output=php’; ///create the query here, output is serialized PHP
$checked=”;
$res = file_get_contents($req_spcheck);
$phpobj_spcheck = unserialize($res);
$key2=0;
foreach($phpobj_spcheck as $arr2) { ///for each loop to get each of the suggestions
$key2++;
$checked.='<a href=”http://trk7.com/ask/mysearch2.php?sstring=’.trim(urlencode($arr2[‘Result’])).’&submit=&co=all”>’.($arr2[‘Result’]).'</a>’; ///create a link that can be used also as a search query on my site
$str4=$arr2[‘Result’];
}
if ($str4!=””){ //making sure we r getting a corrected result
echo “<div style=’font-size:1.2em;background-color:#FFFFCC;border-top:1px solid gold;border-bottom:2px solid gold;text-align:left’><img src=’images/spellcheck.png’ style=’padding-right:2px’ alt=’Spellitrite ‘ />Did you mean:”.$checked.”?</div>”; ///display the spell suggestions
}

This can be viewed online on my Social Search site Sansj.com. try searching for Yahoi Search.

Totally Cool.
P.S. Should have known that, to display code in wordress, should have used htmlentities.Thanks.

SansJ.com updates.

My Social Search Engine:SansJ has had some updates to it’s interface recently. Most notable is the use of CSS based Buttons instead of standard HTML buttons. Thanks to some excellent free web 2.0-ish icons available here, I have been able to create some good-looking CSS buttons.The new menubar at the top has been created with help from here; which is a wonderful site to learn about HTML <li> and also look at some implementations (mainly CSS based menus).

I wa surprised to find that IE does not support :hover for anything other than <a> and was happy to use the workaround available here.

Another weird problem that I encountered with IE (versions 6 & 7).IE doesn’t seem to display text that had the color defined as ‘grey’, but, when I used the HEX value for color grey the text actually was visible. It is a bit difficult for me to put up with these issues as at home (where I mostly code), I use a mac and do not have IE (running windows a Guest OS, is something I wouldn’t want to do again), so I discover these quirks with IE when I get to work and then try to FTP to my site and try to edit files, and is getting quite painful as I am usually busy at work.

More updates, soon.I am hoping to complete some ideas that I would want to implement before I leave to NZ for a 4 day vacation.In preparation to the trip (apart from the shopping), I have decided to get rid of the tummy that could get out of control and found some help here.

Scriptaculous Slider in action.

Slider in action at SansJ

Ever since I had it in my mind-even though I could not think of any immediate benefit for my Social Search Site SansJ-I couldn’t make myself to code something that had a more practical value for my site.After about a week of experimenting (I am very average, but, lazy coder) finally managed to create a slider that is kind of Mac OSX’s Summarize feature (definitely not that good).

The reason for creating it goes this way:Due to the very nature of a Social Search, the relevancy of a result for a particular search query is usually proportional to the number of people who have searched for something similar and voted for a result.In other words if more people start using it, the better the results. Unfortunately, SansJ, has not reached a stage where the people power/wisdom is enough to return top quality search results. So, I had to come up with a solution that would not embarrass me. So if I searched for deleting rows in mysql based on date, I know that I can move the Slider to the left to see the most relevant result and ignore the rest. In other words, I see what I want to see (which also happens to be the most relevant).So until SansJ receives the attention it deserves, I will be happy to devise these tools.

Now to how I implemented it. It was a complete hack! I wouldn’t say this is the most elegant solution, but it works (in Firefox, Safari and Opera(mostly) on my MacOSX, I have a feeling it will break in IE ;)) and that is pretty true with all my coding work, I take a problem-to-solution approach with great emphasis on practicality than elegance (why Classes? I ponder!). I used the script.aculo.us library with help from these articles :

For fear of being exposed as a very sloppy programmer (than for any other reason), I am not displaying the source code (‘view source’ is one of the greatest features in any browser).So check out the feature here: http://sansj.com/mysearch3.php and search for say ajax tutorials , php based ip logging and so forth. Happy Sliding.

SQL and Authentication

Had been dabbling with PHP and SQL lately working on a personal project, and decided to use Authentication. After searching in Google for some info, came across this link: http://www.php-mysql-tutorial.com/user-authentication/, which has some good examples of how authentication can be achieved in PHP.

I am using MySQL V5.something and the auth example using Database was not working, I was not getting authenticated even though I was using the right credentials.After further researching, I found that the ‘HASH’ created by MySQL’s PASSWORD() function was 31-bytes long in Version4.something of MySQL and in later versions the ‘HASH’ has been made more secure by being longer (41-bytes).Thanks to this wonderful article here. After wondering what went wrong (and I did get Lost, mostly bcoz I tend not to document and try to challenge my brain to figure out what is happening).Such are the nuances that one needs to be aware of while programming-kind of takes the joy of programming away from you for some time- and that is why I do not want to be programming for work but for personal pleasure, alone.

The pleasure one gets out of writing/debugging a code and then completing what he has been set to accomplish is enormous, but what could (for a person like me) be more satisfying is maybe reading a good book or watching a great movie or even having some time alone;this blended with a dash of coding purely for trying to bring one’s own ideas to life on the tiny little electronic device that we call Computer. Without doubt, that is where I would like to head.

Then again, it’s said “there’s only one thing you need to know to do ‘high work’: ‘Don’t get comfortable.'”

To hell with that statement!