2

I'm between a hard place and a rock with a CI problem. I have to dynamically connect to a DB with the username and password that users type. Those are their oracle usernames and passwords. So, with that information I have to do the connection to the DB and then keep it alibe for the different models and controllers within the application.

I have a login controller and a login view. I've disabled the database autoload from the database.php and config.php files.

Then, the login.php controller looks like this:

class Login extends CI_Controller {

public function index()
{
    $this->output->enable_profiler(TRUE);

    if (isset($_POST['ingresar'])){

        $db['hostname'] = 'myhost';
        $db['username'] = $_POST['usr'];
        $db['password'] = $_POST['pwd'];

        $db['database'] = 'DBname';
        $db['dbdriver'] = 'oci8';
        $db['dbprefix'] = '';
        $db['pconnect'] = FALSE;
        $db['db_debug'] = FALSE;
        $db['cache_on'] = FALSE;
        $db['cachedir'] = '';
        $db['char_set'] = 'WE8ISO8859P1';
        $db['dbcollat'] = '';
        $db['swap_pre'] = '';
        $db['autoinit'] = TRUE;
        $db['stricton'] = FALSE;



        $db['DB'] = $this->load->database($_SESSION, TRUE);

        redirect('contactos', 'location');
    }
    else{
        $this->load->view('/componentes/header');
        $this->load->view('/componentes/menu_sin_login');
        $this->load->view('/login/login');
        $this->load->view('/componentes/footer');
    }       
}

When the controller redirects to the other controller "contactos" everything crashes because it doesn't recognize a database connection, on line 5, when trying to load a model.

class Contactos extends CI_Controller {
public function __construct()
{
    parent::__construct();
    $this->load->model('Contactos_model');
    $this->load->model('Componentes_model');.....

Any help you can provide would be really appreciated.

Regards, V

Veronica
  • 41
  • 1
  • 6

1 Answers1

0

In the context above $db is a local variable, which means it doesn't really do anything about the broader context of the script, and you're not storing it anywhere that it could be re-used.

To load the DB, you probably want to call:

// you may want to think about using an encrypted CI session instead?
$_SESSION['DB'] = $db;

then, in contactos, you would want:

class Contactos extends CI_Controller {
public function __construct()
{
    parent::__construct();
    $this->load->database( $_SESSION['DB'] );
    // continue as above.
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • Hello again! Thanks for the help but I'm still quite lost and can't make this work. I changed the code as follows: /views/login/login.php added the corresponding input for typing username and password. This is sending post information ok. /controllers/login.php class Login extends CI_Controller { public function __construct() { parent::__construct(); $this->output->enable_profiler(TRUE); } – Veronica Aug 30 '11 at 13:35
  • dismiss the previous one pleas...here it is the correct comment: Hi there...I use your suggestion but cannot make it work. Once I load the object in the session variable then the model doesn't read it. Part of the print_r() output...=> [DB] => CI_DB_oci8_driver Object but $this->load->database($_SESSION['DB'], TRUE); $query = $this->$_SESSION['DB']->query('ALTER SESSION SET nls_sort = \'SPANISH\''); in the model gives an error. Any clue??? Many thanks for the help. – Veronica Aug 30 '11 at 13:43