Anda di halaman 1dari 57

License

Ion Auth is released under the Apache License v2.0. You can read the
license here: http://www.apache.org/licenses/LICENSE-2.0

Installation

1. Download the latest version: http://github.com/benedmunds/CodeIgniter-


Ion-Auth/zipball/2

2. Copy the files from this package to the correspoding folder in your
application folder. For example, copy Ion_auth/config/ion_auth.php to
system/application/config/ion_auth.php.

3. You can also copy the entire directory structure into your third_party/
folder. For example, copy everything to /application/third_party/ion_auth/

4. Run the appropriate SQL file from the /sql directory.

The default login is:

Email: admin@admin.com

Password: password

Upgrading

1. Download the latest version: http://github.com/benedmunds/CodeIgniter-


Ion-Auth/zipball/2
2. Overwrite "libraries/ion_auth.php" and "models/ion_auth_model.php" with
the new versions.

Loading Ion Auth

You load Ion Auth just like any other library:

$this->load->library('ion_auth');

You can also autoload the library.

Configuration Options

Ion Auth is extremely configurable. The following configuration options


are available:

$config['tables']['groups']

$config['tables']['users']

$config['tables']['users_groups']

$config['tables']['login_attempts']

$config['site_title']

$config['admin_email']

$config['default_group']

$config['admin_group']

$config['join']['users']

$config['join']['groups']
$config['identity']

$config['min_password_length']

$config['max_password_length']

$config['email_activation']

$config['remember_users']

$config['user_expire']

$config['user_extend_on_login']

$config['email_type']

$config['email_templates']

$config['email_activate']

$config['email_forgot_password']

$config['email_forgot_password_complete']

$config['salt_length']

$config['store_salt']

$config['forgot_password_expiration']

$config['track_login_attempts']

$config['maximum_login_attempts']

$config['message_start_delimiter']

$config['message_end_delimiter']

$config['error_start_delimiter']

$config['error_end_delimiter']
Using Config File

To change configuration options simply edit the config/ion_auth.php file.

Config

Edit the ion_auth $config array as needed:

'tables['groups']' - The table name to use for the groups table. DEFAULT is
'groups'.

'tables['users']' - The table name to use for the users table. DEFAULT is
'users'.

'tables['users_groups']' - The table name to use for the users groups table.
DEFAULT is 'users_groups'.

'tables['login_attempts']' - The table name to use for the login attempts


table. DEFAULT is 'login_attempts'.

'site_title' - The title of your site, used for email.

'admin_email' - Your administrator email address. DEFAULT is


'admin@example.com'.

'default_group' - Name of the default user group. DEFAULT is 'members'.

'admin_group' - Name of the admin group. DEFAULT is 'admin'.

'join['users'] ' - Users table column you want to join WITH. DEFAULT is
'user_id'.

'join['groups'] ' - Group table column you want to join WITH. DEFAULT is
'group_id'.
'identity' - Column to use for uniquely identifing user/logging in/etc. Usual
choices are 'email' OR 'username'. You should add an index in the users
table for whatever you set this option to. DEFAULT is 'email'.

'min_password_length' - Minimum length of passwords. DEFAULT is '8'.

'max_password_length' - Maximum length of passwords. DEFAULT is '20'.

'email_activation' - TRUE or FALSE. Sets whether to require email activation


or not. DEFAULT is 'false'.

'remember_users' - TRUE or FALSE. Sets whether to enable 'remember me'


functionality or not. DEFAULT is 'true'.

'user_expire' - Sets how long to remember the user for in seconds. Set to
zero for no expiration. DEFAULT is '86500'.

'user_extend_on_login' - TRUE or FALSE. Extend the users session expiration


on login. DEFAULT is 'false'.

'email_type' - Email content type. DEFAULT us 'html'.

'email_templates' - Folder where the email view templates are stored.


DEFAULT is 'auth/email/'.

'email_activate' - Filname of the email activation view template. DEFAULT is


'activate.tpl.php'.

'email_forgot_password' - Filname of the forgot password email view


template. DEFAULT is 'forgot_password.tpl.php'.

'email_forgot_password_complete' - Filname of the forgot password


complete email view template. DEFAULT is 'new_password.tpl.php'.

'salt_length' - Length of the encryption salt. DEFAULT is '10'.


'store_salt' - TRUE or FALSE. Store the salt in a separate database column or
not. This can be useful for integrating with existing apps. DEFAULT is 'false'.

