Archive for May, 2009

Getting Facebook User Info With PHP and Facebook Connect

Recently for a site I’ve been working on we wanted to have the ability to track a great deal of user activity without them having to go through a “Register User” process. Facebook Connect was a logical choice since almost everyone uses it now especially the target market for our site.  Facebook Connect is very easy to implement however most of the tutorials and beginners guides have you use javascript. If you just want a user to comment or do other simple actions, this works great but we needed more. That’s when I was pleasured to hear they have PHP library too!

require "library/facebook-platform/client/facebook.php";
$facebook = new Facebook("your app key", "your app secret");
$uid = $facebook->get_loggedin_user(); // will return users id if connected and logged in to your app*
$userDetails = $facebook->api_client->users_getInfo($uid, array ("last_name", "first_name", "name", "pic"));
$pic = $userDetails[0]["pic"];

The above will set the user’s Facebook Profile Pic to $pic. Making a class of my own out all these pulled details, I soon had everything I need easily at hand and I could integrate them right into our site almost right away.

I would suggest taking the time to adjust to the PHP library for anyone wanting to implement Facebook Connect. I feel it is much more versatile and you won’t have to rely on the fact your user has javascript enabled.

Generate Random Strings in PHP

I needed to generate some random strings today for password resets.  Here’s what I used:

function genRandomString($length = 10)
{
   $characters = ‘0123456789abcdefghijklmnopqrstuvwxyz’;
   $string =;
 
   for ($p = 0; $p < $length; $p++)
   {
      $string .= $characters[mt_rand(0, strlen($characters))];
   }
 
   return $string;
}

Or, recursively:

function genRandomString($i, $length = 10)
{
   $characters = ‘0123456789abcdefghijklmnopqrstuvwxyz’;
   $string =;
 
   if($i >= $length)
   {
      return '';
   }
   return $characters[mt_rand(0, strlen($characters))].genRandomString($i++, $length);
}

Upgrading Wordpress MU from 2.6 to 2.7

I know I’m late to the game but I tried upgrading wordpress from version 2.6 to 2.7 today.  I ran into the seemingly all-too-common redirect loop which seems to have to do with the wp-config.php keys and salts.

Thanks to a comment on the wordpress forums, I was able to find a resolution, which was to remove the block of KEYS and SALTS entirely from the wp-config.php file.  Then, upon login, MU notifies you that your keys are incorrect and asks you to update them.  A simple fix to something that I thought would be a major problem.

I have to say, Wordpress 2.7 is significantly better than 2.6 AND 2.7 MU is even better than that if you are running multiple blogs.  What fun!