Skip to content

Commit

Permalink
login checks db before site entry
Browse files Browse the repository at this point in the history
  • Loading branch information
cws13003 committed Dec 5, 2017
1 parent 98dc136 commit ff4ff6e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
20 changes: 18 additions & 2 deletions www/application/controllers/Login.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
<?php

class Login extends CI_Controller {
function __Construct(){
parent::__Construct();
$this->load->database(); // load database
$this->load->model('User_Info_Model'); // load model
$this->output->enable_profiler(TRUE);
}

public function index(){
$this->load->helper(array('form', 'url'));

$this->load->library('form_validation');

$this->form_validation->set_rules('username', 'Username', 'required');
Expand All @@ -15,7 +21,17 @@ public function index(){
// load the homepage if login successful
// https://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods
else {
redirect('/all_tweets');
//check if user in db
$user = $this->User_Info_Model->isRegistered();

// if yes get user id, launch session, send to home
if($user !== []){
$data['user'] = $user;
redirect('/all_tweets');
}
// if no, refresh login
else $this->load->view('login', $data);

}
}
}
2 changes: 2 additions & 0 deletions www/application/controllers/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function index(){

// load the homepage once properly submitted
// https://www.codeigniter.com/user_guide/libraries/form_validation.html#callbacks-your-own-validation-methods

// add user to db, launch session, send to home
else {
$this->User_Info_Model->addUser();
redirect('/all_tweets');
Expand Down
11 changes: 10 additions & 1 deletion www/application/models/User_Info_Model.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<?php
class User_Info_Model extends CI_Model {
// login
function getUserID(){
function isRegistered(){
$username = $this->input->post('username');
$password = $this->input->post('password');

$this->db->select('*');
$this->db->from('Users');
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
// register
function addUser(){
Expand Down
31 changes: 14 additions & 17 deletions www/application/views/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@
$this->load->library('form_validation');
echo validation_errors();
echo form_open('login'); ?>
<?php
var_dump($user);
?>

<div class="container">
<div class="row">
<div class="col-md-2 col-md-offset-5">
<h1>Twitter</h1>
<img src="../img/twitter.png" alt="Twitter icon.">
<h5>Username</h5>
<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />
<?php echo form_error('username'); ?>
<h1>Twitter</h1>
<!--<img src="../img/twitter.png" alt="Twitter icon.">-->
<h5>Username</h5>
<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />
<?php echo form_error('username'); ?>

<h5>Password</h5>
<input type="password" name="password" value="<?php echo set_value('password'); ?>" size="50" />
<?php echo form_error('password'); ?>
<h5>Password</h5>
<input type="password" name="password" value="<?php echo set_value('password'); ?>" size="50" />
<?php echo form_error('password'); ?>

<br><br>
<div><input class="btn" type="submit" value="Submit"/></div>
</form>
</div>
</div>
</div>
<br><br>
<div><input type="submit" value="Submit"/></div>
</form>

0 comments on commit ff4ff6e

Please sign in to comment.