Speed up development with full-stack environments for every branch.

Learn More

how to implement basic authentication with CodeIgniter

Forked from Hello World with CodeIgniter.

1228 Runs 8129 Views 1353 Copies
Saved

Saved

mannysoft 18

mannysoft
published 4 years ago

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Welcome extends CI_Controller {
 
  function __construct()
  {
      parent::__construct();
  }  

	public function index()
	{
		// Using the terminal create new table by entering the 
    // following SQL:
    /*
    CREATE TABLE IF NOT EXISTS `users` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `username` varchar(32) NOT NULL,
    `password` varchar(64) NOT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
  */
  

  
  
    // Load the database
    $dbconnect = $this->load->database();
     $array = array('username' => 'admin', 'password' => md5('admin'));
  $this->db->insert('users', $array);
    
      // To add new user run the following query sample:
  // $array = array('username' => 'admin', 'password' => md5('admin'));
  // $this->db->insert('users', $array);

    // When the form submit this code will
    // execute
    if ($this->input->post('submit'))
    {
      // Get the input from the form and dont forget to add md5
      // function to the password.
      $this->db->where('username', $this->input->post('username'));
      $this->db->where('password', md5($this->input->post('password')));
      $result = $this->db->get('users');
      
      // If we find a user output correct, else output wrong.
      if($result->num_rows() != 0)
      {
        echo 'Correct!';
      }
      else
      {
        echo 'Wrong!';  
        
      }
    
    }
    
    $this->load->view('welcome_message');
	}
    
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).