• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I make a database row be a global?

#1
How do I make a database row be a global?
I've used google search for answers and none of the results helped me.

I am using this tutorial.

Here is config/globals.php


Code:
<?php

// Create customized config variables
$config['web_Address']= 'https://www.formget.com/blog';
$config['title']= 'CodeIgniter Global Variable';

$this ->load->database();  
$config['me']= $this->db->query("SELECT * from users WHERE id='$_COOKIE[userid]' && password ='$_COOKIE[pass]'"); $me = $me->row_array();

?>

Here are the errors I get.
Undefined property: CI_Loader::$load on Line 7
Call to a member function database() on null on Line 7

Is there a good way of making a database row a global? I am writing my own authentication system as Codeigniter doesn't come with one.
Reply

#2
First, use one of the already built and tested authentication systems out there. (don't reinvent the wheel...)

Second, create a MY_controller that all of the rest of your controllers inherit from, in the constructor, you can setup a call to your authentication methods. Since the system is "setup" at this point, the loading for the databases will work and everything will work as expected (just don't forget to inherit all of the controllers needing authentication to the MY_controller)
Reply

#3
I've worked on extending a core class to do this in core/Before.php


Code:
<?php
class Before extends MY_Admin {

public function __construct()
    {
            parent::__construct();
            $this ->load->database();  
            $data['me'] = "testing 123";
            exit();

    }
    exit();
}

The website does not terminate despite the 2 exit() existing. I have a controller in controllers/Admin.php with a class called Admin.

When I try to echo $me in the view, I get an error saying that $me is undefined.

Please help.
Reply

#4
PHP Code:
<?php
class MY_Admin extends CI_Controller {

public 
$me;

public function 
__construct()
    {
            
parent::__construct();
            
$this ->load->database();  
            
$this->me "testing 123";

    }



Then in your controller:

PHP Code:
public function somefunc() {

   echo 
$this->me;


Reply

#5
Please escape your users input. Cookie data can be manipulated and you are vulnarable to sql injection. You should read up on php (basic) security
Reply

#6
(06-26-2017, 10:56 AM)Diederik Wrote: Please escape your users input. Cookie data can be manipulated and you are vulnarable to sql injection. You should read up on php (basic) security

@Diederik, desbest is a security expert. </sarcasm>


desbest a you a college student or do you work for a company?


use remember me token
use escape_str from ci
use $id = (int) $id;
Reply

#7
(06-26-2017, 08:41 PM)Paradinight Wrote:
(06-26-2017, 10:56 AM)Diederik Wrote: Please escape your users input. Cookie data can be manipulated and you are vulnarable to sql injection. You should read up on php (basic) security

@Diederik, desbest is a security expert. </sarcasm>


desbest a you a college student or do you work for a company?

I'm a college student. I did go to Security Stack Exchange and r/forhire for security help but no one could help me sufficiently. Also the php escaping functions such as mysqli_real_escape_string and php prepared statements can be bypassed very easily, so a server firewall provides much better protection against sql injection than php. Also when I asked people if they knew how to make an authentication system, they didn't know and some were reluctant to say no, their answer was, use Laravel's authentication system, use Laravel.
Reply

#8
(06-26-2017, 10:28 AM)Kaosweaver Wrote:
PHP Code:
<?php
class MY_Admin extends CI_Controller {

public 
$me;

public function 
__construct()
 
   {
 
           parent::__construct();
 
           $this ->load->database();  
            $this
->me "testing 123";

 
   }



Then in your controller:

PHP Code:
public function somefunc() {

 
  echo $this->me;


I did that but I still get the same error.

Undefined property: Admin::$me

I would like a database row to be accessible from all controllers.
Reply

#9
Then you need to create ./application/core/MY_Controller.php

Then extend all of your controllers from that one.

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * ------------------------------------------------------------------------
 * Editor   : PhpStorm 2017.1
 * Date     : 1/10/2017
 * Time     : 7:45 AM
 * Authors  : Raymond L King Sr.
 * ------------------------------------------------------------------------
 *
 * Class        MY_Controller
 *
 * @project     citest
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2017 Pro Covers FX, LLC.
 * @license     http://www.procoversfx.com/license
 * ------------------------------------------------------------------------
 */

/**
 * Class MY_Controller
 *
 * This file should be saved as:
 * ./application/core/MY_Controller.php
 *
 */
class MY_Controller extends CI_Controller {

    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

    /**
     * @var  array
     */
    
protected $data = array();

    
/**
     * --------------------------------------------------------------------
     *  __construct
     * --------------------------------------------------------------------
     *
     * Class Constructor    PHP 5+
     *
     * @access    public
     */
    
public function __construct()
    {
        
parent::__construct();

        
/**
         * check to see if we want to use the CI profiler.
         * Requries the below in the ./application/config/config.php
         *
         * $config['profiler'] = FALSE;  // TRUE / FALSE
         */
        
$this->output->enable_profiler($this->config->item('profiler'));

        
log_message('debug''CI: MY_Controller class loaded');
    }

}    
// End of MY_Controller Class.

/**
 * Class Admin_Controller
 */
class Admin_Controller extends MY_Controller
{
    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

    /**
     *  __construct ()
     * --------------------------------------------------------------------
     *
     * Class    Constructor
     */
    
public function __construct()
    {
        
parent::__construct();

    }


}    
// End of Admin_Controller Class.

/**
 * Class Public_Controller
 */
class Public_Controller extends MY_Controller
{
    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

    /**
     *  __construct ()
     * --------------------------------------------------------------------
     *
     * Class    Constructor
     */
    
public function __construct()
    {
        
parent::__construct();

    }

}    
// End of Public_Controller Class.

/**
 * ------------------------------------------------------------------------
 * Filename: MY_Controller.php
 * Location: ./application/core/MY_Controller.php
 * ------------------------------------------------------------------------
 */ 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#10
I've done the instructions you said. When I use print_r($me); in MY_Controller.php which extends CI_Controller, I can see the $me variable on the screen. However the $me variable isn't passed to all the controllers such as Admin.php "class Admin extends MY_Controller". When I echo or use $me on Admin.php controller I get an error saying $me is undefined.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.