• Home
  • Hire me
Machine Language :: Human Language :: Our Language
not to hate, not to blame, but to understand
  • PHP
  • WordPress
    • WordPress Plugins
  • English
  • Bahasa Indonesia
  • Java
  • jQuery
  • Interlude
  • Lowongan Penulis
  • my twitter
  • my facebook
  • Machine Language :: Human Language :: Our Language
4

Simple MD5 Login System (+Logout) with CodeIgniter

December 15, 2010
by littleflow3r

Case Study : Create Simple MD5 Login System (+Logout) with CodeIgniter

Requirements: Webserver Package, already installed. CodeIgniter bundle.

Login (and Logout) is almost exist in any application that developed with certain programming language, in this case is PHP with framework CodeIgniter. The very basic concept of login is matching the username and password from user with the ones that are in the databases. With a security reason, usually the password is encoded using MD5. MD5 is one of hash function (one-way) that quite popular in cryptography, usually for user autentification.

On the other hand, logout has the simplest basic concept, that is with destroy the user’s session.

Anyway… just let trying it :)

Step 1 : CI Configuration (always start with it)

  1. Open the config.php file located in the system-application-config
  2. Change base_url, adjust according where your ci folder are. FOr example: your ci folder located in www/ci folder
    so change the line $config['base_url']="http://example.com/";
    with
    $config['base_url']="http://localhost/ci";
  3. Setting the database. Open file database.php located in the same folder as config.php. Change hostname, username, password, and the database name according to your mysql setting. For example :
    $db['default']['hostname'] = "localhost";
    $db['default']['username'] = "root";
    $db['default']['password'] = "";
    $db['default']['database'] = "db_tutorial";

Step 2 : Prepare the Database

  1. Make a database named db_tutorial.
  2. Prepare a table named tb_book (for table’s structure, see picture below)
  3. Insert some sample data, for example insert some sample data just like the picture below (REMEMBER : when you inserting some sample data, use MD5 function in to password field —> see the red circle on the picture)
  4. This is the result of sample data (with MD5 password)
  5. Ok, we’re done with database !

Step 3 : Create the Login Form (view)

  1. Type the following script,
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html><head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <title>Login with CI</title>
    <center>
    <h2> <b> Login with CI </b> <h2>
    <form action="<?=base_url();?>login/proseslogin" method="post">
    <table border="0" align="center">
    <tr>
    <td> Username</td>
    <td> <input name="username" type="text"> </td>
    </tr>
    <tr>
    <td> Password</td>
    <td> <input name="password" type="password"> </td>
    </tr>
    <tr>
    <td> &nbsp; </td>
    <td> <input name="submit" type="submit" value="login"> </td>
    </tr>
    </table>
    </form>
    <?php if(isset($error)) echo "<b><span style='color:red;'>$error</span></b>";
    if(isset($logout)) echo "<b><span style='color:red;'>$logout</span></b>"; ?>
    </center>
    </body></html>
    
  2. Save with the name login_view.php in the system-application-views folder

Step 4 : Create the Processing Controller for Login+Logout

  1. Type the following script,
    <?php
    class Login extends Controller {
    //constructor
    function login() {
    parent::Controller();
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->library('session');
    }
    //index for showing the login form
    function index() {
    $this->load->view('login_view');
    }
    //this function will do the login process
    function proseslogin() {
    $username = $this->input->post('username'); //read the username that filled by the user
    $password = $this->input->post('password'); //read the password that filled by the user
    $passwordx = md5($password); //this is for change $password into MD5 form
    //the query is to matching the username+password user with username+password from database
    $q = $this->db->query("SELECT * FROM tb_user WHERE username='$username' AND userpass='$passwordx'");
    if ($q->num_rows() == 1) {
    // if the query is TRUE, then save the username into session and load the welcome view
    $nama = $q->row()->username;
    $this->session->set_userdata('username',$nama);
    $data['welcome'] = "Welcome $nama";
    $this->load->view('welcome_view', $data);
    }
    else {
    // query error
    $data['error']='!! Wrong Username or Password !!';
    $this->load->view('login_view', $data);
    }
    }
    //to do logout process
    function logout() {
    $this->session->sess_destroy();
    $data['logout'] = 'You have been logged out.';
    $this->load->view('login_view', $data);
    }
    }
    ?>
    
  2. Save with the name login.php in the system-application-controllers folder
  3. Penjelasan :See at the script’s comment.

Step 4 : Create the Success View

  1. Type the following script,
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html><head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <title>Login with CI</title>
    <center>
    <?php if(isset($welcome)) echo "<h2><span style='color:red;'>$welcome</span></h2>";
    echo "<br/>";
    echo anchor("login/logout", 'Logout') ?>
    </center>
    </body></html>
    
  2. Save with the name welcome_view.php in the system-application-views folder

Step 7 : Testing the Code

  1. Go to http://localhost/namaFolderCIKamu/login
  2. You will see something like this,
  3. Insert the correct username+password, that is the one in the database (username : june, password : june)
  4. You will be redirected into success login page, that is welcome page like in the picture below,
  5. Click logout to logout from welcome page.
  6. Now insert the wrong username+password (for example username : admin, password : admin)
  7. The warning will shown like below.

Okay, Happy Coding :)
Others that might interesting too:

Copyright

All scripts and techniques demonstrated on itx.web.id can be used in whatever manner you wish without attribution. You cannot copy whole tutorials, either in English or translated to another language.

This post is also available in: Indonesian


