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 article, leaving a comment, tweeting ofr liking it 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
Thursday 2 October 2008 8:48 am
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.
Wednesday 5 August 2009 12:24 am
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]
Monday 26 April 2010 5:28 am
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