'forgot_password_expiration' - Number of seconds before a forgot


password request expires. If set to 0, requests will not expire. DEFAULT is 0.

'track_login_attempts' - Track the number of failed login attempts for each


user or ip. DEFAULT is 'false'.

'maximum_login_attempts' - Set the maximum number of failed login


attempts. This maximum is not enforced by the library, but is used by $this-
>ion_auth->is_max_login_attempts_exceeded(). The controller should
check this function and act appropriately. If set to 0, there is no maximum.
DEFAULT is 3.

'message_start_delimiter' - Starting delimiter for messages. DEFAULT is


'<p>'.

'message_end_delimiter' - Ending delimiter for messages. DEFAULT is '</p>'.

'error_start_delimiter' - Starting delimiter for errors. DEFAULT is '<p>'.

'error_end_delimiter' - Ending delimiter for errors. DEFAULT is '</p>'.

Compatibility with CodeIgniter v2

CodeIgniter v2 requires the class file names to be lowercase. In order to


support this follow the standard installation procedures and then either
rename the following files or create symlinks:
models/Ion_auth_model.php => models/ion_auth_model.php
controllers/Auth.php => controllers/auth.php

Class Function Reference

NOTE: Methods available in the model are called through the controller
using PHP5 magic. You should never use ion_auth_model->method() in your
applications.

login()

Logs the user into the system.

Parameters

1. 'Identity' - string REQUIRED. Username, email or any unique value in your


users table, depending on your configuration.

2. 'Password' - string REQUIRED.

3. 'Remember' - boolean OPTIONAL. TRUE sets the user to be remembered if


enabled in the configuration.

Return

boolean. TRUE if the user was successfully logged in FALSE if the user was
not logged in.

Usage
$identity = 'ben.edmunds@gmail.com';

$password = '12345678';

$remember = TRUE; // remember the user

$this->ion_auth->login($identity, $password, $remember);

logout()

Logs the user out of the system.

Usage

$this->ion_auth->logout();

register()

Register (create) a new user.

Parameters
1. 'Identity' - string REQUIRED. This must be the value that uniquely identifies
the user when he is registered. If you chose "email" as $config['identity'] in
the configuration file, you must put the email of the new user.

2. 'Password' - string REQUIRED.

3. 'Email' - string REQUIRED.

4. 'Additional Data' - multidimensional array OPTIONAL.

5. 'Group' - array OPTIONAL. If not passed the default group name set in the
config will be used.

Return

mixed. The ID of the user if the user was successfully created, FALSE if the
user was not created.

Usage

$username = 'benedmunds';

$password = '12345678';

$email = 'ben.edmunds@gmail.com';

$additional_data = array(

'first_name' => 'Ben',

'last_name' =>

'Edmunds',

);

$group = array('1'); // Sets user to admin.


$this->ion_auth->register($username, $password, $email, $additional_data, $group)

create_user()

create_user is an alternate method for register() method.

update()

Update a user.

Parameters

1. 'Id' - integer REQUIRED.

2. 'Data' - multidimensional array REQUIRED.

Return

boolean. TRUE if the user was successfully updated FALSE if the user was
not updated.

Usage

$id = 12;
$data = array(

'first_name' => 'Ben',

'last_name' => 'Edmunds',

'password' => '123456789',

);

$this->ion_auth->update($id, $data)

delete_user()

Delete a user.

Parameters

1. 'Id' - integer REQUIRED.

Return

boolean. TRUE if the user was successfully deleted FALSE if the user was not
deleted.

Usage

$id = 12;

$this->ion_auth->delete_user($id)
forgotten_password()

Resets a users password by emailing the user a reset code.

Parameters

1. 'Identity' - string REQUIRED. (as defined in config/ion_auth.php)

Return

boolean. TRUE if the users password was successfully reset FALSE if the
users password was not reset.

Usage

- this example assumes you have 'email' selected as the identity in


config/ion_auth.php

//Working code for this example is in the example Auth controller in the github repo

function forgot_password()

$this->form_validation->set_rules('email', 'Email Address', 'required');

