PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 8, 2022

[FIXED] Undefined property: CI::$session

 February 08, 2022     codeigniter, php, session     No comments   

Issue

I am having trouble loading my model on my login page. It has a problem with sessions on here. I have a security class helper which $key is part of it. But I think it is to do with session as error says.

I think I have set up num rows correct not sure if also that may be cause of it.

A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI::$session
Filename: core/Model.php
Line Number: 51

model

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users_model extends CI_Model {

    private $user_id;
    private $username;
    private $permission = array();

    public function __construct() {

        if (isset($this->session->userdata['user_id'])) {
            $user_query = $this->db->query("SELECT * FROM " . $this->input->post('dbprefix') . "user WHERE user_id = '" . (int)$this->session->userdata['user_id'] . "' AND status = '1'");

            if ($user_query->num_rows) {
                $this->user_id = $user_query->row['user_id'];
                $this->username = $user_query->row['username'];

                $this->db->query("UPDATE " . $this->input->post('dbprefix') . "user SET ip = '" . $this->db->escape($this->input->server['REMOTE_ADDR']) . "' WHERE user_id = '" . (int)$this->session->userdata['user_id'] . "'");

                $user_group_query = $this->db->query("SELECT permission FROM " . $this->input->post('dbprefix'). "user_group WHERE user_group_id = '" . (int)$user_query->row['user_group_id'] . "'");

                $permissions = unserialize($user_group_query->row['permission']);

                if (is_array($permissions)) {
                    foreach ($permissions as $key => $value) {
                        $this->permission[$key] = $value;
                    }
                }
            } else {
                $this->logout();
            }
        }
    }

    public function login($username, $password) {
        $user_query = $this->db->query("SELECT * FROM " . $this->input->post('dbprefix') . "user WHERE username = '" . $this->db->escape($username) . "' AND (password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1('" . $this->db->escape($password) . "'))))) OR password = '" . $this->db->escape(md5($password)) . "') AND status = '1'");

        if ($user_query->num_rows) {
            $this->session->userdata['user_id'] = $user_query->row['user_id'];

            $this->user_id = $user_query->row['user_id'];
            $this->username = $user_query->row['username'];         

            $user_group_query = $this->db->query("SELECT permission FROM " . $this->input->post('dbprefix') . "user_group WHERE user_group_id = '" . (int)$user_query->row['user_group_id'] . "'");

            $permissions = unserialize($user_group_query->row['permission']);

            if (is_array($permissions)) {
                foreach ($permissions as $key => $value) {
                    $this->permission[$key] = $value;
                }
            }

            return true;
        } else {
            return false;
        }
    }

Solution

If you are using session in your code then make sure to autoload in config.php file by setting an encryption key. If you don't autoload it then you can load it using $this->load->library('session'). But you must set encryption key in order to autoload or load session in controller functions.

Detailed information here. Working with sessions in codeigniter



Answered By - Hammad
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing