Jul 23 2009

Using PHP/cURL to grok your public IP address

I had the occasion to create a PHP page that displays the server’s current public IP address. Not necessarily a good thing to display. But, I have several internal web sites on a development server where the host names are not available on a public DNS server. Displaying the server’s current public IP address is handy to prevent needing to nslookup my dyndns host name when altering my host file.

So, here is how I did it:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "www.checkip.org");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);

$pattern = '/Your IP:  ([\d]{1,3}.[\d]{1,3}.[\d]{1,3}.[\d]{1,3})<\/span>/';
$matches = array();
preg_match($pattern, $output, $matches);

$yourIP = 'N/A';
if (count($matches) > 1) {
  $yourIP = $matches[1];
}
curl_close($ch);

What I’m doing here is using cURL to get the page at checkip.org and then using a regular expressing to get the IP address returned in that page.

Albeit not completely fault-tolerant, as the web site can change it’s structure, but this type of quick’n'dirty screen scraping was what I needed at the time.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
Apr 17 2009

Vogon Poetry iPhone App

The latest iPhone application that I worked on is a Vogon poetry constructor. With 8 different categories, you can construct the best terrible poetry in the universe. Available in the iTunes store now.

Click Here to get Vogon Poetry

Credits:

Gabriel Serafini – user interface and graphical design

Steve Wamsley – application development

John Julius Tamm-Buckle – vocabulary development

Damon Wallace – animations

We are giving away 24 promotional codes so you can get the app for free. Contact me or Gabriel Serafini to get the hook up. Hurry before they’re all gone!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
Feb 06 2009

How much weight do I need to lose?

How much weight do I need to lose to attain a healthy body mass? The standard weight charts tell that due to my height (5′10″), frame size (medium), and gender (male), I should weigh from 151 to 163 lbs. Really? I mean I currently weight 213 lbs. Do I really need to lose 50 lbs?

The weight charts do not take into account lean body mass (and no, I’m not saying that I’m 200+ lbs. of muscle). So, given my body fat percentage and a realistic goal of attaining a weight consisting of 20% body fat, how much do I really need to lose?

The answer requires a means for measuring body fat and some math. I have a scale at home that measures my weight and body fat and currently I am 213.2 lbs., 33% of which is fat. Here is a formula to find my ideal body weight:

Where Wc is my current weight, Fc is my current body fat percentage, Wg is my goal weight, and Fg is my goal body fat percentage. My ideal body fat percentage for my gender (again, male) and age (38) is between 8 and 19 percent.

Plugging in the numbers for my current weight and body fat and a goal body fat of 19% gives me:

The result is an ideal weight of 176.4. Slightly more than what the body fat charts recommend. Perhaps I do have some extra muscle? Maybe?

So, I need to lose about 37 lbs. Or, two Katos. My cat Kato currently weighs 17 lbs. I’m not going to go into whether or not he is at his ideal weight. He’s a big cat – not fat, just big. But, and more importantly, he’s heavy. If I lost the weight amounting to two Katos, think about the stress that would take off my medium frame? And that is the real goal.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
Dec 25 2008

New app, coming soon to an App Store near you: Cheapest

What is cheapest? Two 12 oz. cans for $4.99 or one pint for $3.99? Find out with the newest iPhone App from Katanaa: Cheapest. We submitted the app to the store last Friday. It will be a day or two before it’s available. Stay tuned.

Update: Cheapest is now available in the App Store: Get the Cheapest application from iTunes.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
Nov 13 2008

iPhone Electronic Formula Calculator

My latest adventure has been in the iPhone/iTouch application development world. The first application to hit the iTunes Store is Formula Sensei, a formula database and calculator. Formula Sensei will encompass a suite of formula calculators, the first of which is the Electronic Formula Calculator. This app has been developed and uploaded for review/approval to the iTunes store. Future editions of Formula Sensei will include financial, real estate, physics, etc. A web site has been set up to provide more information about the product:

formulasensei.com

Update: the E-Formulas app has now been approved for sale! See it at the iTunes Store: E-Formulas.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
Oct 07 2008

Apache Performance: Rotate your logs (duh)

