Mar 19 2012

7-Step Crash Course in AJAX Programming With PHP and XAJAX

This is for the web programming geeks out there who have wanted to do AJAX in PHP but it seems too daunting.  I just added some AJAX functionality to a personal page/project, and would like to show you how easy it is.

For this example, we’ll be building a page where you can enter text, click a button, and have that text show up in a different element.  Here are the steps (caveat: I’m keeping it simple and not doing any sort of input sanitization or anything):

  1. Download xajax 0.5 final (minimal version) from here. (Direct link)
  2. Copy it to your webserver.  For this example, let’s say your PHP file is in your webroot, and you put the xajax files into a subfolder called xajax (so under that you would have directories for xajax_core, and xajax_js).
  3. Toward the top of your PHP code, add these lines:
    require_once('xajax/xajax_core/xajax.inc.php');
    $xajax = new xajax();
  4. Somewhere before your script actually starts outputting anything (even HTML headers and the like), add the following line:
    $xajax->processRequest();
  5. In the <head> section of your HTML, include the following line (the value in parenthesis is the directory where you put xajax_core and xajax_js):
    <?php $xajax->printJavascript('xajax/'); ?>
  6. Make a PHP function and register it with XAJAX like so (color coded to show where various inputs are used):
    $xajax->registerFunction('my_function');
    function my_function($el_id, $text) {
        // Do some PHP stuff here -- database access, whatever
        $response = new xajaxResponse();
        $response->assign($el_id, 'innerHTML', $text);
        return $response;
    }
  7. Call your PHP function via JavaScript — magic!  Here is some HTML code.
    Type something here: <input type="text" id="my_input" /><br>
    <input type="button"
        onClick="xajax_my_function('my_target_element_id', \
        document.getElementById('my_input').value)">Click me!</button><br>
    <span id="my_target_element_id">Here is some text that will change
    when you click the button above</span>

And that’s all there is to it! The xajax library takes care of all the dirty work.  The ending file from start to finish looks like this:

<?php
require_once('xajax/xajax_core/xajax.inc.php');
$xajax = new xajax();
$xajax->registerFunction('my_function');
function my_function($el_id, $text) {
    $response = new xajaxResponse();
    $response->assign($el_id, 'innerHTML', $text);
    return $response;
}
$xajax->processRequest();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" \
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <?php $xajax->printJavascript('xajax/'); ?>
</head>
<body>
  Type something here: <input type="text" id="my_input" /><br>
  <input type="button" onClick="xajax_my_function('my_target_element_id', \
      document.getElementById('my_input').value)">Click me!</button><br>
  <span id="my_target_element_id">Here is some text that will change
  when you click the button above</span>
</body>
</html>

Jan 31 2012

Windows/Apache Sysadmin – Fixing the unadvertised speed limit

This is a post for any Windows sysadmins out there who administrate a server using Apache and PHP.

For a while now, our Windows download servers at work have had some transfer speed issues.  I would turn off all external traffic and try downloading a file directly, and it went nowhere near as quickly as it should have.  Turns out, there were 2 problems.

Problem 1: PHP readfile() apparently sucks

The first thing I noticed was that directly downloading a file via Apache was much faster than using PHP to pass it through with readfile() for some reason.  I fretted for a bit and ended up just doing this:

// Sends a file using fread, 16K at a time
function my_send_file($filename) {
$fp = fopen($filename, “rb”);
//start buffered download
if ($fp) {
while(!feof($fp)) {
print(fread($fp,1024*16));
flush(); ob_flush();
}
fclose($fp);
}
}

That made downloads go about 50% faster, but it still wasn’t near where it should be for some reason.  I left well enough alone for a while until it piqued my curiosity again, and lo and behold I found a few hits on Google!

Problem 2: Windows ignores Apache SendBufferSize setting

This is a rather annoying bug.  The only way to fix it is to edit the registry thusly:

  1. Open up regedit, then go to HKEY_LOCAL_MACHINE > SYSTEM > CurrentControlSet > Services > AFD > Parameters
  2. Create two DWORD values called “DefaultReceiveWindow” and “DefaultSendWindow”
  3. You then set both these values using DECIMAL (not Hex) using this formula:
    DefaultReceiveWindow = (Download Capacity in Kbps * 1024) / 8
    DefaultSendWindow = (Upload Capacity in Kbps * 1024) / 8
    For example, for a 10Mbps (~10000 Kbps) upload and download:
    DefaultReceiveWindow = (10000 * 1024) / 8 = 1280000
    DefaultSendWindow = (10000 * 1024) / 8 = 1280000
  4. Reboot and test your Apache speed.

We saw a 4-6x increase in speed after doing this. It’s possible that problem 1 above was related to this, so maybe after doing this readfile() would work just as well as fread().  But it ain’t broke now, so I’m not going to fix it.


