soedirman15@gmail.com atau WA 085740291996

Himpunan Mahasiswa Teknik Informatika

www.soedirman-hmti.blogspot.com

Himpunan Mahasiswa Teknik Informatika

www.soedirman-hmti.blogspot.com

Himpunan Mahasiswa Teknik Informatika

www.soedirman-hmti.blogspot.com

Himpunan Mahasiswa Teknik Informatika

www.soedirman-hmti.blogspot.com

Himpunan Mahasiswa Teknik Informatika

www.soedirman-hmti.blogspot.com

Himpunan Mahasiswa Teknik Informatika

www.soedirman-hmti.blogspot.com

Himpunan Mahasiswa Teknik Informatika

www.soedirman-hmti.blogspot.com

Himpunan Mahasiswa Teknik Informatika

www.soedirman-hmti.blogspot.com

Himpunan Mahasiswa Teknik Informatika

www.soedirman-hmti.blogspot.com

Himpunan Mahasiswa Teknik Informatika

www.soedirman-hmti.blogspot.com

Tampilkan postingan dengan label codeigniter. Tampilkan semua postingan
Tampilkan postingan dengan label codeigniter. Tampilkan semua postingan

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.";
}

Sabtu, 28 September 2013

Perbedaan Base_Url() dengan Site_Url() pada CodeIgniter


Sebagai pemula dalam bercoding ria menggunakan framework php codeigniter, saya dulu sempat kebingungan dengan fungsi base_url() dengan site_url() yang sepintas tampak sama saja tpi beda. Dan membuat bingung pemula dalam menerapkan ke dalam script

dalam keterangan di user_guide:

site_url() akan mereturn nilai index page yang telah diset di config. Defaultnya index.php -> http://domain/path/index.php

sedangkan
base_url() hanya mereturn alamat site tanpa embel-embel -> http://domain/path/

artinya:
site_url() membaca string link dibelakang index.php sebagai controller/method/variabel/variabel
sedangkan
base_url() bisa membaca string apapun tanpa ada controller & metod alias bisa mereffer ke link/string apapun. Misal untuk tembak url css ato javascript.
Ex:
base_url(‘css/gaya.css’);
artinya tembak url css di path

http://domain/path/css/gaya.css

untuk penggunaan:
jika berhubungan dengan routing url menu, post, fungsi gunakan site_url()

untuk yang berhubungan dengan direct link css,images/js gunakan base_url()

NOTE:
dalam suatu kasus, anda menghilangkan index.Php
anda mengeset
$config['index_page'] =”
maka dalam hal ini site_url() == base_url()
sebagai saran, lebih baik anda tetap gunakan site_url()
karena apabila setting server mod rewritenya gak mau nyala, kalo anda dari awal menggunakan site_url() di link,fungsi,post data maka anda gak perlu repot ubah satu persatu link di script. Tinggal di config.php diset pada bagian $config['index_page'] =’index.Php’
maka semua akan bekerja tanpa perlu htaccess mod rewrite