Seems kinda silly, doesn’t it? After following all the Apache performance tips found on Google, I noticed that the site I was tuning (rss2.com) had access logs exceeding 2GB in size. Now if you imagine each httpd process having to load a file that size, you can imagine why it took so long for new httpd processes to load.

I configured logrotate to rotate logs each hour when the logs exceeded 100K. What a difference! Of course, restarting Apache every hour helps, too. But the change made a significant difference.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
Sep 07 2008

Stubborn Spider

Out on my deck, I had this spider that wove a web that must have been 2 feet in diameter. My first instinct was to tear down the web and squash the little bugger. But I had some crisis of Buddhist-like conscience when I thought about how long it took to create the web. Every night like clockwork, this spider weaves this web to catch its dinner. And every morning it tears it down to start all over again. It made me think about how humans get up every day and go to work to proverbially catch their own dinner. Sympathy for the spider?

Here are some pictures of its artwork:

I mean, I understand we need spiders to control the insect population. But why in my “backyard”? Go climb up someone else’s deck. I did become comfortable with the little guy, as long as I kept my distance. Could this be a metaphor for my ability to coexist with those that I find strange and threatening? I mean, it’s not like this itty bitty spider is a real threat to me. I’m more of a threat to it.

Eventually, the rain came and washed the spider down the spout. It did make a come-back one night. But since then, I haven’t seen my new friend. I will miss him.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
Sep 07 2008

Oops – Don’t Do This

Whenever I bring a new case of soda home, I like to stick one in the freezer to get it nice and cold. Except yesterday, I forgot about it and went to bed early. At around 4 in the morning, I woke up to a “pop” sound from the kitchen.

What a mess.

Not to mention, I picked up lemon-flavored diet.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
Sep 02 2008

JavaScript Keypress Event – the right way

I had an occassion where I had to capture the “enter” key press in a text box and couldn’t quite remember how to do that. So, like the well-adjusted web developer I am, I Google’d for the answer. I was suprised to find how many different solutions there were and how some of them just plain didn’t work.

I turned to the tried-and-true Prototype library (because that’s how I remembered doing it in the first place). The bonus with using Prototype is that it will actually be browser compatible.

Here is the penultimate solution to capturing an “enter” keypress in an HTML input text box.

The HTML:

<input type="text" name="my_text" id="my_text" value="" />

The JavaScript:

<script type="text/javascript"><!--
function onMyTextKeypress(event)
{
if (Event.KEY_RETURN == event.keyCode) {
// do something usefull
alert('Enter key was pressed.');
}
return;
}

Event.observe('my_text', 'keypress', onMyTextKeypress);
//-->
</script>

Now, don’t forget to include the prototype.js script in the HTML page!

<script type="text/javascript" src="/js/prototype.js"></script>

The JavaScript must execute after the DOM elements are rendered. One way to do it is to put the JavaScript code in a SCRIPT element after the INPUT element. However, another way would be to put the following code in the SCRIPT element in the HEAD element:

Event.observe(window, 'load', function() {
Event.observe(Event.observe('my_text', 'keypress', onMyTextKeypress);
});

I like this method because all the JavaScript can be kept in the HEAD, or in a JS library file, instead of splattering the code throughout the document body.

References:

Prototype Event.observe API

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
Jul 01 2008

PHPKeyStore Update

The KeyStore API is code complete. Check it out at phpkeystore.org. The current development release can always be installed with PEAR using:

pear install http://phpkeystore.org/download/KeyStore-current.tgz

All that really remains right now is internal tweaking for best practices and performance.

To summarize the functionality, the key management functionality consists of:

  • Loading and storing the key store
  • Creating secret keys, certificate signing requests, importing signed certificates, and deleting key store entries
  • Querying the key store for the existence of an entry and what type of entry it is

And the key usage functionality consists of:

  • Loading the key store
  • Using a public/private key pair to encrypt, decrypt, sign, and verify
  • Using a secret symmetric key to encrypt and decrypt

The current to-do list:

  • Add configuration file for system default values
  • Support file-based passwords
  • Support user-supplied options on the interface methods in order to support cryptographic functionality other than the default, baked-in settings
  • Add failure-case unit tests
  • Code review
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Twitter