{"id":709,"date":"2013-06-01T08:57:30","date_gmt":"2013-06-01T06:57:30","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=709"},"modified":"2013-06-18T20:56:35","modified_gmt":"2013-06-18T18:56:35","slug":"installing-codeigniter-on-wamp-server-part-2","status":"publish","type":"post","link":"https:\/\/www.extradrm.com\/?p=709","title":{"rendered":"Managing CodeIgniter sessions in mysql &#8211; Part 2"},"content":{"rendered":"<img data-del=\"avatar\" src='https:\/\/www.extradrm.com\/wp-content\/uploads\/2020\/10\/Michel-haddad-150x150.jpg' class='avatar pp-user-avatar avatar-thumbnailwp-user-avatar wp-user-avatar-thumbnail photo ' width='150'\/>\n<p>For some more complex applications that require little security via login panel, we need to manage codeigniter sessions in the database. Otherwise, an old session could be restored by a user modifying their cookies.<\/p>\n<p>When session data is available in a database, every time a valid session is found in the user&#8217;s cookie, a database query is performed to match it. If the session ID does not match, the session is destroyed. Session IDs can never be updated, they can only be generated when a new session is created.<\/p>\n<h2>Configure CodeIgniter<\/h2>\n<h3>Database Access<\/h3>\n<p>Update the file application\/config\/database.php in your CodeIgniter installation with your database info:<\/p>\n<pre>$db['default']['hostname'] = 'localhost';\r\n$db['default']['username'] = 'yourdbusername';\r\n$db['default']['password'] = 'yourdbpassword';\r\n$db['default']['database'] = 'yourdbname';<\/pre>\n<p>In order to store sessions, you must first create a database table for this purpose. Here is the basic prototype (for MySQL) required by the session class :<\/p>\n<pre>CREATE TABLE IF NOT EXISTS  `ci_sessions` (\r\n\tsession_id varchar(40) DEFAULT '0' NOT NULL,\r\n\tip_address varchar(45) DEFAULT '0' NOT NULL,\r\n\tuser_agent varchar(120) NOT NULL,\r\n\tlast_activity int(10) unsigned DEFAULT 0 NOT NULL,\r\n\tuser_data text NOT NULL,\r\n\tPRIMARY KEY (session_id),\r\n\tKEY `last_activity_idx` (`last_activity`)\r\n);<\/pre>\n<p>Note: By default the table is called ci_sessions, but you can name it anything you want as long as you update the application\/config\/config.php file so that it contains the name you have chosen. Once you have created your database table you can enable the database option in your config.php file as follows:<\/p>\n<pre>$config['sess_use_database'] = TRUE;<\/pre>\n<p>Once enabled, the Session class will store session data in the DB.<br \/>\nMake sure you&#8217;ve specified the table name in your config file as well:<\/p>\n<pre>$config['sess_table_name'] = 'ci_sessions';<\/pre>\n<h3>Default Libraries<\/h3>\n<p>In the file application\/config\/autoload.php you can configure the default libraries you want to load in all your controllers. For our case, we\u2019ll load the database and session libraries, since we want to handle user sessions :<\/p>\n<pre>$autoload['libraries'] = array('database','session');<\/pre>\n<p>and also the URL helper for internal link generation :<\/p>\n<pre>$autoload['helper'] = array('url');<\/pre>\n<h3>Encryption Key<\/h3>\n<p>Even if you are not using encrypted sessions, you must set an <strong>encryption key<\/strong> in your config file which is used to aid in preventing session data manipulation.<br \/>\nTo save your key to your application\/config\/config.php, open the file and set:<\/p>\n<pre>$config['encryption_key'] = \"VERY LONG RANDOM KEY\";<\/pre>\n<p>For example :<\/p>\n<pre>$config['encryption_key'] = \"12?*RrfgysyuEssssZ90\";<\/pre>\n<p>To read Part 3, on simple login tutorial with codeIgniter click Here (coming soon).<br \/>\nCheers<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For some more complex applications that require little security via login panel, we need to manage codeigniter sessions in the database. Otherwise, an old session could be restored by a user modifying their cookies.&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2841,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,35],"tags":[191,192,193,194,195],"youtube_video":null,"_links":{"self":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/709"}],"collection":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=709"}],"version-history":[{"count":0,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/709\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/media\/2841"}],"wp:attachment":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}