<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CodeIgniter Tutorial</title>
	<atom:link href="http://www.gotphp.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gotphp.com</link>
	<description>Practical Coding Examples and Advice For PHP Developers</description>
	<lastBuildDate>Tue, 06 Sep 2011 01:01:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Storing Codeigniter Session Data to a Database</title>
		<link>http://www.gotphp.com/storing-codeigniter-session-data-to-a-database/54471/</link>
		<comments>http://www.gotphp.com/storing-codeigniter-session-data-to-a-database/54471/#comments</comments>
		<pubDate>Sat, 30 Oct 2010 04:57:08 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[codeigniter database]]></category>
		<category><![CDATA[codeigniter form]]></category>
		<category><![CDATA[codeigniter sessions]]></category>
		<category><![CDATA[codeigniter tutorial]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[sessions]]></category>

		<guid isPermaLink="false">http://www.gotphp.com/?p=471</guid>
		<description><![CDATA[One of the interesting things about CodeIgniter is how it creates and handles sessions. CodeIgniter doesn’t use native PHP sessions. Instead it uses a cookie to store the serialized data. This method, according to the guide, offers more flexibility for developers. In this example we’re going to try out the CI session class, and explore<a class="moretag" href="http://www.gotphp.com/storing-codeigniter-session-data-to-a-database/54471/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>One of the interesting things about CodeIgniter is how it creates and handles sessions. CodeIgniter doesn’t use native PHP sessions. Instead it uses a cookie to store the serialized data. This method, according to the guide, offers more flexibility for developers.</p>
<p>In this example we’re going to try out the CI session class, and explore storing session data in a database. We’ll use a simple ordering form to capture information, and play around the settings to get some pretty interesting results.</p>
<h2><strong>STEP 1: Configure CodeIgniter </strong></h2>
<p>Autoloading helpers and libraries is one of the ways CI helps simplify application development. So go ahead and load the&nbsp; database and session library on your config/autoload.php file . While we’re here, load the form and url helpers as well.</p>
<p>[sourcecode]<br />
$autoload['libraries'] = array(&#8216;database&#8217;, &#8216;session&#8217;);<br />
$autoload['helper'] = array(&#8216;form&#8217;, &#8216;url&#8217;);<br />
[/sourcecode]</p>
<p>On the same config folder, open up the config.php file and set up CI for saving session data to a database.</p>
<p>You want to look for:</p>
<p>[sourcecode]<br />
$config['sess_use_database']&nbsp;= TRUE;<br />
[/sourcecode]</p>
<p>Make sure to change that to TRUE. You can also specify the name of the database you want to use for your session data. Just look for:</p>
<p>[sourcecode]<br />
$config['sess_table_name']&nbsp;= &#8216;ci_sessions&#8217;;<br />
[/sourcecode]</p>
<p>By default the database is named <strong>ci_sessions</strong>, and that&#8217;s the name we&#8217;re going to use to create our database later on.</p>
<p>Now for this demonstration, we’re going to play around with the session expiration. So look for the line:</p>
<p>[sourcecode]<br />
$config['sess_expiration']&nbsp;= 7200;<br />
[/sourcecode]</p>
<p>Set the session expiration to 2 seconds. Normally, the session is set to expire in 2 days or 7200 seconds. But for this activity we want make multiple entries to our session database. So every two seconds the session expires and a new one can be created when the user submits new data from our ordering form.</p>
<h2><strong>STEP 2: Build the Database</strong></h2>
<p>Now that we have our config files set. Lets proceed to building our database. The CI user guide provides and excellent example of the basic prototype we can use for our database:</p>
<p>[sourcecode]<br />
CREATE TABLE IF NOT EXISTS&nbsp; `ci_sessions` (<br />
session_id varchar(40) DEFAULT &#8217;0&#8242; NOT NULL,<br />
ip_address varchar(16) DEFAULT &#8217;0&#8242; NOT NULL,<br />
user_agent varchar(50) NOT NULL,<br />
last_activity int(10) unsigned DEFAULT 0 NOT NULL,<br />
user_data text NOT NULL,<br />
PRIMARY KEY (session_id)<br />
);<br />
[/sourcecode]</p>
<p>Go ahead and take a look at the database table that was created. Inside the ci_sessions table you&#8217;ll see five fields:</p>
<ol>
<li>session_id</li>
<li>ip_address</li>
<li>user_agent</li>
<li>last_activity</li>
<li>user_data</li>
</ol>
<p>The user_data field is where our custom data will be stored later on.</p>
<h2><strong>STEP 3: Create the Form</strong></h2>
<p>Ok, we’ve got our database ready. Lets take it for a spin by building a simple ordering form. In our views folder, you want to create sessionexample.php file with the following code:</p>
<p>[sourcecode]<br />
&lt;html xmlns=&#8221;http://www.w3.org/1999/xhtml&#8221;&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text/html; charset=utf-8&#8243; /&gt;<br />
&lt;title&gt;Order Form&lt;/title&gt;<br />
&lt;link type=&#8221;text/css&#8221; rel=&#8221;stylesheet&#8221; href=&#8221;&lt;?php echo base_url();?&gt;css/style.css&#8221; &gt;<br />
&lt;/head&gt;</p>
<p>&lt;body&gt;<br />
&lt;h5&gt;Fruity Orders&lt;/h5&gt;<br />
&lt;?php<br />
$attributes = array(<br />
&#8216;name&#8217; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;=&gt; &#8216;orderform&#8217;<br />
, &#8216;id&#8217; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;=&gt; &#8216;orderform&#8217;<br />
);<br />
echo form_open(&#8216;sessionexample/process&#8217;, $attributes);</p>
<p>echo form_label(&#8216;Order for: &#8216;, &#8216;ordername&#8217;);<br />
echo form_input(&#8216;ordername&#8217;).&#8217;&lt;br /&gt;&#8217;;</p>
<p>echo form_label(&#8216;How Many: &#8216;, &#8216;quantity&#8217;);<br />
$data = array(<br />
&#8216;name&#8217; =&gt; &#8216;quantity&#8217;<br />
, &#8216;id&#8217; =&gt; &#8216;quantity&#8217;<br />
, &#8216;maxlength&#8217; =&gt; &#8217;2&#8242;<br />
, &#8216;size&#8217; =&gt; &#8217;1&#8242;<br />
);<br />
echo form_input($data).&#8217; &#8216;;</p>
<p>echo form_label(&#8216;Choose Item &#8216;, &#8216;choice&#8217;);<br />
$options = array(<br />
&#8216;apples&#8217; =&gt; &#8216;Apples&#8217;<br />
, &#8216;oranges&#8217; =&gt; &#8216;Oranges&#8217;<br />
, &#8216;pears&#8217; =&gt; &#8216;Pears&#8217;<br />
);<br />
echo form_dropdown(&#8216;choice&#8217;, $options, &#8216;apples&#8217;).&#8217;&lt;br /&gt;&#8217;;</p>
<p>echo form_submit(&#8216;submit&#8217;, &#8216;Send My Order&#8217;);<br />
echo form_close();</p>
<p>echo (isset($message) ? &#8220;&lt;div class=&#8217;message&#8217;&gt;&#8221;.$message.&#8221;&lt;/div&gt;&#8221; : NULL);<br />
?&gt;</p>
<p>&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/sourcecode]</p>
<h2><strong>STEP 4: Create the Controller</strong></h2>
<p>Now for the fun part. Create a file called sessionexample.php in the controllers folder. Inside it, we’re going to catch the data as the user submits his order, then store the data into our session using flashdata.</p>
<p>[sourcecode]<br />
class Sessionexample extends MY_Controller {<br />
function __construct(){<br />
parent::__construct();<br />
}</p>
<p>function index(){<br />
$this-&gt;load-&gt;view(&#8216;sessionexample&#8217;);<br />
}</p>
<p>function process(){<br />
$values = array(<br />
&#8216;ordername&#8217; &nbsp;&nbsp; &nbsp;=&gt; $this-&gt;input-&gt;post(&#8216;ordername&#8217;)<br />
, &#8216;quantity&#8217; &nbsp;&nbsp; &nbsp;=&gt; $this-&gt;input-&gt;post(&#8216;quantity&#8217;)<br />
, &#8216;choice&#8217; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;=&gt; $this-&gt;input-&gt;post(&#8216;choice&#8217;)<br />
);<br />
$this-&gt;session-&gt;set_flashdata($values);</p>
<p>$data['message'] &nbsp;&nbsp; &nbsp;= &#8220;You ordered &#8220;.$values['quantity'];<br />
$data['message'] &nbsp;&nbsp; &nbsp;.= &#8221; &#8220;.$values['choice'].&#8221; for &#8220;.$values['ordername'].&#8221;.&#8221;;</p>
<p>$this-&gt;load-&gt;view(&#8216;sessionexample&#8217;, $data);<br />
}<br />
}<br />
[/sourcecode]</p>
<p>Notice that after storing the user data, we retrieve it from the session object and send it back to our view as feedback for the user. You can take the application for a spin now or finish off with some styles.&nbsp; After as the session is created, CI automatically stores the information to our database. Since we tweaked the session expiration to 2 seconds, you can make multiple database entries to observe how CI stores the information.</p>
<h2><strong>STEP 5 (Optional): Finish with style </strong></h2>
<p>Let’s polish off our web application with styling. We’re also going to link a style sheet to our order form:</p>
<p>[sourcecode]<br />
&lt;link type=&#8221;text/css&#8221; rel=&#8221;stylesheet&#8221; href=&#8221;&lt;?php echo base_url();?&gt;css/style.css&#8221; &gt;<br />
[/sourcecode]</p>
<p>Go ahead and create style.css where we’re going to place the following styles:</p>
<p>[sourcecode]<br />
body {<br />
background: #EFB4B3;<br />
margin: 0; padding: 0;<br />
font-family: arial;<br />
}</p>
<p>#orderform {<br />
border: 1px solid black;<br />
margin: 15px auto 0; padding: 1em;<br />
width: 350px;<br />
-moz-border-radius: 5px;<br />
-webkit-border-radius: 5px;<br />
}</p>
<p>.message {<br />
background: #FFFFCC;<br />
margin: 15px auto 0; padding: 1em;<br />
width: 350px;<br />
-moz-border-radius: 5px;<br />
-webkit-border-radius: 5px;<br />
}</p>
<p>h5 {<br />
color: #336699;<br />
font-size:24px; font-weight:bold;<br />
margin-bottom: 2px;<br />
text-align: center;<br />
}</p>
<p>input[type=text], select {<br />
margin: 0 0 1em 0; padding: 2px;<br />
width: auto;<br />
border: 5px;<br />
font-size:14px;<br />
-moz-border-radius: 1px;<br />
-webkit-border-radius: 1px;<br />
}</p>
<p>input[type=submit]{<br />
background: #3399FF;<br />
border: none;<br />
padding: 6px;<br />
text-decoration: none;<br />
color: white; font-size: 12px;<br />
-moz-border-radius: 4px;<br />
-webkit-border-radius: 4px;<br />
}</p>
<p>input[type=submit]:hover{<br />
background: #0099CC;<br />
cursor: pointer;<br />
}<br />
[/sourcecode]</p>
<p>There you have it, a quick and simple demo on how to store CodeIgniter Session Data on a database.</p>
<div style="width: 1px;height: 1px;overflow: hidden">sessionexample</div>
<p><script type="text/javascript" src="http://forms.aweber.com/form/32/159230832.js"></script></p>
<p><strong>If you found this article useful, please share your comments below.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gotphp.com/storing-codeigniter-session-data-to-a-database/54471/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