Related Articles:

  • Create a Pagination in CodeIgniterCreate a Pagination in CodeIgniter
  • Export from Database to Excel File in the CodeIgniterExport from Database to Excel File in the CodeIgniter
  • Tutorial to Create Simple Tabs-AJAX with AJAX and PHPTutorial to Create Simple Tabs-AJAX with AJAX and PHP
  • Tutorial Searching Data with PHP and MysqlTutorial Searching Data with PHP and Mysql
  • Tutorial Image Uploading, Saving and Showing.Tutorial Image Uploading, Saving and Showing.
  • PHP Form Input Date with Datetimepicker JQueryPHP Form Input Date with Datetimepicker JQuery
: PHP
: codeigniter login system, login codeigniter, login system, login with md5

About the author

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she lo
littleflow3r.wordpress.com/

4 Comments

  • yohan says:
    April 28, 2011 at 3:32 pm

    Login with CI

    Login with CI
    <form action="login/proseslogin” method=”post”>

    Username

    Password

     

    <?php if(isset($error)) echo "$error“;
    if(isset($logout)) echo “$logout“; ?>

    *saya copas knp yg keluar hanya “Login With CI”??
    yg lain nya ga keluar??

    Reply
  • sunupf says:
    May 14, 2011 at 4:41 pm

    numpang tanya :D
    saya juga sedang buat form login dengan CI.

    nah masalah saya ialah. ketika saya load form login dia tetep muncul padahal udah login..

    gimana ya pemecahannya..?…

    Reply
  • onimoed says:
    July 10, 2011 at 12:12 pm

    wow the best idea, i like your guide… (drinking)

    Reply
  • anggi says:
    October 20, 2011 at 10:26 am

    kok ndak mvc ya mas??? (sick) (sick)

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

*

*

RSS feed for this post (comments) · TrackBack URI
Previous Post
« Create a Pagination in CodeIgniter
Next Post
Export from Database to Excel File in the CodeIgniter »
  • Like itx.web.id?
  • Domba Garut
    Benih Alfalfa
    Beras Organik
  • Search

    • Hai
    • Posts
    • Comments
    Inilah blog itx.... seorang yang suka pemrograman web khususnya PHP dan WordPress... jangan ragu tinggalkan pesan di sini ... :)
    Sebagian besar artikel di sini tidak saya tulis, melainkan ditulis oleh teman-teman author (penulis lepas). Jadi kalo menyapa jangan salah alamat yaaa... lihat dulu siapa penulis artikel ybs.
    • Create Multi-User in WordPress CMS
    • Instant Sorting Function with PHP
    • Installing New Theme in WordPress CMS
    • Create Page (Statis Post) in WordPress (Beginner Only)
    • Code Igniter : Export Data into PDF File
    • String Functions in the PHP
    • Get the Data from Database with Codeigniter and Show it with JQuery UI Tab
    • Round Number in PHP
    • Put Flickr Album Photos in to WordPress
    • Upload Image in to WordPress Post (for Beginner)
    • kiki: sangat bermanfaat..
    • fdrock: Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\skripsi\delete.php on line 5
    • Dary Pradipta: masih berlaku kah ini mas ??
    • mausul: assalam alaikum,,, cuma salam kenal... dan mau tanya gimana cara apat tema kaya milik blog ini, bisa nggak tema ini dipakai ...
    • rio: kk q dh daftar pi confirm emailnya kok lama ea kk user name ku riosaragi88
    • ReneeSturn: selamat malam saya username : ReneeSturn ingin menjadi penulis. Karya tulis PHP di post dimana?
    • Dwi: @yang punya web: bang kok ni script ke hapus sih ke hapus tapi gk ada pesan Are You Sure to ...
    • Ridho Ramidianto: id : ridho-r
    • Ridho Ramidianto: saya ingin jadi penulis,.. ? gmana ?
    • Sonia: Masih bisakah dicoba? Thanks, Sonia
    • gendut_79: Salam Admin Saya sudah mendaftar atas nama : gendut_79 Mohon dijadikan AUTHOR Terima kasih
    • rani r: saya berminat untuk menulis disini
    • arism: halo min, udah daftar nih, id arism. ditambah jadi author ya.
    • FARA DILA SANDY: pengen punya kerjaan sambilan.,., gg dibayar sementara juga gag pa.,., pengalaman dulu :)
    • eisthon: saya sudah menjadi anggota. bagaimana cara untuk mulai menulis...mohon penjelasan..thanks???!
  • Postingan populer

    • Tutorial Upload, Menyimpan, dan Menampilkan Gambar dengan PHP dan Mysql
    • Membuat Form ComboBox Dinamis dari Database (Mysql) dengan PHP
    • Tutorial Searching / Pencarian Data dengan PHP dan Mysql
    • Form Input Tanggal PHP dengan Datetimepicker JQuery
    • Membuat Fungsi Update Data dalam Database dengan PHP Mysql
    • Login Multi-User dengan PHP dan Mysql
    • Penggunaan Fungsi Date / Time pada PHP
  • Tags

    ajax tab ceil checkbox delete multiple data codeigniter login system codeigniter pagination code igniter pdf date time function delete function with php dynamic combobox export database to excel export to pdf floor image GD library image thumbnail include php install wordpress login codeigniter login system login with md5 metadata wordpress object oriented php oop php php codeigniter php round posting code in the wordpress searching script simple mobile tab thumbnail version update data with php xml to database
    • en  English
    • id  Indonesia
  • Themes made by itx

    Albizia Theme
    - Download Albizia

    Bombax Theme
    - Download Bombax

    Calotropis Theme
    - Download Calotropis

  • Plugins made by itx

    Exclude Plugins
    - Download Exclude Plugins

  • Jika memiliki pertanyaan tentang theme atau plugin buatan itx, silahkan menuju forum.
  • Business Directory
    Benih Alfalfa
    Benih Asparagus
    Benih Chicory
    Bedak Saripohatji

Machine Language :: Human Language :: Our Language
Bombax Theme designed by itx