PHP Session MVC using CodeIgniter -
so have been developing simple login , insert data pages using codeigniter, want every user has own data kept in db , every user has own set of-db. focus here session first, because can't session code.
i new php , never use php native 1 before. here controller:
public function login(){ $this->load->library('session'); $email = $this->input->post('email'); $password = $this->input->post('password'); log_message('error','isi email : '.$email); log_message('error','isi password : '.$password); $return = $this->welcome_model->checklogin($email, $password); if($return){ $this->session->set_userdata('email',$email); $var = $this->session->userdata; redirect('welcome/masuk');}
model:
public function checklogin($email, $password) { $return = $this->db->get_where('login',array('email'=>$email, 'password' => $password)); if($return->result_array() != null){ return true; }else{ return false; } }
view:
<form action="<?php echo base_url(); ?>welcome/login" method="post"> <input type="email" class="form-control" name="email" placeholder="email"> <input type="password" class="form-control" name="password" placeholder="password"> <button type="submit" class="btn btn-primary btn-block btn-flat">sign in</button> </form>
any appreciated! thanks!
alright, think need following points while considering making login page :-
1. $this->load->library('session'); //try put in autoload.php in application/config. $autoload['libraries'] = array('session'); 2. <form action="<?php echo base_url(); ?>welcome/login" method="post"> can replace in view <?= form_open() ?>....code....<?= form_close() ?> 3. try use form-validation rules $this->form_validation->set_rules( 'title', 'title', 'required|min_length[20]');
and yes separating users need separate each user on bases of it's unique id,username,usergroup...etc. more importantly codeigniter user-guide , if you've got stuck anywhere we've stackoverflow take care of us, keep updating database structure per requirements.
https://www.codeigniter.com/user_guide/
that's login page per application requirements
public function login() { $data = array(); $this->form_validation->set_rules( 'username', 'username', 'required|min_length[5]' ); $this->form_validation->set_rules( 'password', 'password', 'trim|required|min_length[6]' ); if ($this->form_validation->run() == false) { $this->template->set('title', 'login'); $this->template->load('main', 'contents' , 'login', $data); } else { $username = $this->input->post('username'); $password = $this->input->post('password'); if ($this->main_model->resolve_user_login($username, $password)) { $user_id = $this->main_model->get_user_id_from_username($username); $user = $this->main_model->get_user($user_id); $this->session->user_id = (int)$user->id; $this->session->username = (string)$user->username; $this->session->name = (string)$user->name; $this->session->password = (string)$user->password; $this->session->created_at = (bool)$user->created_at; $this->session->blog_id = (bool)$user->blog_id; $this->session->logged_in = (bool)true; redirect('dashboard'); } else { $this->template->set('title', 'login'); $this->template->load('main', 'contents' , 'login', $data); } } }
Comments
Post a Comment