CodeIgniter session 'set_userdata' does not work - codeigniter

I am trying to start the session with CodeIgniter, but it does not show any error either. I tried to change the session's driver to database and also I tried to specify the session path, but I didn't work.
$session_data = array(
'is_login' => TRUE,
'username' => $user->username,
'id' => $user->id,
'type' => $user->type,
'name' => $user->name,
);
$this->session->set_userdata($session_data);
From Comment
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = sys_get_temp_dir();
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

First open the file located at application/config/autoload.php
Find the following section.
The $autoload['libraries'] = array(); array holds the list of libraries that need to be autoloaded.
As per our requirement, let's change it to look like this:
$autoload['libraries'] = array('session');
Also, there's another way you could have achieved that. You can use the following code somewhere in your controller file to load the session library.
$this->load->library('session');

Related

Cart showing 0 Items when using Codeigniter Shopping Cart Library

I am using Codeigniter 3.1.9, and added items to my shopping cart, items are added and showing but when i put my system to hibernate mode with browser open, then after turning on my sytem, the cart shows 0 products. It should contain the item that i have added. Please help to sort out my issue.
My add to cart code snippet is:
$data = array(
'id' => $product['id'],
'qty' => 1,
'size' => $size,
'name' => $product['title'],
'image' => $product['image']
);
$this->cart->insert($data);
this is config file:
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
Codeigniter cart library save data in session. after hibernate, the session got destroy and your cart got cleared.

Codeigniter: What is the best practice to maintain multiple database connection datas?

My typical development pattern is:
1 - local work machine
2 - online testing server
3 - production server.
Each of these has a database with unique connection data.
So for my projects I use a config file that implements a switch( what_environment ){ // assign appropriate db connection info } that assigns the proper db connection info (host, username, password, db name, ...) depending on the environment.
However in codeigniter's /config/database.php, there is no such construct, but rather just one set of properties. Obviously I can put my own server-sniffing switch statement in there. But I am presuming that there is a codeigniter way of handling this.
Can anyone shed light on the proper codeigniter way of maintaining multiple db connection infos?
On your application/config/constants.php write:
define('ENVIRONMENT', 'development');
switch(ENVIRONMENT):
case 'development':
# DB
defined('DB_HOST') ? null : define('DB_HOST', 'localhost');
defined('DB_USER') ? null : define('DB_USER', 'root');
defined('DB_PASSWORD') ? null : define('DB_PASSWORD', '');
defined('DB_NAME') ? null : define('DB_NAME', 'dev_db');
break;
case 'production':
# DB
defined('DB_HOST') ? null : define('DB_HOST', 'production');
defined('DB_USER') ? null : define('DB_USER', 'production_user');
defined('DB_PASSWORD') ? null : define('DB_PASSWORD', '12345');
defined('DB_NAME') ? null : define('DB_NAME', 'production_db');
break;
endswitch;
Then on your application/config/database.php
$db['default'] = array(
'hostname' => DB_HOST,
'username' => DB_USER,
'password' => DB_PASSWORD,
'database' => DB_NAME,
);
I do not know if this is the best solution, however, it is the simplest to implement and always worked very well for me:
After the declaration of $db['default']:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '...',
'password' => '...',
...
);
I add this code:
if (ENVIRONMENT == 'development'){
$db['default']['username'] = 'root';
$db['default']['password'] = 'XXXXX';
$db['default']['database'] = 'developmentdatabasename';
}
In the index.php of your project:
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
if you use apache - activate SetEnvIf (if it itsn't already)
after that put in your htaccess
SetEnvIf Host localhost$ CI_ENV=development
SetEnvIf Host testhost$ CI_ENV=testing
SetEnvIf Host productionhost$ CI_ENV=production
now just simply create in your application/config/ folder 3 folder named development, testing and production
and if you put now your different config files in this folders - ci accesses it depending on your current working environment
please follow the CI Documentation for further Information
At your config/database.php you can set multiple environment like this
$active_group = 'local';
if(ENVIRONMENT=='development')
{
$active_group = 'online';
}
elseif(ENVIRONMENT=='production')
{
$active_group = 'production';
}
$db['local'] = array(
'dsn' => '',
'hostname' => 'xxxx',
'username' => '',
//other configs
);
$db['online'] = array(
'dsn' => '',
//other configs
);
$db['production'] = array(
'dsn' => '',
//other configs
);
You can do it lots of way.See documentation

Codeigniter session lost

I use codeigniter for a project, my session is configured to use the mysql database :
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
In my controller i have a code like this :
$params = array(
"type" => $data['activite_name'],
"activite" => $data['activite'],
"nom" => $data['name'],
"prenom" => $data['fname'],
"tel" => $data['tel'],
"email" => $data['email'],
"adresse" => $data['adr'],
"cp" => $data['cp'],
"ville" => $data['ville']
);
....
#Load session library and save $params as session
$this->load->library('session');
$this->session->set_userdata($params);
#Test session
$sess_name = $this->session->userdata('name');
echo $sess_name;
#Load view
$this->load->view("templates/header_interne", $data);
$this->load->view("pages/payment_view", $data);
$this->load->view("templates/footer", $data);
So when payment_view is loaded i can see my $sess_name value, but when i refresh the page i lost this session, and so on for all my sessions.
Anyone has an idea please?
Thank you.

CodeIgniter - can't delete particular cache

I have this model:
public function Vote($data) {
$gameid = $this->input->post('gameid');
$userid = $this->input->post('userid');
$rate = $this->input->post('rate');
$ins = array(
'user_id' => $userid,
'game_id' => $gameid,
'rate' => $rate
);
$q = $this->db->insert('rates', $ins);
$this->db->cache_delete('games',$gameid);
}
It inserts data and then it should clear the cache. But it's not - it does nothing. When I go with cache_delete_all it works. What am I doing wrong?
Problem solved, but not without a little shooting in the dark ;) My problem was the configuration for database. In config/database.php I had $db['default']['cachedir'] = 'dbcache';. Adding slash in the end helped!
Still, you have to remember to have your cache dir in root. Having it in, eg., application folder will also provide errors like mine.

How can I use the default Joomla's database username and password, to connect to another database?

Basically I just want to connect to the same host but a different database in a Joomla module. So how can i get the user/pass info from the config to avoid the need to hard code or parametrize that info, since its already there.
Thanks
You can use this code ( it's the same code that's in the JFactory::getDBO() method ).
jimport('joomla.database.database');
jimport('joomla.database.table');
$conf = JFactory::getConfig();
$host = $conf->get('host');
$user = $conf->get('user');
$password = $conf->get('password');
$database = 'YOUR_DATABASE_NAME';
$prefix = $conf->get('dbprefix'); //***Change this if the dbprefix is not the same!***
$driver = $conf->get('dbtype');
$options = array ('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix);
$db = JDatabase::getInstance($options);
I hope it helped!
Get config values like this:
$app = JFactory::getApplication();
echo $app->getCfg('user');
echo $app->getCfg('password');

Resources