Aug 25 2011

Why Steve Jobs Stepping Down Could be Good For Apple

Yesterday, Steve Jobs resigned as CEO of Apple, volunteering himself to be chairman of the board instead. I predicted that their stock would plummet, but my company’s president (also named Steve!) said he didn’t think the stock would drop much because Apple “graduated everyone into the idea that he’s out.” Turns out, Steve was right — guess there’s a reason he’s in the driver’s seat.

In any case, I think this could be great for Apple. Actually, when Jobs leaves completely, I think Apple could make some great strides forward. How, you ask?

Because they won’t have Jobs’s ego holding them back. As one big example, HTML 5 is not replacing Flash, sorry buddy. Flash does not drain batteries like the filthy whore that Jobs makes it out to be (as proven by Flash running fine on hacked iPhones).  If iOS devices were to support Flash, that would knock out the main advantage that Android devices have*.  And hey, maybe they’ll be more willing to admit to mistakes too.  Remember that whole iPhone 4 antenna issue and Steve Jobs’s response to it?  Yeah.

Edit: It has been pointed out that I only used the example of Flash, which does not a valid argument make.  So as another example, Apple has a somewhat-recent policy of disallowing apps from linking to their own websites where people can buy stuff.  This, I feel, comes from the Jobs-like mentality of, “We’re the best, if you want in on our platform you play by our rules and give us a cut of everything that happens on our device.”  However, this will ultimately weaken them as companies turn instead to web apps, as Amazon recently did with their Cloud Reader. This gets consumers more comfortable with using web apps, and companies soon realize, “Hey, you mean I only have to pay to develop this once, and it will work on any mobile device with a browser — including both Apple products and Android products?  Sweet!”  More companies do that, meaning iOS has fewer exclusive apps, and consumers have a more viable choice in alternatives.

In general, I feel that Jobs has an attitude of, “We are the market leaders, therefore the market goes where we say, not the other way around.  We tell people what they want; they do not tell us what they want.”  Which, to some extent, is true.  But that level of ego also blinds one to their own weaknesses, which is to Apple’s detriment.

* I’m sure Android devices have many other awesome advantages, but from an average non-tech-savvy average Joe perspective, Flash is really the one functional thing missing from iPhones and iPads that Android has.


Jul 9 2011

How I Plan to Use Google+ Circles

Google+ is the shiny new toy over the past week or so.  It is very Spartan compared to Facebook, which is its strength.  It is also very flexible in how you can use it… which I fear may be its downfall (those who remember Wave will be familiar with this).  Circles are cool things — they’re like Facebook friend lists, but instead of being an afterthought as in Facebook, they are a core part of the design of Google+.

I plan to split my circles into two types: “broadcasting” and “receiving.”

Broadcasting circles will contain groups of people whom I want to broadcast certain types of information to.  For example, many people in my friend list really don’t care about my latest Exalted game, but the gamers might.  So I have a circle called Gamers that I will post that sort of thing to.  Basically, the sorts of people that if I would talk to these sorts of things about if I were in front of them face-to-face.  My friend Sheena has even publicly posted a list of her circles and asked people which they would like to be in, which I think is a great idea (maybe I’m into knitting and she just never knew!).  I’m going to post mine on my public profile.

Receiving circles will be groups that I want to listen to for different topics.  I am thinking this will be a much smaller set of circles.  Probably only two, actually: one that I pay attention to always, one that I pay attention to sometimes, and the third implied group of everyone.

What is the purpose of this?

The “broadcasting circles” are to help combat information overload.  One of my main issues with Facebook is that there is a lot of information overload on there: I have to wade through tons of posts that I simply don’t care about in order to get to the few that I do.  So to help alleviate this for others, I plan to be specific about what I share with whom.

The receiving circles is to help combat information overload from the other end.  I’ve just got started with Google+ and I have nearly 150 people on my list.  If all of them were to use the above philosophy of broadcasting circles then my stream would probably be manageable.  However, I don’t realistically expect that — many will probably post publicly (or to all of their Circles) by default.  Which is no knock against them; I realize not everyone is going to see circles the same way that I do.

What are your thoughts on this?  How do you plan to organize your circles?


Jun 25 2011

Installing iOS 5 Beta 2 in Windows

Apple pretty much assumes that everyone who is using their iOS beta has a Mac — which is a reasonable assumption, because the beta is really more for developers to make their application work with iOS 5 than for nerds like me who just want to use the features before they’re publicly available.  But I digress.

