Select Page

I got no IPod. I got no IPhone. But I’ve got an OpenID. I’m like a low class geek. But whatever. I’ve read a bit lately about OpenID and wanted to give it a try on one of my personal projects (CodeIgniter based, you bet).

I’ve stumbled upon Rémi Prévost — who’s blog a follow more or less consistently due to the lack of time — CIOpenID module. It’s basically a CI embed of Janrain’s PHP_OpenID.

His code is great and works well. But one thing I don’t like about it is that it requires a different bootstrap file (a modified version of index.php) and a somewhat hacked version of your typical .htaccess file. The reason is, CodeIgniter annihilates the $_GET variable during initialization, because GET queries aren’t secure (ok, this is overly simplified, but you get the idea).

Being who I am, and somewhat liking to reinvent the wheel during my free time, I used a different approach. Rather than using a different bootstrap file, I rather chose to use the pre_system hook.

The installation process is quite simple:

  1. Put my ciopenid.php library in your /system/application/libraries directory.
  2. Create /system/application/libraries/openid, and unzip PHP_OpenID 2.x.x’s Auth directory in it.
  3. Open your /system/application/config/config.php file, and make sure that hooks are enabled:
    $config['enable_hooks'] = TRUE;
  4. Open your /system/application/config/hooks.php file, and add the following lines:
    $hook['pre_system'][] = array(
       'class'    => 'ciopenid',
       'function' => 'pre_system_hook',
       'filename' => 'ciopenid.php',
       'filepath' => 'libraries',
       'params'   => null
       );

To make an authentication query to an OpenID URL in $openid:

$this->load->library('ciopenid');
try {
   $redirect_url = $this->ciopenid->authenticate($openid, site_url($this->uri->uri_string()));
   header("Location: ".$redirect_url);
} catch (Exception $e) {
   die('Error: '.$e->getMessage());
}

And to get the OpenID query response:

$this->load->library('ciopenid');
if (isset($this->ciopenid->response)) {
   switch ($this->ciopenid->response->status) {
      case Auth_OpenID_SUCCESS:
         $openid = $this->ciopenid->response->endpoint->claimed_id;
         print 'Success: '.$openid;
         break;
      case Auth_OpenID_CANCEL:
         print 'User cancelled';
         break;
      default:
         print 'Internal error';
         break;
   }
}

That’s it! Ooops, here’s the file…

PHP Source (zipped) ciopenid.zip (3k)

Enjoy!