Welcome Guest, Not a member yet? Register   Sign In
Login Username - Store as a variable but can't use in other controller class
#1

[eluser]cinewbie81[/eluser]
When a user successfully login to the system, i want the system to set the user login name to a variable. This variable can then be used from any of the Controller class. (I know we should use cookies to store username, but this is just an example)

For testing purpose, i created a helper file call login_helper.php as following:


login_helper.php
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

   $myUsername = 'ABC';
   define("__VARIABLE_1__","variable1");

?>

I try to call from a controller class, let's say main.php
Code:
class Main extends Controller {

    // Constructor
    function Main()
    {
        parent::Controller();
        $this->load->helper(array('login', 'setting'));
        $this->load->library('table');
        $this->load->model('dbmodel', 'sql', TRUE);
        $this->load->database();
    }

    function index( )
    {
        echo $myUsername;
         // It didn't show up here. WHY :(

        echo __VARIABLE_1__;
        // result = variable 1
    }    

}

Why i echo $myUsername it didn't show up anything, and i echo __VARIABLE_1__ it show the correct value ?
#2

[eluser]schnoodles[/eluser]
I think you should look up globals, which most people dont overly like.

You can always do function index( $myUsername ) { } or use some form of registry pattern.

Although im not sure exactly what you want to accomplish here, do you just want a basic config. In which case codeigniter has.
#3

[eluser]cinewbie81[/eluser]
How to use global in codeigniter ??

In normal php we can use require_once('mypage.php'), then we can get the variable from mypage.php, how can i accomplish it using codeigniter ??
#4

[eluser]cinewbie81[/eluser]
Ok let me stated it clear here ..

I store some System Setting Data such as 'Database Version', 'Company Name' etc in a database.

I can get all these information from any class by do the query such as "select dbversion, compname from setting" then get the corresponding results .. instead of have to query each time, i want the system to RUN the query when the system first loaded, then assign it to a global variable such as folloing:

runQuery("select dbversion, compname from setting")
$dbVersion = field[dbversion];
$compname = field[compname];

So in other controller class, instead of query the same thing again again, i want it to simply refer to $dbVersion and $compname variable .. how am i going to do it ?




Theme © iAndrew 2016 - Forum software by © MyBB