Since Apple doesn’t really provide any instructions or support for installing iOS 5 using a PC, I will tell you how.

  1. Download and install iTunes 10.5 Beta 2 from here (you will need to have a valid Apple developer account, which costs $99/year).  This step is very important! Even if you downloaded 10.5 Beta 1 to install iOS 5 Beta 1, you still need to download Beta 2, or the upgrade won’t work.
  2. Download iOS 5 Beta 2 from here (again, you’ll need a valid Apple developer account).
    • Make sure to download the correct version for your device. If you’re on AT&T, download the GSM version; if you’re on Verizon, download the CDMA version.
  3. Using 7-Zip, unzip the .dmg file, then unzip the file “2.hfs” that is inside of that.
    • Inside of that, there should be a file named something like “iPhone3,1_5.0_9A5248d_Restore.ipsw”.  Note its location.
  4. Plug your iPhone into your computer and start up iTunes if it does not start automatically.
  5. Sync your iPhone to make sure that you have all the data on it backed up.
  6. Shift+click on Restore, then navigate to that .ipsw file from step 3 and select it.
  7. Restore your phone.
  8. After restoring using that file, you will need to restore again using the backup from step 5.  This will take some time, but it will add all of your contacts, text messages, songs, apps, etc. back onto your phone.

And then you’re done! The only real glitch I’ve noticed is that during the last step, some applications seem to get lost in the ether, so you may have to reinstall them. You may also have to re-enter email passwords and the like.


Jun 17 2011

Upgrade Your iPhone 4 to iOS 5 Beta in 4 Easy(ish) Steps

Important Note: This is for iOS 5 Beta 1. I will provide an article for iOS 5 Beta 2 soon, though currently I am not sure if the workaround for non-developers listed below works for Beta 2.

There aren’t many good tutorials on this out there, so I’m making one. I actually made the instructions for my girlfriend to start out with, but I figured some other people out there might be interested too.

Caveat 1: I am not responsible for any damage/bricking/etc. that may occur from this — iOS betas are generally pretty stable, but sometimes bad things happen, so don’t blame me if they do.

Caveat 2: The beat is really only supposed to be for Apple developers. It will be buggy, and I have already found several bugs, in fact.

Caveat 3: It is possible that with the next release of the beta, Apple will patch any holes that allow non-developers to have access to this, which could brick your phone (restoring to a previous iOS is very hairy). If that happens, please do contact me and I’ll do my best to help you fix it.

