• 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
8

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:

  • Simple Mobile Programming with PHP and DeviceAtlasSimple Mobile Programming with PHP and DeviceAtlas
  • Tutorial to Create Simple Tabs-AJAX with AJAX and PHPTutorial to Create Simple Tabs-AJAX with AJAX and PHP
  • Passing variables between pages in PHP with URL and SessionPassing variables between pages in PHP with URL and Session
  • Get the Data from Database with Codeigniter and Show it with JQuery UI TabGet the Data from Database with Codeigniter and Show it with JQuery UI Tab
  • Code Igniter : Export Data into PDF FileCode Igniter : Export Data into PDF File
  • Instant Sorting Function with PHPInstant Sorting Function with PHP
: 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/

8 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
  • sebastian says:
    July 19, 2012 at 2:44 pm

    pengen nanya nih mas,
    kalo misalnya kita mau bikin user yang levelnya admin bgmana ya?

    Reply
  • Muneer Hussain says:
    August 20, 2012 at 2:25 am

    nice V.UseFul code. thanks alot…

    Reply
  • nola says:
    March 5, 2013 at 3:23 pm

    kok aneh ya , tggal copy ja tpi kok error . udah yakin tuh bner ?

    Reply
  • Tom Sheilds says:
    May 20, 2013 at 12:49 pm

    I think it’s better not to provide social security amount.Do they called you ans ID?

    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 Cabe Terpedas di Dunia
    Benih Sorgum Unggul
    Benih Terong
    Benih Markisa
    Beras Hitam Organik
    Benih Beras Hitam
  • 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)
    • http://is.gd/1nNk3Tdk: Nutritional supplements for Men
    • HDim Yati: Mas...,saya juga ingin jadi penulis yg digemari oleh pembaca, bahkan bukan hanya artikel saja...gemana?
    • 博主说的貌似很给力,我觉得我们能交流一下!我的博客博风: I do agree with all of the ideas you have introduced on your post. They're really convincing and will certainly ...
    • http://is.gd/4eNf7Trf: increase male organ
    • Watch Game of Thrones Season 3 Episode 8 Online: Thanks for sharing your thoughts about update data with php. Regards
    • Medibatam-Ajax: Gan kalau untuk membuat timer dengan ajax punya kodingnya gak gan, saya lagi nyari koding ajax untuk membuat timer di ...
    • http://tiny.cc/6yPj7Meb: increase male organ
    • UVI: Saya juga lagi nyari penulis untuk update website.
    • Romeo Cafarella: So beautiful! I love the earthy & classic tones of the wedding! I am so stealing the succulent idea. Beautiful ...
    • Annalee Simental: Alege elegant si simplu, alege online! Rochii Online iti aduce cele mai noi colectii de rochii din Marea Britanie. Indiferent ...
    • Sherie Swails: Do you know how I avoid duplicate iCal alerts? (Apple support does not, so far...) I think b/c of iCloud ...
    • Tifany Pilakowski: Even though I don't agree with you, I obtain your supply of your respective opinion one which I'm able to ...
    • Tom Sheilds: I think it's better not to provide social security amount.Do they called you ans ID?
    • Kraig Plauche: Mr. Elliott, Letters via USPS sounds so novel in the internet age regardless of whether it is an individual letter produced ...
    • Gladis Vongkhamchanh: I have been browsing on-line greater than 3 hours today, but I by no means located any interesting guide like ...
  • 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
    • Membuat Fungsi Update Data dalam Database dengan PHP Mysql
    • Form Input Tanggal PHP dengan Datetimepicker JQuery
    • 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 Terong Gendut
    Benih Cabe Terpedas & Terunik di Dunia
    Benih Mawar beraneka warna unik

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