Pages

Saturday, February 19, 2011

CodeIgniter App - Login Page

/**
 * Implementation of login page using CodeIgniter
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : February 20, 2010
 */

<?php

class Blog extends CI_Controller{
   
    function index(){
        header("Location: blog/page");
    }
   
    // login form
    function page(){
        $this->load->view('blog/front');
    }
   
    // check the login validation  
    function check(){
        $username = $_POST['username'];
        $pass = $_POST['password'];
        // add more data
        $data = array(
            'username' => $username,
            'log_in' => TRUE,   // for the next checking
        );
        if(strcmp($username,"admin") == 0  && strcmp($pass,"bali") == 0){
            $this->load->library('session');
            $this->session->set_userdata($data);
            header("Location: hello");
        }else{
            header("Location: page");
        }
    }
   
    // authenticated page
    function hello(){
        $this->load->library('session');
        if(!$this->session->userdata('log_in')){
            header("Location: page");
        }
       
        echo "<h1>You are log in</h1>";
       
        echo "<a href=\"logout\">Go Logout</a>";
    }
   
    // logout page
    function logout(){
        $this->load->library('session');
        $this->session->unset_userdata('log_in');
        header("Location: page");
    }
}


blog/front.php - view files
<div id="login">
  <h1>Login</h1>
  <table>
   <form action="check" method="post">
    <tr><td>Username</td>
        <td>:</td>
        <td><input type="text" name="username"/></td></tr>
    <tr><td>Password</td>
        <td>:</td>
        <td><input type="password" name="password"/></td></tr>
    <tr><td colspan="3">
        <input type="submit" value="Log In"/></td></tr>
   </form>
  </table>
</div>

No comments:

Post a Comment