Codeigniter login error switch between login and welcome - codeigniter

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)){

Related

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?

delete_cookie('name') not working codeigniter 4

I am trying to delete the cookie i create when user logs in but somehow delete_cookie() function is not deleting the cookie i made. I checked the documentation and everything but i cannot get it to work
Here is my code
public function __construct()
{
helper('cookie');
}
public function login() {
$data = [];
$session = session();
$model = new AdminModel();
$username = $this->request->getPost('username');
$password = $this->request->getPost('password');
$remember = $this->request->getPost('agree');
$rules = [
'username' => 'required',
'password' => 'required',
];
if(!$this->validate($rules)) {
$data['validation'] = $this->validator;
} else {
$admin = $model->where('username', $username)->where('password', $password)->first();
if($admin) {
$session->set('uid', $admin['id']);
if($remember) {
set_cookie([
'name' => 'id',
'value' => $admin['id'],
'expire' => '3600',
'httponly' => false
]);
}
} else {
$session->setFlashdata('msg', 'Incorrect Username or Password');
return redirect()->to('admin/login');
}
}
return view('admin/login', $data);
}
public function logout() {
$session = session();
$session->destroy();
delete_cookie('id');
return redirect()->to('admin/login')->withCookies();
}
Edit:
I fixed it. I had to redirect with withCookies();
use this Library
use Config\Services;
Services::response()->deleteCookie('id');
refer this link
https://codeigniter.com/user_guide/libraries/cookies.html

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

404 not found on Codeigniter

I am trying to access my page 'login', and I'm using Codeingiter.
I'm using TAL php too, for templates.
I just defined my routes like this:
$route['default_controller'] = "site";
$route['404_override'] = '';
$route['login'] = 'login';
$route['signup'] = 'login';
$route['success'] = 'login';
And here is my 'login' controller :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_controller{
public function __construct(){
parent::__construct();
$this->tal->setOutputMode(PHPTAL::HTML5);
if($this->session->userdata('loggedIn') == true){
$this->tal->template = 'admin';
$this->tal->login = true;
}else{
$this->tal->template = 'main';
$this->tal->login = false;
}
$this->load->model('membership_model');
}
public function index(){
$requestPage = $this->uri->segment(1);
switch($requestPage){
case 'login':
$this->tal->title = 'Connexion';
$this->tal->display('login.php');
break;
case 'signup':
$this->tal->title = 'Inscription';
$this->tal->display('signup.php');
$this->createMember();
break;
case 'success':
$this->tal->title = 'Inscription validée!';
$this->tal->display('messagePage/signupSuccess.php');
break;
}
}
function validateCredentials(){
$query = $this->membership_model->validate();
if($query){
$userdata = array(
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password')),
'loggedIn' => true
);
$this->session->set_userdata($userdata);
redirect('backoffice/profile/#');
}
$this->index();
}
function createMember(){
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Nom', 'trim|required');
$this->form_validation->set_rules('forename', 'Prénom', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('adress', 'Adresse', 'trim|required');
$this->form_validation->set_rules('zipcode', 'Code Postal', 'trim|is_natural|required');
$this->form_validation->set_rules('city', 'Ville', 'trim|required');
$this->form_validation->set_rules('country', 'Pays', 'trim|required');
$this->form_validation->set_rules('username', 'Pseudo', 'trim|required|callback_usernameCheck');
$this->form_validation->set_rules('password', 'Mot de passe', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Confirmation du mot de passe', 'trim|required|matches[password]');
$this->form_validation->set_rules('captcha', 'Captcha');
$data = array(
'firstname' => $this->input->post('name'),
'forename' => $this->input->post('forename'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'adress' => $this->input->post('adress'),
'zipcode' => $this->input->post('zipcode'),
'city' => $this->input->post('city'),
'country' => $this->input->post('country'),
'password' => $this->input->post('password'),
'passwordHashed' => md5($this->input->post('password'))
);
if($this->form_validation->run() == false){}
else{
$result = $this->membership_model->usernameCheck();
var_dump($result);
switch($result){
case false:echo 'false';
$this->form_validation->set_message('', 'Désolé mais ce nom d\'utilisateur et / ou cet email sont déjà pris');
break;
case true:echo 'true';
$this->membership_model->createMember($data);
redirect('success');
break;
}
}
}
function logout(){
$this->session->sess_destroy();
redirect('/');
}
}//this ends class
My local URL is : http://localhost/dist.
And when I try to access it, with the URL http://localhost/dist/login, I just get '404 Page Not Found : The page you requested was not found.'
I don't understand why. All is ready.
Try accessing the url http://localhost/dist/index.php/login. It doesn't appear that you have removed the index.php in the config file. Here is how to do that.
Have you tried debugging it?
If you don't have any routes defined, do you still get the 404 error?
I would bet that the issue is because you're routing a path to itself, but I don't know for sure.
Take out all of the routes and make sure that the path works. Then, add one at a time until it stops working.

How to set the logged in user ID on a session (CodeIgniter)?

I am having trouble, getting the logged in user ID from the database...
This is my controller code :
function validation(){
$this->load->model('users_model');
$query = $this->users_model->validate();
if ($query){
$this->users_model->get_userID($this->input->post('email'));
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true,
'user_id' => $user_id
);
$this->session->set_userdata($data);
redirect('home');
}
else {
$this->index();
}
}
This is my model function where I return the user id from the database:
function get_userID($email){
$this->db->where('email',$email);
$query = $this->db->get('users');
foreach ($query->result() as $row)
{
$user_id = $row->id;
}
return $user_id;
}
Everything works perfect, except that $user_id returns empty... What am I doing wrong?
Try:
$user_id= $this->users_model->get_userID($this->input->post('email'));
try modifying the model function to this -
function get_userID($email){
$this->db->where('email',$email);
$query = $this->db->get('users')->row_array(); // retrieves only first-row.
return $query['id'];
}
In controller the function needs just a bit of modification as follows -
function validation(){
$this->load->model('users_model');
$query = $this->users_model->validate();
if($query){
//$user_id was not assigned before :)
$user_id = $this->users_model->get_userID($this->input->post('email'));
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true,
'user_id' => $user_id
);
$this->session->set_userdata($data);
redirect('home');
}
else {
$this->index();
}
}
hope it helps,
faisal ahmed
blog / site

Resources