Why we need to store session in database codeigniter 3.0? - codeigniter

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;

Related

when i try to import sql file in c panel phpmyadmin - Specified key was too long; max key length is 767 bytes #1071

there are some similar questions but not what i exactly want.
how can i increase this limit. what is file destination where can i replace this number (767 bytes) with another.
-- Dumping structure for table jofr.categories
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`parent_id` int(10) unsigned DEFAULT NULL,
`order` int(11) NOT NULL DEFAULT '1',
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `categories_slug_unique` (`slug`),
KEY `categories_parent_id_foreign` (`parent_id`),
CONSTRAINT `categories_parent_id_foreign` FOREIGN KEY (`parent_id`) REFERENCES `categories` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
MySQL said: Documentation
#1071 - Specified key was too long; max key length is 767 bytes
You could set the default string length inside the AppServiceProvider.php file
app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
For more information, you can reference a similar question asked in Stackoverflow
This is a MySQL InnoDB limit, not a PHP one. You can only work around the limit using code, or you can potentially increase the limit using a database option.
The issue is with your index on slug. slug is defined as 255 characters, and you're using utf8mb4, which uses 4 bytes per character, so your index on slug would require 1020 bytes.
There are a couple workaround options.
1. Reduce the size of your field.
Instead of making your slug 255 characters, make it 191. 191 * 4 = 764 < 767. You can do this by specifying the field length in your migration, or by not specifying the length and setting \Illuminate\Support\Facades\Schema::defaultStringLength(191); as mentioned by others.
2. Reduce the size of your index.
You can keep your field size at 255, but tell MySQL to only index the first 191 characters. I don't know if Laravel migrations support this, but you can always try.
$table->index('slug(191)');
3. Enable the innodb_large_prefix database option with DYNAMIC row formats.
The innodb_large_prefix database option increases the key length limit to 3072 bytes. However, this option only affects tables that have a row format of DYNAMIC or COMPRESSED.
If you're on MySQL >= 5.7.7, the innodb_large_prefix option is enabled by default.
If you're on MySQL >= 5.7.9, the default row format is DYNAMIC, and the innodb_large_prefix option is enabled by default, so you wouldn't be having this issue, unless you've changed the defaults.
If you're on MySQL < 5.7.9, the default row format is COMPACT, so you'd need to figure out how to tell Laravel to use the DYNAMIC row format. If you want to do this for all tables, you can set 'engine' => 'InnoDB ROW_FORMAT=DYNAMIC', in your database config. If you only want to do this for one table, you'll need to run raw DB create statements in your migration file.
References:
MySQL create index documentation - information on key size, row formats, and partial indexes
MySQL innodb_large_prefix option
MySQL innodb_default_row_format option
problem is on your unique columns with a length bigger than 191
`slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
in this line replace varchar(255) with varchar(191) and this will be resolved
Read this.
edit your AppServiceProvider.php file and inside the boot method set a
default string length:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}

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`)
);

codeigniter session is not storing all data

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

Resources