how to use session check properly - codeigniter

I have made session check, I am using:
if ($data!==false) {
$this->load->view("a");
}
But the both code is still running (true and false), and that session also run, so i got my screen was a half code false a half code true :")
Controller
public function aksi_login()
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$where = array(
'username' => $username,
'password' => $password
);
$cek = $this->product_model->cek_login("admin",$where)->num_rows();
if($cek > 0){
$data_session = array(
'nama' => $username,
'status' => "login"
);
$this->session->set_userdata($data_session);
redirect(base_url("admin"));
}else{
echo "Username dan password salah !";
}
}
View
<?php if($this->session->userdata("data_session") !== false)
{$this->load->view("login");} ?>
False and true statement is running together so my screen is splited become 2 side.

Related

How to create a admin login session in Codeigniter 4

if($data)
{
$pass = $data['password'];
$authenticatePassword = password_verify($password, $pass);
if($authenticatePassword)
{
$ses_data = [
'id' => $data['id'],
'name' => $data['name'],
'email' => $data['email'],
'isLoggedIn' => TRUE
];
$session->set($ses_data);
return redirect()->to('/profile');
I want to make it so the admin can log in and direct it to his page.
do this in your admin controller
function index()
{
if($data)
{
$username= $this->input->post('username');
$password= $this->input->post('password');
$row= $this->admins->adminLogin($username, $password);
if(count($row)==1)
{
foreach($row as $val)
{
$data= array('USERID' =>$val->id,
'USERNAME' => $val->username,
'logged_in' => true,
'id' => $val->id,
);
$this->session->set_userdata($data);
}
redirect('admin/dashboard');
}
else
{
$this->session->set_flashdata('message','<div class="alert alert-danger">Invalid Username and Password.</div>');
}
}
$this->load->view('admin/index');
}

Get Userdata from database by Login Sessions Codeigniter 3

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",
);

session userdata dont show admin value

In database i have username, password, id, admin and etc. but my admin value not show. Why?
http://prntscr.com/ewuiz9
http://prntscr.com/ewuj4o
// Get username
$username = $this->input->post('username');
// Get and encrypt the password
$password = md5($this->input->post('password'));
// Login user
$user_id = $this->user_model->login($username, $password);
if($user_id){
// Create session
$user_data = array(
'admin' => $admin,
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);
$this->session->set_userdata('logged_in', $user_data);
// Set message
$this->session->set_flashdata('user_loggedin', 'You are now logged in');
redirect('posts');
} else {
// Set message
$this->session->set_flashdata('login_failed', 'Login is invalid');
redirect('users/login');
}
}
}
Because you have set it like
$user_data = array(
'admin' => $admin,
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);
$this->session->set_userdata('logged_in', $user_data);
You can access it like on another controller.
$userdata = $this->session->userdata('logged_in');
echo $userdata['username'];
If you just need want to access like
echo $this->session->userdata('username');
Then you just need to set it like
$user_data = array(
'admin' => $admin,
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);
$this->session->set_userdata($user_data);
Make sure you have autoloaded session so can have access to it over website
config/autoload.php
$autoload['libraries'] = array('session');
On config.php make sure you have set the session path and session folder permission 0700
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = APPPATH . 'cache/session/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
Thank you.
In model i found that:
// Log user in
public function login($username, $password){
// Validate
$this->db->where('username', $username);
$this->db->where('password', $password);
$result = $this->db->get('users');
if($result->num_rows() == 1){
return $result->row(0)->id;
} else {
return false;
}
}
i change to:
// Log user in
function login($username, $password){
$this->db->select('id,admin,username,password,email');
$this->db->from('users');
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get();
if($query->num_rows()==1){
return $query->result();
} else{
return false;
}
}

Laravel Auth::attempt always return false?

Here is myscreen shot of database and my login input
public function action(Request $request){
$username = $request->username;
$pass = bcrypt($request->password);
$credentials = [
'id' => $username,
'password' => $pass
];
dd(Auth::attempt($credentials));
if (Auth::attempt(['id' => $username, 'password' => $pass])) {
echo 'Ok';
}else{
echo 'Not ok';
}
I'm trying to make a login action using laravel auth::attemptbut it always return false.
$pass = bcrypt($request->password);
should be
$pass = $request->password;
The attempt method will automatically handle the encryption and comparison. You are in effect bcrypting it twice, so a match isn't found

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