codeigniter session is not storing all data - codeigniter

I have the following code:
$session_data = array(
'user_id' => $user_id,
'username' => $username,
'group_id' => $group_id,
'logged_in' => TRUE
);
$this->session->set_userdata($session_data);
The user_id and logged_in is stored perfectly into db, but the username and group_id is stored with an "N". The structure of the system is "modular", in my older applications following just MVC would work just fine.
Did anybody had this problem before, and if you did could you give a little bit of help ?
EDIT:
all N values are the problem where instead should be the actual value of each data
below is the user_data field from the database
a:5:{s:9:"user_data";
s:0:"";s:7:"user_id";N;
s:8:"username";N;
s:5:"group";N;
s:9:"logged_in";b:1;}

You need to enable saving data in session.
Set in application/config/config.php your application to use database for saving sessions.
$config['sess_use_database'] = TRUE;
Create table in database ans also give same name to the table as it's set in config.php
CREATE TABLE IF NOT EXISTS ci_sessions (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(45) DEFAULT '0' NOT NULL,
user_agent varchar(120) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL,
PRIMARY KEY (session_id),
KEY last_activity_idx (last_activity)
);
Then, whenever you use sessions, codeigniter will relay on database and there, you can set "user_data" field with what ever you need. Perhaps, it would be good that you serialize that data ans save in "user_data" field.
Saving data in php session or cookie have certain limitations.

I fixed it. This had nothing to do with CI Session Library, I had an error in my database query :(
sorry for any inconvenience that I caused

Related

Why we need to store session in database codeigniter 3.0?

I have read the codeigniter document about session. but i still don't understand why they implement accessing session in database?
Session Library
Any comments would be appreciated :)
There is no big deal with it. It means there is no any Security Reason for it
Main purpose of this is store user logins, and managing those.
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(40) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
`data` blob NOT NULL,
PRIMARY KEY (id),
KEY `ci_sessions_timestamp` (`timestamp`)
);
Ex :timestamp will gather the user logging time. So you can get detail about how many time user log in to site
Codeigniter Store Session
why is it good save session in database
On codeigniter 3 you do not need to store sessions in database you can do it with out or use files
config.php starting from line 358
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 1440;
$config['sess_save_path'] = FCPATH . 'application/somefolder';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

How to get DataMapper to not specify a NULL default value

I'm getting an error when I use DataMapper's auto_upgrade! method to add fields in an SQLite3 db based on the properties defined in my code:
DataObjects::SyntaxError at /history
Cannot add a NOT NULL column with default value NULL
An example of an offending line would be:
property :fieldname, Text, required: true
The error goes away if I (a) remove the line, (b) remove required: true, (c) change true to false, or (d) add a default value.
SQLite does not require a default value to be specified for every field, so this problem is definitely with DataMapper, not SQLite.
How can I get around this, so DataMapper can specify that a field is required without assuming that not specifying a default value automatically means the default should be NULL?
(If you want to know more about why I'm designing this way: there will be another client process accessing SQLite and logging data into the SQLite database, while a Sinatra app will be pulling data out of the db for display in a browser. I want the database therefore to enforce the field requirements, but DM's auto_upgrade is a very convenient way to be able to upgrade the database as needed—so long as it doesn't foul things up in the process.)
You are requiring the field, hence it cannot be NULL. This is simple table properties.
When DataMapper runs auto_upgrade! it runs the SQL commands on the database.
CREATE TABLE Test
(
P_Id int NOT NULL,
lname varchar(255) NOT NULL,
fname varchar(255),
Address varchar(255),
City varchar(255)
)
And doing something like this won't work.
CREATE TABLE Test
(
P_Id int NOT NULL,
lname varchar(255) NOT NULL DEFAULT NULL,
fname varchar(255),
Address varchar(255),
City varchar(255)
)
I tested it in MySQL and this is the error.
02:52:43 CREATE TABLE TestTest ( P_Id int NOT NULL, lname
varchar(255) NOT NULL DEFAULT NULL, fname varchar(255), Address
varchar(255), City varchar(255) ) Error Code: 1067. Invalid default
value for 'lname' 0.062 sec
Correction: SQLite does allow you to create a table with such properties. However, when trying to insert anything to that table makes it throw an error whether the field is NULL or not. So DataMapper might be doing some sanitation for your before even creating the table.
It is not clear to me if you are creating a new table or modifying an existing one.
If you have an existing table and are trying to alter it with a column defined as NOT NULL, then you must provide a default value so that the existing rows can be migrated. The RDBMS needs to know what to put in the field for pre-existing rows.
If you are creating a new table, then the property definition you have should be fine.

Magento admin form not saving the decimal values with decimal

For one of my custom module I have following sql setup script
$installer = $this;
$installer->startSetup();
$installer->run("
CREATE TABLE {$this->getTable('wholesale')} (
`wholesale_id` int(11) unsigned NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '',
`percentage` float(5,3) NOT NULL,
`status` smallint(6) NOT NULL default '0',
`created_time` datetime NULL,
`update_time` datetime NULL,
PRIMARY KEY (`wholesale_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$installer->endSetup();
The value passed to this percentage` float(5,3) NOT NULL, column is saving with decimal but always with 00 for example. If I save a value 10.23 it save in database as 10.00.
Please help
I ran into the same issue. After clearing the cache with "Flush Magento cache", it started to save properly. In my case, I had altered the column and it was being cached.
I truncated the table and inserted one record manually and it worked for me.
I am not sure why this happened but this worked for me.

How to save session counts and retrieve the number of user access

I would like to know how to save the number of times a user access a webpage and return the session count to the user
Eg
A user opens his user profile on a web page and would be able to see
Users number of access: 20times.
I have created the page with Codeigniter and created a table in the database.
CREATE TABLE IF NOT EXISTS `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(45) DEFAULT '0' NOT NULL,
user_agent varchar(120) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL,
PRIMARY KEY (session_id),
KEY `last_activity_idx` (`last_activity`)
);
But I don't know how to save the number of times the user has accessed the webpage

Codeigniter - session userdata size

I store my session in database with Codeigniter,
maybe stupid my question, but i do not catched if session data has a size limit, and i can't understand what will happen in the app if reach that limit?
Thank you
There is no limit when using database to save sessions. CI serializes the data using serialize and saves it to database.
Only limit then could be the field in the database, which differ in cases:
TEXT 65,535 bytes ~64kb
MEDIUMTEXT 16,777,215 bytes ~16MB
LONGTEXT 4,294,967,295 bytes ~4GB
you can store user_data in database with TEXT field size, TEXT field can store ~64kb.
hello guys set user_data in database with BLOB field size for fine et it 's recommanded by codeigniter documentation
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(128) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
`data` blob NOT NULL,
KEY `ci_sessions_timestamp` (`timestamp`)
);

Resources