problems with session variables ci - codeigniter

my problem is that changing controller, My session variables are reset, creates a new cookie.HELP!
controller
if ($autenticacion) {
// creamos un array con las variables de sesión 'usuario_id' y 'login_ok'
$sesion_data = array(
'usuario_id' => $usuario,
'login_ok' => TRUE
);
// creamos la sesión con dichas variables
$this->session->set_userdata($sesion_data);
// y redirigimos al controlador principal
$datos['titulo'] = "Asociacion de futbol San Ignacio";
$datos['contenido_plantilla'] = "bienvenido";
$this->load->view('plantilla/plantillaAdmin', $datos);
config
$config['sess_cookie_name']= 'ci_session';
$config['sess_expiration']= 7200;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['cookie_prefix'] = "";
$config['cookie_domain'] = "";
$config['cookie_path'] = "/";
$config['cookie_secure'] = FALSE;

Related

Connect multiple database in controller or model Codeigniter

I want connect over database
because dbHost, dbUser, dbPass... is Variable should i want connect it in controller or model.
In controller file or model file, What should I write?
In config.php file set as follow:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = 'db1';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['db2']['hostname'] = 'localhost';
$db['db2']['username'] = '';
$db['db2']['password'] = '';
$db['db2']['database'] = 'db2';
$db['db2']['dbdriver'] = 'mysql';
$db['db2']['dbprefix'] = '';
$db['db2']['pconnect'] = TRUE;
$db['db2']['db_debug'] = TRUE;
$db['db2']['cache_on'] = FALSE;
$db['db2']['cachedir'] = '';
$db['db2']['char_set'] = 'utf8';
$db['db2']['dbcollat'] = 'utf8_general_ci';
$db['db2']['swap_pre'] = '';
$db['db2']['autoinit'] = TRUE;
$db['db2']['stricton'] = FALSE;
Or where you want to use directly do as follow:
$db2 = $this->load->database("database_name);
in active record:
$this->db2->select("*");
You can do this without adding to any file
1)We need to create DB Driver String from your given parameters
dbdriver://username:password#hostname/database
//Controller function
function connnectExternalDB()
{
$dbdriver = $this->input->get_post('dbdriver'); //mysqli or mysql
$username = $this->input->get_post('username'); // DB user name
$password = $this->input->get_post('password'); // DB password
$hostname = $this->input->get_post('hostname'); // DB host url
$database = $this->input->get_post('database'); // DB name
$dsn_string = "$dbdriver://$username:$password#$hostname/$database";
// Following will overwrite your current DB connection
$this->load->database($dsn_string, FALSE, TRUE); // 2nd parameter will return db object,3rd parameter will return active record enable for this db connection;
$this->load->model("example_model");
}
2) You can do the same without overwriting the current db connection like this
function connnectExternalDB()
{
$dbdriver = $this->input->get_post('dbdriver'); //mysqli or mysql
$username = $this->input->get_post('username'); // DB user name
$password = $this->input->get_post('password'); // DB password
$hostname = $this->input->get_post('hostname'); // DB host url
$database = $this->input->get_post('database'); // DB name
$dsn_string = "$dbdriver://$username:$password#$hostname/$database";
// Following will overwrite your current DB connection
$DB_OBJ = $this->load->database($dsn_string, TRUE, TRUE); // 2nd parameter will return db object,3rd parameter will return active record enable for this db connection;
$query = "select * from user";
$user_list = $DB_OBJ->query($query)->result_array();
}
It's in database.php in applications/config/ directory not config.php

Codeigniter: Change arguments of database dynamically

Can we change arguments like host-name, user-name, password and database dynamically or we must go to database.php to change it.
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '****';
$db['default']['database'] = 'database';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
Is there any possibility to change them dynamically?
I think something like this would work, in a model or Library.
$dynamic = array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '*****',
'database' => 'database'
);
$test['hostname'] = $dynamic['hostname'];
$test['username'] = $dynamic['username'];
$test['password'] = $dynamic['password'];
$test['database'] = $dynamic['database'];
$test['dbdriver'] = 'mysqli';
$test['dbprefix'] = '';
$test['pconnect'] = FALSE;
$test['db_debug'] = TRUE;
$test['cache_on'] = FALSE;
$test['cachedir'] = '';
$test['char_set'] = 'utf8';
$test['dbcollat'] = 'utf8_general_ci';
$test_db = $this->load->database($test, true);
Then you would access this database, like this;
$test_db->get('users');
Where are you planning on getting the $dynamic settings from?

Connecting oracle 11g with codeigniter

