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:
- Put my
ciopenid.phplibrary in your/system/application/librariesdirectory. - Create
/system/application/libraries/openid, and unzip PHP_OpenID 2.x.x’sAuthdirectory in it. - Open your
/system/application/config/config.phpfile, and make sure that hooks are enabled:$config['enable_hooks'] = TRUE;
- Open your
/system/application/config/hooks.phpfile, 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…
Enjoy!
If you like this acrticle, leaving a comment, Digging it, adding it to your del.icio.us bookmarks is always appreciated.
Friday 4 July 2008 12:37 pm
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!
Friday 4 July 2008 1:15 pm
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