Codeigniter : Basic login with database
autoload
1 |
$autoload['libraries'] = array('database', 'session'); |
1 |
$autoload['helper'] = array('form','url'); |
database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => 'password', 'database' => 'db_test', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE ); |
table
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
-- phpMyAdmin SQL Dump -- version 4.5.1 -- http://www.phpmyadmin.net -- -- Host: 127.0.0.1 -- Generation Time: Mar 20, 2018 at 07:45 AM -- Server version: 10.1.16-MariaDB -- PHP Version: 5.5.38 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- Database: `db_kpi` -- -- -------------------------------------------------------- -- -- Table structure for table `tbl_employee` -- CREATE TABLE `tbl_employee` ( `emp_id` int(10) NOT NULL, `emp_username` varchar(60) NOT NULL, `emp_password` varchar(50) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `tbl_employee` -- INSERT INTO `tbl_employee` (`emp_id`, `emp_username`, `emp_password`) VALUES (1, 'a', 'a'), (2, 'm', 'm'), (3, 'b', 'b'); -- -- Indexes for dumped tables -- -- -- Indexes for table `tbl_employee` -- ALTER TABLE `tbl_employee` ADD PRIMARY KEY (`emp_id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `tbl_employee` -- ALTER TABLE `tbl_employee` MODIFY `emp_id` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |
controller login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Login extends CI_Controller { public function __construct() { parent::__construct(); } public function check() { $emp_username=$this->input->post('emp_username'); $emp_password=$this->input->post('emp_password'); $sql =" SELECT * FROM tbl_employee WHERE emp_username='$emp_username' AND emp_password='$emp_password' "; $query = $this->db->query($sql); if($query->num_rows()>0){ $newdata = array( 'emp_username'=>$emp_username, 'logged_in'=>'Login-OK' ); $this->session->set_userdata($newdata); redirect('login/home'); } else{ redirect('?act=F'); } } public function home() { $status=$this->session->userdata('logged_in'); if($status <>'Login-OK'){ redirect(''); }else{ $data['result'] = "Login Successfully"; $this->load->view('loginok',$data); } } public function logout() { $this->session->unset_userdata('emp_username'); $this->session->unset_userdata('logged_in'); $this->session->sess_destroy(); redirect(''); } } |
view loginok.php
1 2 3 4 5 6 7 |
<?php echo $result; echo '<hr />'; print_r($_SESSION); ?> <br /> <a href="<?php echo site_url('login/logout');?>"> logout </a> |