Okay, with that out of the way, here we go! Note that this tutorial will also work for the iPad and the iPhone 3GS, but you will need to download a different IPSW file to do that, as mentioned above under step 3. Also this tutorial is assuming you’re using Windows, but there’s really not much difference (except that Apple doesn’t directly offer iTunes 10.5 even to developers as far as I can tell).

  1. Back up your iPhone using iTunes.  Just do a normal sync operation, this should handle it.
    • Just to be extra sure, you might want to copy that backup to a different location. You can find this backup using these instructions.
  2. Install iTunes 10.5 beta, from here (if that doesn’t work for some reason, there are other links listed here under “iTunes 10.5 (Windows x64(64-bit, 74.07MB)”).
    • If you have a developer account and want to get it in a more authorized way, you can — the Windows version is well-hidden under the iCloud downloads. iTunes 10.5 Windows Beta.
  3. Download the iOS 5 beta torrent here (or again, if you have an Apple developer account, just get it from there but see the third note below if you do that).
    • For AT&T choose the GSM version; for Verizon choose CDMA.
    • If you don’t already have a BitTorrent client, I recommend uTorrent.
    • If you do a Google search and get it from elsewhere (or from Apple’s developer site) as a .dmg file, you might need a special program to unzip that; go ahead and install 7Zip to do that. After you unzip it, go into that folder and unzip the file “2.hfs”.  That should have a directory named “iOS 5 beta – iPhone 4 – 9A5220p” which will contain the file iPhone3,1_5.0_9A5220p_Restore.ipsw — that’s what we’re interested in.
  4. Follow instructions here (basically fire up iTunes, hold down Shift while you click the Check for Update button, and select the IPSW file you unzipped above).

Tada!  Your phone will go through a few reboots, you will be prompted to activate it, and at that point you’ll be really scared because none of your apps, contacts, music, etc. will be on there.  Never fear — just run a restore from that backup you made in step 1, and you’re golden.

Happy upgrading!  And please, if any of this doesn’t work for you, or any links are broken, let me know.


Dec 3 2010

Another test post

One more test post.


Nov 22 2010

iPhone Features I Want to See

With today’s release of iOS 4.2, i would like to comment on a few basic features that are missing from the iPhone experience.

Voice text entry. Anywhere there is a text entry field, I should have the option to press a microphone icon to enter text via voice. I think this feature could actually save lives, and lots of dollars’ worth of car repairs, for people who just can’t seem to stop texting while driving*, even though it’s illegal in many states now. It seems silly to me that iPhones already have voice recognition for spoken commands (like calling someone, playing a certain song, etc.), but not for general text entry. This would be useful for other things too, like getting directions while you’re already on the road — I actually use Bing’s navigation app sometimes, specifically because I can search and enter addresses via my voice.

Text to speech. Likewise, I would like built-in functionality to have text messages, or other simple things (like emails), read to me. Not necessarily web pages or anything complex like that, though — I know that would be nearly impossible to do well for anything other than very simple web pages.

If this were Android, there could be an app to do both of these things, but apps don’t have access to text messages on the iPhone. Well, actually Android already has voice transcription for most places where you can enter text, from what I’ve seen. Maybe I should install Android on my iPhone. However, come to think of it, the Google Voice app could fairly easily add both of these things (for text messages), if Google wanted.

Better photo management/organization. I would also like the ability to save pictures to something other than the default album, and the ability to move items between albums. I’m not sure why this wasn’t possible from the initial version of the iPhone.

I fully expect all three of these things to be implemented by Apple… eventually. Sooner rather than later would be nice, though. And the speech-to-text feature may indeed be sooner, since there are rumors that Apple recently purchased Nuance, the makers of the voice recognition software Dragon Naturally Speaking (and also the makers of a free voice recognition app for the iPhone).

* Yes, I realize this is still distracted driving, which is also very dangerous, but it’s not quite so dangerous as actually taking your eyes off the road for a few seconds at a time.


Nov 19 2010

World of Warcraft “Patch as you play” Feature

This post was actually drafted a while ago… I just totally forgot to post it when I wrote it.

Long story short, things randomly screwed up and I have to reinstall WoW to be able to play it.  Boo, Blizzard.

However, in the process of doing this, I am getting to check out the option to “play as you patch.” Basically, when your client is not up to the latest version, it prioritizes and installs as you update. I was dubious about this… until now. when I reinstalled, it put in the basic files, and is now patching up.  I’ve got about 3% of the whole patch… and I’m able to play. This is pretty magical.

Also, I have had almost a full bottle of wine. So objects in your blog post may be less magical than they appear.

Anyway, this is actually very awesome. I have the minimum required to play, which means when I logged in, things started spontaneously appearing around me. Like the rug under my feet. Actually, when I first went to log in, I couldn’t even see my character.

I’m queued for a dungeon run. This should be… interesting.

Under the normal loading bar, I saw a sub-loading bar for the first time, which I guess was loading things necessary to the area my character was in. Screenshot below, click for the bigger version, in which you can see the secondary loading bar better.

OMG I’m in a dungeon run now. afk. Not that you can tell I’m afk, since this is a blog post after all. Hmm, maybe not. It’s downloading the dungeon on the fly, which is taking a minute. And now everyone is dead and I’m partway through Drak’Tharon Keep. This could be a bad sign. Or… perhaps not. Rolled it.

Anyway, this whole endeavor has been surprisingly pain-free. I can’t use my add-ons, though that’s expected. Things take a little longer to load since they load on the fly — also expected. But I’ve seen no major glitches, and was just able to run a dungeon as game data was still being applied. This is very cool. I can only imagine that the system to do this is incredibly complex, and it’s rare that such things work in a smooth way. So on that count, bravo, Blizzard.


Nov 3 2010

Updates: Reinstalled Blog, Second Home

Part of the reason for my pause in posts has been that at some point my site got hacked. It was probably due to an installation of an old bit of software that had a security flaw — I had a lot of stuff installed that I never really kept up with. In any case, I’ve wiped my entire site and recreated it, which was a bit of effort. I think I’ve still got the Livejournal crossposting working correctly, so this will be a test of that too.

In other news, I’m looking at buying a second home in Decatur. Found a deal that seemed too good to be true, but on a lark I contacted Rich Murray, a friend of mine who happens to be a kick-ass realtor. He said that it’s a short-sell, and normally at the dirt-cheap price it’s listed at the bank wouldn’t accept it, except in this case a nearly identical property sold for the same asking price.

However, there is a big bureaucratic hurtle: it has to be owner-occupied. I would actually like to rent out my house and move in there, but that’s irrelevant; when working up the loan, they will look at the mortgage amount on both properties and assume I’ll be living in the more expensive one, which is my house (by far). The value of both properties is actually about the same, but the mortgage is far different — my house has gone down in value a lot since I bought it.

So, it’s unlikely this will go through. I’m not getting my hopes up too much, and trying to remind myself that if it does go through it’ll be a pain to move and find someone to rent out my house, not to mention act as a landlord (though I could always have a third party deal with all of that). But man, this is the exact area I want to be in Atlanta, so I am still going to try my damndest.