How to make login system for CodeIgniter using OpenID. This example uses Google Accounts for login.
Basically we need library that communicates with oogle. Then we need to adjust CodeIgniter for it. Then we get unique url that is id for your user, or you can make it secondary value so primary autoincreased int is id.
Getting The Library
I personally like LightOpenID witch is released under MIT license. Then we need to copy openid.php form the arcive and place it in our libraries folder. Also change the file name to Openid.php and the file contains class that is called to LightOpenID, cange that to Openid aswell. You can also take a look at example-google.php, because that is what we are going to do, but in CodeIgniter.Query Strings:
This library needs query strings, but for SEO reasons we want to keep pritty urls for our site. So we need a little hack. Go to your config/config.php file in application folder. Don't enable query strings, but change uri_protocol to PATH_INFO. Then you can use this line in your login controller to enable query strings only for that controller.Source code viewer
class Login extends Controller { function __construct() {#<-PHP5 - __construct(); PHP4 - class name() parent::Controller(); #enable query strings for this class } ... }Programming Language: PHP
CodeIgniter OpenID Google:
This is a function I made, based on example-google.php file from the archive. This function only validates user and gets unique user id, how you save the session is up to you. So basically, how you make the login is up to you. But here is the function:
This is a function I made, based on example-google.php file from the archive. This function only validates user and gets unique user id, how you save the session is up to you. So basically, how you make the login is up to you. But here is the function:
Source code viewer
function google_openid(){ $this->load->library('openid'); try { $openid = new Openid; $openid->identity = 'https://www.google.com/accounts/o8/id'; } elseif($_GET['openid_mode'] == 'cancel') echo 'User has canceled authentication!'; else { $openid = new Openid; if($openid->validate()){ #Here goes the code that gets interpreted after successful login!!! } else echo 'User has not logged in.'; } } catch(ErrorException $e) { echo $e->getMessage(); } }Programming Language: PHP