Get Userdata from database by Login Sessions Codeigniter 3 - codeigniter

i'm trying to make a login system. When user already login it will show user personal data like name, email or something. I'm try to modify the login code but doesn't work. Maybe someone can help.
Here is my code.
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Model_user');
$this->load->library('form_validation');
}
public function index()
{
$this->load->view('user/user_login');
}
function aksi_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$where = array(
'username' => $username,
'password' => $password,
'status' => 1
);
$cek = $this->Model_user->cek_login('tb_user',$where)->num_rows();
if($cek > 0){
$data_session = array(
'username' => $username,
'status' => "userlogin"
);
$this->session->set_userdata($data_session);
redirect('user/user_dash');
}else{
$this->session->set_flashdata('flash_data', '<div class="alert alert-danger" role="alert" style="font-size:12px">
<center><b>Sorry !!</b> Username / Password is not correct.
</center>
</div>');
redirect('user/user_login');
}
}
function logout(){
$this->session->sess_destroy();
redirect('user/user_login');
}
}?>
Model
function cek_login($table,$where){
return $this->db->get_where($table,$where);
}
I'm can make username session appear,
<?php echo $this->session->userdata('username') ?> it's working.
But try to show name it doesnt appear. Appreciate any kind help. Thank you

You can try something like this.
$query = $this->Model_user->cek_login('tb_user',$where);
if($query->num_rows() > 0){
$user = $query->row();
$data_session = array(
'username' => $username,
'status' => "userlogin",
'name' => $user->name,
);
$this->session->set_userdata($data_session);
redirect('user/user_dash');
} {
$this->session->set_flashdata('flash_data', '<div class="alert alert-danger" role="alert" style="font-size:12px">
<center><b>Sorry !!</b> Username / Password is not correct.
</center>
</div>');
redirect('user/user_login');
}
For this to work, there must be a name column in the table tb_user

You need to store the full name in the session too.
Try editing this
$cek = $this->Model_user->cek_login('tb_user',$where)->num_rows();
if($cek > 0){
$data_session = array(
'username' => $username,
'status' => "userlogin"
);
to this
$cek = $this->Model_user->cek_login('tb_user',$where);
if($cek->num_rows() == 1){
$row=$cek->result_array()[0]; //returns an array of results, pick the first
$data_session = array(
'fullname' => $row['fullname'], //assuming column name is 'fullname'
'username' => $username,
'status' => "userlogin",
);

Related

How do I set the old() value of a form_dropdown in Codeigniter 4?

I've got a form in Codeigniter 4 and I'm using the form helpers to build the form.
When I use a form_input I can reload the submitted values of the form using the old() method. Like below.
<span class="input-group-text" id="inputGroup-sizing-sm">Membership Number</span>
<?php
$current_membership_options = [
'id' => "membership_number",
'name' => "membership_number",
'class' => "form-control",
'type' => 'text'
];
echo form_input($current_membership_options, old('membership_number'))
?>
I've tried a couple of different options but I can't get it to repopulate with a form_dropdown.
THE VIEW
<span class="input-group-text" id="inputGroup-sizing-sm">Membership Type</span>
<?php
$membership_type_option = [
'id' => 'membership_type',
'name=' => 'membership_type',
'class' => 'form-select ',
'type' => 'text'
];
echo form_dropdown($membership_type_option, $membership_type, old('membership_type'));
?>
I get $membership_type from the user controller.
public function new(): string
{
$user = new User;
$data = $this->model->getMemberships();
return view('Admin/Users/new', [
'user' => $user,
'membership_type' => $data
]);
}
And the model
/**
* #throws Exception
*/
public function getMemberships(): array
{
$result = $this->db->query('select * from membership_type')->getResultArray();
$dropdown = array();
$dropdown['0'] = 'Please Select';
foreach ($result as $r)
{
$dropdown[$r['id']] = $r['type'];
}
return $dropdown;
}
Thanks in advance.

Codeigniter sessions, display user's firstname

I'm trying to display the user's firstname after login but it doesn't appear on the page. I tried to display it like this
<h2> Welcome <?php echo $this->session->userdata('firstname'); ?> </h2>
but it didn't display the user's firstname, even though there are data in DB. But when I tried to change the 'firstname' into 'username', it displayed the email of the user(test#gmail.com). How can I display the user firstname? Here's my controller:
function login(){
$this->form_validation->set_rules('username', 'Username', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE) {
redirect('/');
} else {
$user_login = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
);
$login = new UI_model;
$result = $login->login_user($user_login);
if($result){
$auth_details = array(
'firstname' => $result->firstname,
'lastname' => $result->lastname,
'username' => $result->username,
);
$this->session->set_userdata($auth_details);
// $this->session->set_userdata('authenticated', '1');
$this->session->set_flashdata('status', 'User Logged in Successfully!');
redirect(base_url('pages/index'));
} else {
$this->session->set_flashdata('status', 'Invalid Credentials. Please try again');
redirect(base_url('pages/about'));
}
}
}
Here's my Model:
public function login_user($data){
$this->db->select('*');
$this->db->where('username', $data['username']);
$this->db->where('password', $data['password']);
$this->db->from('users');
$this->db->limit(1);
$query = $this->db->get();
if($query->num_rows() == 1){
return $query->row();
} else {
return false;
}
}
Can you attach an image of your database you are using for and echo all data session?

Codeigniter 3 Multi User database

I just finished creating website for my school projects with CI 3. I've also made a session and also all the menu for admin and user. But, when i tried to register a new user account and then login with it, the page showing the exact data like user 1. How can i make a different crud database to every different user (with the same views of page), so every user can do CRUD on their data.
This is my authh controller code :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Authh extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->library('form_validation');
}
public function index(){
$this->form_validation->set_rules('email','Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password','Password', 'trim|required');
if($this->form_validation->run() == false){
$data['title']= 'LCA Master Login';
$this->load->view('auth/header2',$data);
$this->load->view('authh');
$this->load->view('auth/footer2');
} else{
$this->_login();
}
}
private function _login()
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$user = $this->db->get_where('user',['email' => $email])->row_array();
//jika usernya ada
if($user) {
// jika usernya aktif
if($user['is_active']==1){
//cek password
if(password_verify($password, $user['password'])){
$data= [
'email' => $user['email'],
'role_id' => $user['role_id']
];
$this->session->set_userdata($data);
if($user['role_id']== 1){
redirect('admin');
} else {
redirect('user');}
}else {
$this->session->set_flashdata('message','<div class="alert alert-danger" role="alert">Wrong password!</div>');
redirect('authh');
}
} else{
$this->session->set_flashdata('message','<div class="alert alert-danger" role="alert">Email has not been activated!</div>');
redirect('authh');
}
} else {
$this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Email is not registered!</div>');
redirect('authh');
}
}
public function registration(){
$this->form_validation->set_rules('name', 'Name', 'required|trim');
$this->form_validation->set_rules('email', 'Email','required|trim|valid_email|is_unique[user.email]',[
'is_unique'=> 'This Email is already Registered!'
]);
$this->form_validation->set_rules('password1', 'Password', 'required|trim|min_length[3]|matches[password2]', [
'matches' => 'Password dont match!',
'min_length' => 'Password too short!'
]);
$this->form_validation->set_rules('password2', 'Password', 'required|trim|matches[password1]');
if ($this->form_validation->run() == false){
$data['title']= 'LCA Master Registration';
$this->load->view('auth/header2',$data);
$this->load->view('register');
$this->load->view('auth/footer2');
} else{
$data = [
'name' => htmlspecialchars($this->input->post('name', true)),
'email' => htmlspecialchars($this->input->post('email', true)),
'image' => 'default.jpg',
'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
'role_id' => 2,
'is_active' =>1,
'date_created'=>time()
];
$this->db->insert('user', $data);
$this->session->set_flashdata('message','<div class="alert alert-info" role="alert">
Your Account Has Been Created! Please Login </div>');
redirect ('authh');
}
}
public function logoutt()
{
$this->session->unset_userdata('email');
$this->session->unset_userdata('role_id');
$this->session->set_flashdata('message','<div class="alert alert-success"
role="alert">You have been logged out!</div>');
redirect('authh');
}
}

codeigniter login Cannot modify header information

I test on my localhost is working well and now show any error but after upload to the godaddy server. it is codeigniter version 2.2.6.
this error show in the login page.
Error Message 1
Message: Cannot modify header information - headers already sent by (output started at /home/user/public_html/ngapali/test/application/controllers/admin/user.php:1)
Filename: libraries/Session.php
Line Number: 433
Error Message 2
Message: Cannot modify header information - headers already sent by (output started at /home/user/public_html/ngapali/test/application/controllers/admin/user.php:1)
Filename: libraries/Session.php
Line Number: 689
Admin Controller of My Controller
<?php
class Admin_Controller extends MY_Controller{
function __construct(){
parent::__construct();
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('user_m');
$this->load->model('roomtype_m');
$this->load->model('room_m');
$this->load->model('roomcapacity_m');
//login check
$exception_uris = array(
'admin/user/login',
'admin/user/logout'
);
if(in_array(uri_string(), $exception_uris) == FALSE) {
if($this->user_m->loggedin() == FALSE) {
redirect('admin/user/login');
}
}
}}?>
My login page /user/login
Controller
<?php
class User extends Admin_Controller {
function __construct(){
parent::__construct();
}
public function index() {
$this->data['users'] = $this->user_m->get();
$this->data['subview'] = 'admin/user/index';
$this->load->view('admin/_layout_main',$this->data);
}
public function login() {
$dashboard = 'l0gadmin/dashboard';
$this->user_m->loggedin() == FALSE || redirect($dashboard);
$rules = $this->user_m->rules;
$this->form_validation->set_rules($rules);
if($this->form_validation->run() == TRUE){
if($this->user_m->login() == TRUE){
redirect($dashboard);
}else{
$this->session->set_flashdata('error','That email/password combination does not exist ');
redirect('admin/user/login','refresh');
}
}
$this->data['subview'] = 'admin/user/login';
$this->load->view('admin/_layout_modal',$this->data);
}
public function logout(){
$this->user_m->logout();
redirect('admin/user/login');
}}?>
Model
<?php
class User_m extends MY_Model {
function __construct(){
parent::__construct();
}
public function login(){
$user = $this->get_by(array(
'email'=> $this->input->post('email'),
'password' => $this->hash($this->input->post('password')),
),TRUE);
if(count($user)){
$data = array(
'name'=> $user->name,
'email' => $user->email,
'id'=> $user->id,
'loggedin'=>TRUE,
);
$this->session->set_userdata($data);
}
}
public function logout(){
$this->session->sess_destroy();
}
public function loggedin(){
return (bool) $this->session->userdata('loggedin');
}
public function hash($string){
return hash('sha512', $string . config_item('encryption_key'));
}}?>
View Page of login
<div class="row">
<div class="col-md-3">
<h2>Admin Login</h2>
<?php
echo validation_errors();
echo form_open('');
$email = array(
'name' => 'email',
'class' => 'form-control',
'placeholder' => 'Email Address'
);
echo form_input($email) . "<br/>";
$password = array(
'name' => 'password',
'class' => 'form-control',
'placeholder' => 'Password'
);
echo form_password($password) . "<br/>";
$submit = array(
'name' => 'submit',
'class' => 'form-control btn btn-primary',
'value' => 'Login'
);
echo form_submit($submit);
echo form_close();
?>
</div>
</div>
i search this problem can be of open and closed tag. I try to add all the page but it still show error. Why?.
Read the error message:
output started at
/home/user/public_html/ngapali/test/application/controllers/admin/user.php:1
/user.php file has some blank character on line 1
check for any blank space before <?php tag.
Remove it and refresh error will be go away.
One more suggestion,
Omit ending php tags ?> in controller and model files.
Read more here : Why would one omit the close tag?
This is most probably because there occured an error or warning, which is not visible because of your current display_errors setting. I would suggest turning it on in order to fix these errors/warnings.

Codeigniter login error switch between login and welcome

Now I'm developing system documentation using Codeigniter. But the problem is Not logged in Problems switching between pages. login and Welcome To switch on like this forever.
public function index()
{
if($this->input->post(NULL)){
$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->m_login->check_login($username,$password);
if(!empty($result)){
$data_account = array();
foreach($result as $row){
$data_account = array(
'account_id' => $row->id,
'username' => $row->user_name,
'password' => $row->password,
'fullname' => $row->fullname,
'email' => $row->email,
'tel' => $row->tel,
'detail' => $row->detail
);
}
$this->session->set_userdata('data_account',$data_account);
$this->__login();
//create_log_activity($this->account_id, date("Y-m-d H:i:s"));
redirect('welcome', 'refresh');
exit;
}else{
$this->load->view('login');
}
}else if($this->session->userdata('data_account')){
redirect('welcome', 'refresh');
exit;
}else{
$this->load->view('login');
}
}
public function logout(){
$this->session->unset_userdata('data_account');
redirect('','refresh');
}
I want to know how to solve the problem, thanks in advance.
Use
if($_SERVER['REQUEST_METHOD'] == "POST"){
instead of
if($this->input->post(NULL)){

Resources