soedirman15@gmail.com atau WA 085740291996

Kamis, 17 Oktober 2013

Syntax Authentication User Management on CodeIgniter FrameWork

Logging a user in

You may put this in the "login" method of your website.
// These variables may come from a form, for instance
if($this->user->login($login, $password)){
    // user is now logged in, you may redirect it to the private page
    redirect('private_page');
} else {
    redirect('login_page');
}

Validating a session (user is logged in)

You can create custom actions with this function.
if($this->user->validate_session()) {
    echo "If you can see this you are logged in.";
}

Auto redirect on invalid session

Auto redirects if the user isn't logged in. The first parameter tells where to redirect if theres a invalid session (controller/method/etc). If you wish to lock the whole controller, you can put it on the constructor.
$this->user->on_invalid_session('home/login');

Auto redirect on valid session

Auto redirect function if the user is logged in. The first parameter tell where to redirect if theres a valid session (controller/method). Ideal for login pages.
$this->user->on_valid_session('home');

Displaying errors

Codeigniter-user library uses two flashdata names for displaying errors. They are "error_message" for errors and "success_message" for successes. You may want to show them ahead the login form, for example:
<form id="login_form">
    <div class="error_message"><?php echo $this->session->flashdata('error_message');?></div>
    <div class="success_message"><?php echo $this->session->flashdata('success_message');?></div>
    // the login inputs and buttons go here...
</form>

Get the current logged in name, id & email

Simple way to retrieve the logged user name and login.
echo 'Hello, ' . $this->user->get_name() . '! Your ID is: ' . $this->user->get_id() . ' and your e-mail is: ' . $this->user->get_email();

Get the current logged in data

Simple way to retrieve the logged user data. All the available data is dumped into this variable.
var_dump($this->user->user_data);

Check permission

Checks if user has a permission. The first parameter is the permission name.
if($this->user->has_permission('editor')){
    $this->load->view('editor_menu');
}

Logout user

Removes all session from browser and redirects the user to the received path.
$this->user->destroy_user('home/login');

Change user password or login on the fly

Call these functions for updating user's password or login. Theres no need to update the database.
// changing the user login and password with received data from form
$this->user->update_pw($this->input->post('new_password'));
$this->user->update_login($this->input->post('new_login'););

Managing users

There is a separated library for user managing. After setting up the database config, load up the user_manager library. There are some examples of:

Adding a new user

$fullname = "Johnny Winter";
$login = "john"
$password = "123becarefulwithafool";
$active = true;
$permissions = array(1, 3);
$new_user_id = $this->user_manager->save_user($fullname, $login, $password, $active, $permissions);

Creating a permission

$permission_id = $this->user_manager->save_permission('editor', 'The editors of my website.');

Deleting a user


if( $this->user_manager->delete_user($user_id)){
    echo "User was deleted.";
}

0 komentar:

Posting Komentar