CodeIgniter Forums
Session is not working in database.php file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Session is not working in database.php file (/showthread.php?tid=47957)



Session is not working in database.php file - El Forum - 12-29-2011

[eluser]web_developer[/eluser]
I have dynamic site database..

I have to load database according to site wise, I have DB settings in session variable.
But it doesn't working in database.php file.. Can anyone suggest? how can I get session variable in database.php file?


Code:
$active_group = 'mysite';
$db[$active_group]['hostname'] = (isset($_SESSION['mysite_db_host']))?$_SESSION['mysite_db_host']:'localhost';
$db[$active_group]['username'] = (isset($_SESSION['mysite_db_user']))?$_SESSION['mysite_db_user']:'abcd';
$db[$active_group]['password'] = (isset($_SESSION['mysite_db_pass']))?$_SESSION['mysite_db_pass']:'abcd';
$db[$active_group]['database'] = (isset($_SESSION['mysite_db_name']))?$_SESSION['mysite_db_name']:'';
$db[$active_group]['dbdriver'] = 'mysql';
$db[$active_group]['dbprefix'] = '';
$db[$active_group]['pconnect'] = TRUE;
$db[$active_group]['db_debug'] = TRUE;
$db[$active_group]['cache_on'] = FALSE;
$db[$active_group]['cachedir'] = '';
$db[$active_group]['char_set'] = 'utf8';
$db[$active_group]['dbcollat'] = 'utf8_general_ci';
$db[$active_group]['swap_pre'] = '';
$db[$active_group]['autoinit'] = TRUE;
$db[$active_group]['stricton'] = FALSE;
unset($active_group);



Session is not working in database.php file - El Forum - 01-25-2012

[eluser]jjDeveloper[/eluser]
Codeigniters session class might be a better alternative as your db credentials are currently available to anyone on the server. Plus you would be able to use it in your model.


Session is not working in database.php file - El Forum - 01-25-2012

[eluser]CroNiX[/eluser]
If you want to do that, why don't you just pass in the configuration array instead of using the database config file? You can do it manually.
Code:
$this->load->database($config);



Session is not working in database.php file - El Forum - 01-25-2012

[eluser]Aken[/eluser]
Also consider using the ENVIRONMENT constant to set the appropriate environment, instead of directly using session variables.