I have problem with codeigniter 3x and I'm using php 8, in method login_aksi undefine property $input, $form_validation
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct(){
parent::__construct();
}
// menampilkan halaman login
public function index(){
$this->load->view('v_login');
}
// validasi login
function login_aksi(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$sebagai = $this->input->post('sebagai');
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required');
this my autoload
$autoload['packages'] = array();
$autoload['libraries'] = array('database','session','form_validation');
I tried to change $aoutuload['libraries']
are you add this code after set rules ?
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
$this->load->view('formsuccess');
}
Related
Why do I get this error? I Already Load database in libraries too,
but it's giving error
Undefined property: Login_Controller::$user
I'm stuck for making login validation
This Is The Picture
This is my autoload
$autoload['model'] = array();
$autoload['helper'] = array('url');
$autoload['libraries'] = array('database', 'session','form_validation');
this is my Login_Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login_Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('login');
}
function verify()
{
$this->load->model('user_model');
$check = $this->user->validate();
//$this->load->library('database');
if($check)
{
$this->load->view('admin/main');
}
else
{
$this->load->view('login');
$message = "Username and/or Password incorrect.\\nTry again.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
}
And This Is My Model
class User_model extends CI_Model
{
public function __construct() {
parent::__construct();
}
function validate()
{
$array['username'] = $this->input->post('username');
$array['password'] = $this->input->post('password');
$this->db->get_where('user',$array)->result();
}
}
Thanks For The Help
Change this line
$check = $this->user->validate();
to
$check = $this->user_model->validate();
Update the validate() function as follows
function validate(){
$array['username'] = $this->input->post('username');
$array['password'] = $this->input->post('password');
return $this->db->get_where('user',$array)->num_rows();
}
Update $this->load->model('user_model'); To $this->load->model('user_model', 'user');
and update line $this->db->get_where('user',$array)->result(); to return $this->db->get_where('user',$array)->result(); in User_model validate method.
When you load user model $this->load->model('user_model'); by default it will assign User_model class object to user_model variable in Login_Controller. Which you can access as $this->user_model.
So when you access User_model class method validate using $this->user->validate(); you are getting error Undefined property: Login_Controller::$user.
If you want to use user variable pass 2nd parameter to load mode method as bellow:
$this->load->model('user_model', 'user');
I have a View that will send Login data with Ajax jquery as follows :
views/Login.php
I have a View that will send Login data with Ajax jquery as follows :
views/login.php
<!-- begin snippet: js hide: false console: true babel: false -->
That will send username & password to Model Login / insert_login
controller/Login.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct ()
{
parent::__construct();
$this->load->model('Login_Model');
}
public function index ()
{
$this->load->view('Login');
}
public function insert_login ()
{
$user = $this->input->post('user');
$pass = $this->input->post('pass');
$cekUser = $this->Login_Model->check_user($user);
if (empty($cekUser->username)) {
$sts = 'user_wrong';
}else{
if (md5($pass) != $cekUser->password) {
$sts = 'pass_wrong';
}else{
if ($cekUser->tipe_user == 'admin'){
$sts = 'success_admin';
$this->session->set_userdata(array('user'=>$user,'status'=>'admin'));
redirect(base_url('Admin/Home'));
}else{
$sts = 'success_user';
$this->session->set_userdata(array('user'=>$user,'status'=>'user'));
redirect(base_url('Admin/Home'));
}
}
}
echo '{"status":"'.$user.'"}';
}
public function logout ()
{
$this->session->sess_destroy();
redirect(base_url('Admin/Login'));
}
}
?>
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login_Model extends CI_Model {
public function check_user ($user)
{
return $this->db->get_where('m_user', array('username' => $user))->row();
}
}
?>
I wonder why Query Database Function get_where / query / Any query can not be executed, but I have added Liblary Database in autoload.php:
$autoload['libraries'] = array('database','session'),
And I test using a Controller other than Login Controller still can run.
Why only in the model & Login controller can not execute Query?
But if I try to display the data captured divariabel:
$user = $this->input->post('user');
$pass = $this->input->post('pass');
echo $user.' '.pass;
When i test like it can already get and display data, tapi masalahnya kenapa ketika memunculkan Query tidak muncul apapun.
Please help me, I've really stack, stay up every day to clean up my job ...
set data into array in controller file
set name of array key as your database field name username & password
controller file
$user = $this->input->post('user');
$pass = $this->input->post('pass');
$where=array('username'=>$user,
'password'=>md5($pass));
$cekUser = $this->Login_Model->check_user($where);
give yout table name in from query
model file
class Login_Model extends CI_Model {
public function check_user ($where)
{
$this->db->select('*');
$this->db->from('table_name');
$this->db->where($where);
$res=$this->db->get();
return $res->result();
}
}
I am trying to use session in all controllers, but can't get success.
Here is my controller code of library.
<?php if(!defined('BASEPATH')) exit('NO direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function is_logged_in()
{
$user = $this->session->userdata('username');
return isset($user);
}
}
?>
And here I am inherit It in my other controller file.
class Homepage extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function CheckSession()
{
if ($this->is_logged_in())
{
echo '111';
}
}
}
I also try helper but giving me same error.
<?php
function is_logged_in() {
// Get current CodeIgniter instance
$CI =& get_instance();
// We need to use $CI->session instead of $this->session
$user = $CI->session->userdata('user_data');
if (!isset($user)) { return false; } else { return true; }
}
?>
autoload.php part
$autoload['helper'] = array('url','form','file','login');
$autoload['drivers'] = array('session');
$autoload['libraries'] = array('database','session', 'email', 'form_validation', 'MY_Controller');
I am Following this link.
either you need to autoload session library in your authoload.php like this
$autoload['libraries'] = array('session');
or load session library in constructor of library file like this
public function __construct()
{
parent::__construct();
$this->load->library('session');
}
I am working on a login script and it used to work perfectly but then all of a sudden it stopped working. I commented out a lot of code but it still does the same. When I login it sees the userdata but it disappears right after.
This is what I got in my controller: (I leave commented code out)
Login controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends MY_Controller {
public function __construct(){
parent::__construct();
$this->smarty->assign('header',"../../../views/login_header.tpl");
$this->smarty->assign('footer',"../../../views/login_footer.tpl");
$this->load->library('form_validation');
}
public function index()
{
$this->load->model('mdl_login');
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|callback_credential_check');
if ($this->form_validation->run() == FALSE)
{
$errors = validation_errors();
$this->smarty->assign('errors', $errors);
}
elseif($this->mdl_login->login($email, $password) == FALSE){
$errors = 'Your login information is incorrect.';
$this->smarty->assign('errors', $errors);
}
else //Validation success
{
//Get user
$user = $this->mdl_login->user($email);
$user['logged_in'] = TRUE;
//It's not the if statement here because I also tried without it
if($this->session->userdata('logged_in') == FALSE){
$this->session->set_userdata($user);
}
print_r($this->session->all_userdata());
die(); //At this point the userdata is correct but on refresh or going to another page it isn't
//Redirect to homepage
//redirect('dashboard', 'refresh');
}
$this->smarty->view('login/login.tpl');
}
}
Login Model:
<?php
class Mdl_login extends CI_Model {
public function login($email, $password){
return TRUE;
}
public function user($email){
$query = $this->db->get_where('korisnici_tbl', array('email' => $email));
return $query->row_array();
}
}
?>
My_Controller:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
/* load the MX_Controller class */
require APPPATH."third_party/MX/Controller.php";
class MY_Controller extends MX_Controller {
public function __construct(){
print_r($this->session->all_userdata());
$this->smarty->assign('header',"../../../views/header.tpl");
$this->smarty->assign('footer',"../../../views/footer.tpl");
$this->smarty->assign('form_url_plugin',"../../../views/form_url_plugin.tpl");
$this->smarty->assign('controller',$this->uri->segment(1));
$this->smarty->assign('userdata',$this->session->all_userdata());
if($this->session->userdata('logged_in') != TRUE && $this->uri->segment(1) != 'login'){
if($this->session->userdata('logged_in') != TRUE){
print_r($this->session->all_userdata());
}
//redirect('login', 'refresh');
}
}
}
I am using modular extensions. I am sure there is nothing wrong in My_Controller because I tested it by setting it back to MX_Controller. (I never changed anything in MX_Controller)
I found the answer to my question. In my model I was adding all data of the logged in user. One of the columns contain the IP Address. And I don't know why exactly but when I add the IP Address to the session it removes the session, and adds a new session with the default codeigniter session data and a new session id.
So somehow I can't store IP Addresses in my userdata.
I have installed tank auth to save me time creating a authentication script.
As I am using HMVC, tank auth has it's own module (modules/auth).
How can I protect my other modules (/admin, /members etc) with the login script??
From what I have read I need to do something like:
modules::run('auth/is_logged_in');
My auth controller looks like:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Auth extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//$this->load->library('security');
$this->load->library('tank_auth');
$this->lang->load('tank_auth');
}
function index()
{
if ($message = $this->session->flashdata('message')) {
//$this->load->view('auth/auth/general_message', array('message' => $message));
$main_content = 'auth/auth/general_message';
$this->load->view('includes/template', array('message' => $message, 'main_content' =>$main_content));
} else {
redirect('auth/login/');
}
}
/**
* Login user on the site
*
* #return void
*/
function login()
{
if ($this->tank_auth->is_logged_in()) { // logged in
redirect('admin');
} elseif ($this->tank_auth->is_logged_in(FALSE)) { // logged in, not activated
redirect('auth/send_again/');........
an the controller/module I want to protect with the login script:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Admin extends MX_Controller {
public function __construct(){
parent::__construct();
modules::run('auth/auth/login');
}
The above does not seem to work? What am I missing?
I put this code into my controllers:
function _remap($method)
{
if (method_exists($this, $method) && $this->tank_auth->is_logged_in())
{
$this->$method();
}
else
{
redirect('/auth/login/');
}
}