if ($this->form_validation->run() == false) {

//setup the input


$this->data['email'] = array('name' => 'email',

'id'

);

//set any errors and display the form

$this->data['message'] = (validation_errors()) ?

validation_errors() : $this->session->flashdata('message');

$this->load->view('auth/forgot_password', $this->data);

else {

//run the forgotten password method to email an activation code

to the user

$forgotten = $this->ion_auth->forgotten_password($this-

>input->post('email'));

if ($forgotten) { //if there were no errors

$this->session->set_flashdata('message', $this-

>ion_auth->messages());

redirect("auth/login", 'refresh'); //we should display a

confirmation page here instead of the login page

}
else {

$this->session->set_flashdata('message', $this-

>ion_auth->errors());

redirect("auth/forgot_password", 'refresh');

forgotten_password_complete()

Final step of resetting a users password. The user comes to this page from
their email.

Parameters

1. 'Code' - string REQUIRED.

Return

string. The users new password.

Usage

//Working code for this example is in the example Auth controller in the github repo
public function reset_password($code)

$reset = $this->ion_auth->forgotten_password_complete($code);

if ($reset) { //if the reset worked then send them to the login page

$this->session->set_flashdata('message', $this->ion_auth-

>messages());

redirect("auth/login", 'refresh');

else { //if the reset didnt work then send them back to the forgot password

page

$this->session->set_flashdata('message', $this->ion_auth-

>errors());

redirect("auth/forgot_password", 'refresh');

logged_in()
Check to see if a user is logged in.

Return

boolean. TRUE if the user is logged in FALSE if the user is not logged in.

Usage

if (!$this->ion_auth->logged_in())

redirect('auth/login');

is_admin()

Check to see if the currently logged in user is an admin.

Parameters

1. 'id' - integer OPTIONAL. If a user id is not passed the id of the currently


logged in user will be used.

Return

boolean. TRUE if the user is an admin FALSE if the user is not an admin.

Usage
if (!$this->ion_auth->is_admin())

$this->session->set_flashdata('message', 'You must be an admin to view

this page');

redirect('welcome/index');

in_group()

Check to see if the currently logged in user is in the passed in group.

Parameters

1. 'Group ID or Name' - string, integer or array of strings and integers


REQUIRED.

2. 'User ID' - integer OPTIONAL. If a user id is not passed the id of the currently
logged in user will be used.

Return

boolean. TRUE if the user is in any of the given groups, FALSE otherwise.

Usage
# single group (by name)

$group = 'gangstas';

if (!$this->ion_auth->in_group($group))

$this->session->set_flashdata('message', 'You must be a gangsta to view

this page');

redirect('welcome/index');

# single group (by id)

$group = 1;

if (!$this->ion_auth->in_group($group))

$this->session->set_flashdata('message', 'You must be part of the group 1

to view this page');

redirect('welcome/index');

# multiple groups (by name)

$group = array('gangstas', 'hoodrats');


if (!$this->ion_auth->in_group($group))

$this->session->set_flashdata('message', 'You must be a gangsta OR a

hoodrat to view this page');

redirect('welcome/index');

# multiple groups (by id)

$group = array(1, 2);

if (!$this->ion_auth->in_group($group))

$this->session->set_flashdata('message', 'You must be a part of group 1

or 2 to view this page');

redirect('welcome/index');

# multiple groups (by id and name)

$group = array('gangstas', 2);

if (!$this->ion_auth->in_group($group))

{
$this->session->set_flashdata('message', 'You must be a part of the

gangstas or group 2');

redirect('welcome/index');

username_check()

Check to see if the username is already registered.

Parameters

1. 'Username' - string REQUIRED.

Return

boolean. TRUE if the user is registered FALSE if the user is not registered.

Usage

//This is a lame example but it works. Usually you would use this method with

form_validation.

$username = $this->input->post('username');

$password = $this->input->post('password');

$email = $this->input->post('email');
$additional_data = array(

'first_name' => $this-

>input->post('first_name'),

'last_name' => $this-

>input->post('last_name'),

);

if (!$this->ion_auth->username_check($username))

$group_name = 'users';

$this->ion_auth->register($username, $password, $email,

$additional_data, $group_name)

email_check()

Check to see if the email is already registered.

Parameters

1. 'Email' - string REQUIRED.

Return
boolean. TRUE if the user is registered FALSE if the user is not registered.

Usage

//This is a lame example but it works. Usually you would use this method with

form_validation.

$username = $this->input->post('username');

$password = $this->input->post('password');

$email = $this->input->post('email');

$additional_data = array(

'first_name' => $this-

>input->post('first_name'),

'last_name' => $this-

>input->post('last_name'),

);

if (!$this->ion_auth->email_check($email))

$group_name = 'users';

$this->ion_auth->register($username, $password, $email,

$additional_data, $group_name)

}
identity_check()

Check to see if the identity is already registered.

Parameters

1. 'Identity' - string REQUIRED.

Return

boolean. TRUE if the user is registered FALSE if the user is not registered.

Usage

//This is a lame example but it works.

$user = $this->ion_auth->user();

$data = array(

'identity' => $this->input->post('identity'),

'first_name' => $this->input->post('first_name'),

'last_name' => $this->input->post('last_name'),

);

if ($data['identity'] === $user->username || $data['identity'] === $user->email ||

$this->ion_auth->identity_check($data['identity']) === FALSE)

{
$this->ion_auth->update_user($user->id, $data)

is_max_login_attempts_exceeded()

If login attempt tracking is enabled, checks to see if the number of failed


login attempts for this identity or ip address has been exceeded. The
controller must call this method and take any necessary actions. Login
attempt limits are not enforced in the library.

Parameters

1. 'Identity' - string REQUIRED.

Return

boolean. TRUE if maximum_login_attempts is exceeded FALSE if not or if


login attempts not tracked.

Usage

$identity = 'ben.edmunds@gmail.com';

if ($this->ion_auth->is_max_login_attempts_exceeded($identity))

{
$this->session->set_flashdata('message', 'You have too many login

attempts');

redirect('welcome/index');

get_attempts_num()

Returns the number of failed login attempts for this identity or ip address.

Parameters

1. 'Identity' - string REQUIRED.

Return

int. The number of failed login attempts for this identity or ip address.

Usage

$identity = 'ben.edmunds@gmail.com';

$num_attempts = $this->ion_auth->get_attempts_num($identity);
increase_login_attempts()

If login attempt tracking is enabled, records another failed login attempt


for this identity or ip address. This method is automatically called during
the login() method if the login failed.

Parameters

1. 'Identity' - string REQUIRED.

Usage

$identity = 'ben.edmunds@gmail.com';

$password = '12345678';

if ($this->ion_auth->login($identity, $password) == FALSE) {

$this->ion_auth->increase_login_attempts($identity)

clear_login_attempts()

Clears all failed login attempt records for this identity or this ip address.
This method is automatically called during the login() method if the login
succeded.
Parameters

1. 'Identity' - string REQUIRED.

Usage

$identity = 'ben.edmunds@gmail.com';

$password = '12345678';

if ($this->ion_auth->login($identity, $password) == TRUE) {

$this->ion_auth->clear_login_attempts($identity)

user()

Get a user.

Parameters

1. 'Id' - integer OPTIONAL. If a user id is not passed the id of the currently


logged in user will be used.

Return

stdClass Object (

[id] => 1
[ip_address] => 127.0.0.1

[username] => administrator

[password] => 59beecdf7fc966e2f17fd8f65a4a9aeb09d4a3d4

[salt] => 9462e8eee0

[email] => admin@admin.com

[activation_code] =>

19e181f2ccc2a7ea58a2c0aa2b69f4355e636ef4

[forgotten_password_code] =>

81dce1d0bc2c10fbdec7a87f1ff299ed7e4c9e4a

[remember_code] =>

9d029802e28cd9c768e8e62277c0df49ec65c48c

[created_on] => 1268889823

[last_login] => 1279464628

[active] => 0

[first_name] => Admin

[last_name] => Account

[company] => Some Corporation

[phone] => (123)456-7890

Usage

$user = $this->ion_auth->user()->row();

echo $user->email;

users()

Get the users.

Parameters

1. 'Group IDs' - array OPTIONAL. If an array of group ids are passed (or a single
group id) this will return the users in those groups.

Return

array of objects

Usage

$users = $this->ion_auth->users()->result();

group()
Get a group.

Parameters

1. 'Id' - integer REQUIRED.

Return

object

Usage

$group_id = 2;

$group = $this->ion_auth->group($group_id);

groups()

Get the groups.

Return

array of objects

Usage

$groups = $this->ion_auth->groups()->result();
messages()

Get messages.

Return

string

Usage

$id = 12;

$data = array(

'first_name' => 'Ben',

'last_name' => 'Edmunds',

);

if ($this->ion_auth->update_user($id, $data))

$messages = $this->ion_auth->messages();

echo $messages;

else

{
$errors = $this->ion_auth->errors();

echo $errors;

messages_array()

Get messages as an array.

Return

array

Parameters

1. 'Langify' - boolean OPTIONAL. TRUE means that the messages will be


langified.

Usage

$id = 12;

$data = array(

'first_name' => 'Ben',

'last_name' => 'Edmunds',

);
if ($this->ion_auth->update_user($id, $data))

$messages = $this->ion_auth->messages_array();

foreach ($messages as $message)

echo $message;

else

$errors = $this->ion_auth->errors_array();

foreach ($errors as $error)

echo $error;

}
get_users_groups()

Get all groups a user is part of.

Parameters

1. 'Id' - integer OPTIONAL. If a user id is not passed the id of the currently


logged in user will be used.

Return

stdClass Object (

[id] => 1

[name] => admins

[description] => Administrator

Usage

$user_groups = $this->ion_auth->get_users_groups($user->id)->result();

add_to_group()
Add user to group

Parameters

1. 'Group_id' - integer or array REQUIRED.

2. 'User_id' - integer REQUIRED.

Return

boolean. TRUE if the user was added to group(s) FALSE if the user is not
added to group(s).

Usage

// pass an array of group ID's and user ID

$this->ion_auth->add_to_group(array('1', '3', '6'), $user_id);

// pass a single ID and user ID

$this->ion_auth->add_to_group(1, $user_id);

remove_from_group()

Remove user from group(s)

Parameters
1. 'Group_id' - NULL, integer or array REQUIRED. NULL will remove the user
from all groups.

2. 'User_id' - integer REQUIRED.

Return

boolean. TRUE if the user was removed from group(s) FALSE if the user is
not removed from group(s).

Usage

// pass an array of group ID's and user ID

$this->ion_auth->remove_from_group(array('1', '3', '6'), $user_id);

// pass a single ID and user ID

$this->ion_auth->remove_from_group(1, $user_id);

// pass NULL to remove user from all groups

$this->ion_auth->remove_from_group(NULL, $user_id);

create_group()
Create a group

Parameters

1. 'group_name' - string REQUIRED.

2. 'group_description' - string.

Return

brand new group_id if the group was created, FALSE if the group creation
failed.

Usage

// pass the right arguments and it's done

$group = $this->ion_auth->create_group('new_test_group', 'This is a test description');

if(!$group)

$view_errors = $this->ion_auth->messages();

else

$new_group_id = $group;

// do more cool stuff


}

update_group()

Update details of a group

Parameters

1. 'group_id' - int REQUIRED.

2. 'group_name' - string REQUIRED.

3. 'group_description' - string.

Return

boolean. TRUE if the group was updated, FALSE if the update failed.

Usage

// source these things from anywhere you like (eg., a form)

$group_id = 2;

$group_name = 'test_group_changed_name';

$group_description = 'I changed the name yay';

// pass the right arguments and it's done


$group_update = $this->ion_auth->update_group($group_id, $group_name, $group_description);

if(!$group_update)

$view_errors = $this->ion_auth->messages();

else

// do more cool stuff

delete_group()

Remove a group. Removes the group details from the configured 'groups'
table. Users belonging to the group are stripped of this status (references
to this group are removed from users_groups), but user data itself
remains untouched.

Parameters

1. 'group_id' - int REQUIRED.


Return

boolean. TRUE if the group was deleted, FALSE if the delete failed.

Usage

// source this from anywhere you like (eg., a form)

$group_id = 2;

// pass the right arguments and it's done

$group_delete = $this->ion_auth->delete_group($group_id);

if(!$group_delete)

$view_errors = $this->ion_auth->messages();

else

// do more cool stuff

}
set_message_delimiters()

Set the message delimiters.

Parameters

1. 'Start Delimiter' - string REQUIRED.

2. 'End Delimiter' - string REQUIRED.

Usage

$id = 12;

$data = array(

'first_name' => 'Ben',

'last_name' => 'Edmunds',

);

if ($this->ion_auth->update_user($id, $data))

$this->ion_auth-

>set_message_delimiters('<p><strong>','</strong></p>');

$messages = $this->ion_auth->messages();

echo $messages;

else
{

$this->ion_auth->set_error_delimiters('<p><strong>','</strong></p>');

$errors = $this->ion_auth->errors();

echo $errors;

errors()

Get the errors.

Return

string

Usage

$id = 12;

$data = array(

'first_name' => 'Ben',

'last_name' => 'Edmunds',

);

if ($this->ion_auth->update_user($id, $data))
{

$messages = $this->ion_auth->messages();

echo $messages;

else

$errors = $this->ion_auth->errors();

echo $errors;

errors_array()

Get error messages as an array.

Return

array

Parameters

1. 'Langify' - boolean OPTIONAL. TRUE means that the error messages will be
langified.
Usage

$id = 12;

$data = array(

'first_name' => 'Ben',

'last_name' => 'Edmunds',

);

if ($this->ion_auth->update_user($id, $data))

$messages = $this->ion_auth->messages_array();

foreach ($messages as $message)

echo $message;

else

$errors = $this->ion_auth->errors_array();

foreach ($errors as $error)

{
echo $error;

set_error_delimiters()

Set the error delimiters.

Parameters

1. 'Start Delimiter' - string REQUIRED.

2. 'End Delimiter' - string REQUIRED.

Usage

$id = 12;

$data = array(

'first_name' => 'Ben',

'last_name' => 'Edmunds',

);

if ($this->ion_auth->update_user($id, $data))

{
$this->ion_auth-

>set_message_delimiters('<p><strong>','</strong></p>');

$messages = $this->ion_auth->messages();

echo $messages;

else

$this->ion_auth->set_error_delimiters('<p><strong>','</strong></p>');

$errors = $this->ion_auth->errors();

echo $errors;

set_hook()

Set a single or multiple functions to be called when trigged by


trigger_events(). See an example
here: http://gist.github.com/657de89b26decda2b2fa

Parameters

1. 'Event' - string REQUIRED.


2. 'Name' - string REQUIRED.

3. 'Class' - string REQUIRED.

4. 'Method' - string REQUIRED.

5. 'Arguments' - Array OPTIONAL.

Usage

class Accounts extends CI_Controller {

public function __construct()

parent::__construct();

/*

make sure we loaded ion_auth2

The following does not need to go in __construct() it just needs to be set before

you trigger_events().

*/

$event = 'socialpush';

$class = 'Accounts';

$args = array('this is the content of the message', 'billy');


$name = 'activate_sendmail';

$method = 'email';

$this->ion_auth->set_hook($event, $name, $class, $method, $args);

$name = 'call_Twitter';

$method = 'twitter';

$this->ion_auth->set_hook($event, $name, $class, $method, $args);

$name = 'call_MailChimp_API';

$method = 'mailchimp';

$this->ion_auth->set_hook($event, $name, $class, $method, $args);

$name = 'call_Facebook_API';

$method = 'facebook';

$this->ion_auth->set_hook($event, $name, $class, $method, $args);

$name = 'call_gPlus_API';

$method = 'gplus';

$this->ion_auth->set_hook($event, $name, $class, $method, $args);

public function Post_Message($one)


{

$this->ion_auth->trigger_events('socialpush');

public function email($content, $who)

return true;

public function twitter($content, $who)

return true;

public function mailchimp($content, $who)

return true;

public function facebook($content, $who)

return true;

}
public function gplus($content, $who)

return true;

trigger_events()

Call Additional functions to run that were registered with set_hook().

Parameters

1. 'Name' - String or Array REQUIRED.

Usage

$this->ion_auth->trigger_events('socialpush');

And here's an yet another ad. These ads bring in about $50/month so it
doesn't come close to paying for the time I spend supporting Ion Auth but
every little bit does help. Thanks for visiting.
======================================
Mongo_DB

MongoDB class for CodeIgniter

by Ben Edmunds
======================================

Yo,

This is a class for using MongoDB with CI. This is an extreme work in
progress. There are a ton of features I would like to add/change so
feel free to fork and add features.

INSTALLATION:
Just copy the files from this package to the correspoding folder in your
application folder. Then insert the appropriate settings in the config
file. You will also need MongoDB setup and working with the PECL Mongo
class.

USING MONGO_DB:
Below is sample controller code. There is also a example model
named Blog_model that will walk you through extending mongo_db
with your own models.

USING EXAMPLE MODEL:

//this would likely be auto-loaded but you can load it when needed
as well
$this->load->model('mongo_db');

//load the example blog model


$this->load->model('blog_model');

//create our post (document)


$post = array('id' => ($this->blog_model->count()+1),
'title' => 'test1',
'body' => 'blah',
'author' => 'ben',
'date' => time(),
);
$this->blog_model->add_post($post);

echo 'There are ' . $this->blog_model->count() . ' total posts:<br


/>';
foreach ($this->blog_model->find() as $post) {
print_r($post);
}

echo '<br /><br />This is the most recent post:<br />';


print_r($this->blog_model->find_one(array('id' => $this->blog_model-
>count())));
USING MONGO_DB DIRECTLY IN YOUR CONTROLLER:

//this would likely be auto-loaded but you can load it when needed
as well
$this->load->model('mongo_db');

//set which collection to use


$this->mongo_db->collection('posts');

//add a post (document)


$post = array('id' => ($this->mongo_db->posts->count()+1),
'title' => 'test1',
'body' => 'blah',
'author' => 'ben',
'date' => time(),
);
$this->mongo_db->posts->insert($post);

echo 'There are ' . $this->mongo_db->posts->count() . ' total


posts:<br />';
foreach ($this->mongo_db->posts->find() as $post) {
print_r($post);
}

echo '<br /><br />This is the most recent post:<br />';


print_r($this->mongo_db->find_one(array('id' => $this->mongo_db-
>posts->count())));

Feel free to send me an email or create an issue if you have any problems
but keep in mind this is extremely new...

Thanks,
-Ben Edmunds
ben.edmunds@gmail.com
@benedmunds
Ion Auth 2
The future of authentication

by Ben Edmunds

Redux Auth 2 had a lot of potential. It's lightweight, simple, and clean, but had a ton of
bugs and was missing some key features. So we refactored the code and added new
features.

This version drops any backwards compatibility and makes things even more awesome
then you could expect.

Support

If you use this to further your career, or put money in your pocket, and would like to
support the project please consider a moral license.

Documentation

Documentation is located at http://benedmunds.com/ion_auth/

Installation

Just copy the files from this package to the corresponding folder in your application folder.
For example, copy Ion_auth/config/ion_auth.php to application/config/ion_auth.php

You can also copy the libraries and models directories into your third_party/ion_auth
folder. For example, copy to /application/third_party/ion_auth/. The directory structure
would be:

controllers/Auth.php
views/
third_party/ion_auth/libraries/Ion_auth.php
third_party/ion_auth/libraries/Bcrypt.php
third_party/ion_auth/models/Ion_auth_model.php
Then in your controller add the package path and load the library like normal

$this->load->add_package_path(APPPATH.'third_party/ion_auth/');
$this->load->library('ion_auth);

CodeIgniter Version 2 Compatibility


CodeIgniter v2 requires the class file names to be lowercase. In order to support this follow
the standard installation procedures and then either rename the following files or create
symlinks

models/Ion_auth_model.php => models/ion_auth_model.php


controllers/Auth.php => controllers/auth.php

Relational DB Setup

Then just run the appropriate SQL file (if you're using migrations you can get the
migrations from JD here:https://github.com/iamfiscus/codeigniter-ion-auth-migration).

Usage

In the package you will find example usage code in the controllers and views folders. The
example code isn't the most beautiful code you'll ever see but it'll show you how to use
the library and it's nice and generic so it doesn't require a MY_controller or anything else.

Default Login

Username: admin@admin.com Password: password

Important

It is highly recommended that you use encrypted database sessions for security!

Optimization

It is recommended that you add your identity column as a unique index.

Options

Time Based One-Time Password (TOTP) - There is a Time Based One-Time Password
(TOTP) implementation compatible with Google Authenticator available. Feature branch
maintained by biscofil and is available athttps://github.com/benedmunds/CodeIgniter-Ion-
Auth/tree/otp

Feel free to send me an email if you have any problems.

Thanks, -Ben Edmunds ben.edmunds@gmail.com @benedmunds


Upload foto di codeigniter
Posted on May 1, 2017 by kintamahadji Posted in Tutorial Codeigniter Pemula .
Pada tutorial codeigniter untuk pemula kali ini saya akan sharing tentang sebuah konsep
sederhana dan tutorial tentang upload foto di codeigniter.
Upload foto adalah sebuah aktifitas yang lumrah kita jumpai di hampir semua aplikasi dan
web aplikasi, facebook, twitter, instagram dan sebagainya, pasti memikili fitur upload foto
didalamnya.
Konsep sederhana upload foto di codeigniter

1. Pada halaman upload foto, user memilih file yang akan di upload kemudian tekan tombol
submit.
2. Controller menerima action post dari form, kemudian datanya diolah, jika upload foto sesuai
dengan setup config parameter codeigniter, result true, sebaliknya false.
3. Return data berbentuk array kemudian dilempar kembali ke view, yang kemudian dapat diolah
lebih lanjut, dapat diambil nama filenya, ukuran file, tipe file dsb.

Bagaimana, sudah pahamkan konsepnya ? jika masih ada yang ingin ditanyakan silahkan
tuliskan di kotak komentar, dan berikut ini adalah tutorial lengkapnya, selamat mencoba,
link download lengkap ada di bagian bawah tutorial ini.

Tahap 1 : Buat folder upload


Di folder utama codeigniter, buatlah folder upload dengan permission code 775 (di linux)
dan write enable di windows.

Tahap 2 : Buat form upload


di folder aplications > view, buatkah file form view dengan nama form_upload_foto.php

kemudian ketikkan code berikut ini :


<!DOCTYPE html>
1
<html lang="en">
2
<head>
3
<meta charset="utf-8">
4
<meta http-equiv="X-UA-Compatible" content="IE=edge">
5
<meta name="viewport" content="width=device-width, initial-scale=1">
6
<title>Upload foto</title>
7
</head>
8
<body>
9
<h1>Upload foto</h1>
10
11
<form action="<?php echo site_url(); ?>/upload/upload_foto" method="post" enctype="multipart/form-
12
data">
13
14
<input type="file" name="nama_file">
15
16
<button type="submit">Submit</button>
17
</form>
18
19
<hr>
20
<small>Tutorial codeigniter : kintamahadji.com</small>
21
</body>
22
</html>

Tahap 3 : Buat file controller Upload.php


Di folder aplications > controller, buatlah file dengan nama Upload.php, kemudian paste
kode berikut ini, penjelasan kode ada di dalam kode.

1 <?php
2 defined('BASEPATH') OR exit('No direct script access allowed');
3
4 class Upload extends CI_Controller {
5
6 public function index()
7 {
8 // load helper url, digunakan untuk membuat form di view
9 $this->load->helper('url');
10
11 // menampilkan view form_upload_foto.php
12 $this->load->view('form_upload_foto');
13 }
14
15
16 public function upload_foto()
17 {
18 // setup config untuk upload
19
20 // nama folder upload, tanda ./ merupakan folder root dari aplikasi
21 $config['upload_path'] = './uploads';
22
23 // format file yang di dukung, untuk menambahkan format lain tinggal |namaformat
24 $config['allowed_types'] = 'gif|jpg|png|bmp';
25
26 // memanggil library upliad
27 $this->load->library('upload', $config);
28
29 // upload foto ! dan simpan hasilnya ke variabel $upload foto
30 $upload_foto = $this->upload->do_upload('nama_file');
31
32 echo '<pre>';
33
34 // jika hasil upload error, tampilkan error
35 if($upload_foto === FALSE)
36 {
37 print_r($this->upload->display_errors());
38 }
39
40 // jika hasil benar
41 else
42 {
43 $upload_data = $this->upload->data();
44
45 //tampilkan data dalam bentuk array
46 print_r($upload_data);
47
48 // untuk mengambil nama file, caranya
49 //echo $upload_data['nama_file'];
50 }
51 }
52 }

di bawah ini adalah contoh hasil output ketika submit file foto.

1 Array
2 (
3 [file_name] => button-dari-images.png
4 [file_type] => image/png
5 [file_path] => /Applications/XAMPP/xamppfiles/htdocs/teach/ci_upload_foto/uploads/
6 [full_path] => /Applications/XAMPP/xamppfiles/htdocs/teach/ci_upload_foto/uploads/button-dari-images.png
7 [raw_name] => button-dari-images
8 [orig_name] => button-dari-images.png
9 [client_name] => button-dari-images.png
10 [file_ext] => .png
11 [file_size] => 29.79
12 [is_image] => 1
13 [image_width] => 419
14 [image_height] => 320
15 [image_type] => png
16 [image_size_str] => width="419" height="320"
17 )

Contoh, misal kita ingin mendapatkan nama file, maka cara mengambilnya di file view
adalah $file_name;

Anda mungkin juga menyukai