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!


If you like this article, leaving a comment, tweeting ofr liking it is always appreciated.

category PHP, Social networks Wednesday 25 June 2008 Comments (5)

5 Responses :

  1. Rémi Prévost

    Whoa, thanks for improving my code! I never really had the time to make it better — that and also the fact that I don’t like CI anymore :)

    Good luck!

  2. tlacroix

    Hehe, yeah, I should give a shot to Kohana too, but no time and there was a lack of doc the last time I checked. Anyhow, thanks for stopping by :-)

  3. kyaw kyaw naing

    Thank you very much.

    CodeIgniter is very cool. It is a good citizen, working nicely with so many other frameworks/modules.
    No doubt, Rasmus recommends it, next to his “no-framework” framework.

    Thanks again.

  4. Jeroen

    After spending the whole day looking for why this didn’t work on my machine I figured out that in the file libraries/ciopenid.php, in the function [code]pre_system_hook()[code], the call to [code]$this->OpenIDConsumer->complete[/code] needs an attribute called current_url.

    This value should be equal to the value openid_return_to which is returned by the OpenId server.

    I hacked it as follows:

    [code]
    $return_url = $_GET['openid_return_to'];

    // Save response
    $this->response = $this->OpenIDConsumer->complete($return_url);
    [/code]

  5. tuanka

    Thank you for your post!

    I’ve tried this and got the following error: Cannot modify header information – headers already sent by (output started at C:\Program Files\xampp\htdocs\groupon\system\libraries\Exceptions.php:166).
    Can you solve this for me? thanks

Leave a Reply