I am trying to connect oracle 11g with codeigniter(V-2.1.4) using xampp(v-1.7.3) . I have changed my config.php to this :
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'system';
$db['default']['password'] = 'root';
$db['default']['database'] = 'orcl';
$db['default']['dbdriver'] = 'oci8';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
and also changed var $dbdriver = 'oci8'; in DB_driver.php file.
But um not able to connect with db. I am getting some sort of database error . It says :
Unable to connect to your database server using the provided settings.
Filename: G:\F\installed\Xampp\xampp\htdocs\codeigniter\system\database\DB_driver.php
Line Number: 124
I am giving line 118 - 127 of DB_driver.php here :
if ( ! $this->conn_id)
{
log_message('error', 'Unable to connect to the database');
if ($this->db_debug)
{
$this->display_error('db_unable_to_connect'); (this is line 124)
}
return FALSE;
}
I want to add that when i try to connect with the following code i am able to see "Succesfully connected with Oracle DB :-)" this message.
conn=oci_connect("system","root","localhost/orcl");
If (!$conn)
echo "Failed to connect to Oracle";
else
echo "Succesfully connected with Oracle DB :-)";
make your database file config as below
host will be IP whre the oracle is installed..
$db['default']['hostname'] = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = xxx.xxx.xx.xx)(PORT = 1521))
(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XYZ)))';
$db['default']['username'] = "USERNAME";
$db['default']['password'] = "PASSWORD";
$db['default']['database'] = "DATABASE";
$db['default']['dbdriver'] = "oci8";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
try this configuration :
$db['default']['hostname'] = 'localhost/orcl'; // orcl is instance name
$db['default']['username'] = 'system';
$db['default']['password'] = 'root';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'oci8';
and
$db['default']['db_debug'] = FALSE; // default TRUE
this work for me..

Configuring config/database.php file using php script

I have a script in my controller which is as follows:
if($this->db->query("CREATE DATABASE IF NOT EXISTS db_ecommerce"))
{
$sql=file_get_contents('./databse_backup/backup.sql');
foreach (explode(";\n", $sql) as $sql)
{
$sql = trim($sql);
//echo $sql.'<br/>============<br/>';
if($sql)
{
$this->db->query($sql);
}
}
}
It creates a database and then runs a backup sql file.
My issue is that we need to configure the config/database.php file beforehand.
First I want this script to run and after that the database.php file should be changed.
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'db_name';
Since its now for localhost hence,
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'db_name';
An approach would be to have 'template file' in your installation folder like:
ex. database.php
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = '<?php echo $hostname;?>';
$db['default']['username'] = '<?php echo $username;?>';
$db['default']['password'] = '<?php echo $password;?>';
$db['default']['database'] = '<?php echo $database;?>';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '<?php echo $prefix;?>';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
and then you can get the values for db connection (from a form for example) and write the new file in your codeigniter application (application/config/database.php)
ex.
$settings['hostname'] = $this->input->post('hostname');
$settings['username'] = $this->input->post('username');
$settings['password'] = $this->input->post('password');
$settings['database'] = $this->input->post('database');
$settings['prefix'] = $this->input->post('prefix');
$file_contents = $this->load->view('templates/database', $settings, true);
write_file($_SERVER['DOCUMENT_ROOT'].'application/config/database.php', $file_contents);

CodeIgniter MultiDatabse connect error

I see a lot of method/tutorial to connect multi database in CodeIgniter? Still far far away with me. Some one provide me to connect multi database in CI. This is my using way i was found it in a blog. pardon me, i don't recognize the blog address.
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'vk_global';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
//add alternate database settings
$db['wow']['hostname'] = 'localhost';
$db['wow']['username'] = 'root';
$db['wow']['password'] = 'root';
$db['wow']['database'] = 'auth';
$db['wow']['dbdriver'] = 'mysql';
$db['wow']['dbprefix'] = '';
$db['wow']['pconnect'] = TRUE;
$db['wow']['db_debug'] = TRUE;
$db['wow']['cache_on'] = FALSE;
$db['wow']['cachedir'] = '';
$db['wow']['char_set'] = 'utf8';
$db['wow']['dbcollat'] = 'utf8_general_ci';
$db['wow']['swap_pre'] = '';
$db['wow']['autoinit'] = TRUE;
$db['wow']['stricton'] = FALSE;
i was call it with this code
$DB2 = $this->load->database('auth',true);
and i was insert a simple row with this code
$this->$DB2->insert('events_grouptable',$data);
i got this error You have specified an invalid database connection group.
how should i solve this error?
$DB1 = $this->load->database('default', TRUE);
$DB2 = $this->load->database('wow', TRUE);
You need to set which db to use with which group otherwise the alternative does not work as
$active_group = 'default';
More details are here http://ellislab.com/codeigniter/user-guide/database/connecting.html

Resources