Last active
December 17, 2015 13:29
-
-
Save unisys12/5617298 to your computer and use it in GitHub Desktop.
Just playing around with some user authentication in Codeigniter - Work in progress!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Users extends CI_Model { | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
public function getUser($user) | |
{ | |
$query = $this->db | |
->where('username', $user['username']) | |
->where('password', $user['password']) | |
->get('users'); | |
if($query->num_rows() > 0) | |
{ | |
$user = $query->row(); | |
return $user; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Welcome_controller extends CI_Controller { | |
public function index() | |
{ | |
$this->load->view('welcome'); | |
} | |
public function dashboard() | |
{ | |
// Bring in all POST data from the login form | |
$user = $this->input->post(NULL, TRUE); | |
// Run the query check if the username and | |
// password exists and matches (returns TRUE | |
// or FALSE), by passing it the POST data. | |
// Assign that to the var $auth | |
$auth = $this->users->getUser($user); | |
// Pass $auth to the view | |
$data['auth'] = $auth; | |
// Display Users Name | |
$data['name'] = $auth->name; | |
// Check if $auth is TRUE or FALSE | |
if($auth == TRUE) | |
{ | |
// Set the users session, setting | |
// their logged state to TRUE. | |
$array = array( | |
'logged_in' => 'TRUE' | |
); | |
$this->session->set_userdata( $array ); | |
// The user is authenticated and 'logged_in' | |
// has been set in the session cookie, load | |
// the view | |
$this->load->view('dashboard', $data); | |
} | |
else | |
{ | |
// If $auth is false, log in failed and the | |
// user is redirected to the top of the | |
// Welcome controller. | |
redirect('welcome'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment