8. Sessions and Cookies

Session and Cookie classes in Eden was mainly included as a wrapper for these variables. Both session and cookie classes can be accessed as arrays in the following manner.

Figure 1. Sessions and cookies as arrays.
$session = eden('session')->start();		//instantiate

$session['name'] = 'value';			//set 'name' to 'value' in session data
echo $session['name'];				//get session data where key is 'name'
unset($session['name']);			//unset session data where key is 'name'
isset($session['name']);			//returns true if a key called 'name' exists

foreach($session as $key => $value) {}	//loop through session data

echo $session; // outputs a json version of the session data

$cookie = eden('cookie');			//instantiate

$cookie['name'] = 'value';			//set 'name' to 'value' in session data (no expiration)
echo $cookie['name'];				//get session data where key is 'name'
unset($cookie['name']);				//unset session data where key is 'name'
isset($cookie['name']);				//returns true if a key called 'name' exists

foreach($cookie as $key => $value) {}	//loop through cookie data

echo $cookie; // outputs a json version of the cookie data

Eden's session object directly works with PHP's $_SESSION property, so setting properties in either way will do the exact same thing. Similar to sessions, cookies work in the exact same manor. Both were created to compliment these PHP variables.

Figure 2. Session Methods
$session->start();					//starts session
$session->getId();					//get session id

$session->set('name', 'value');		//set 'name' to 'value' in session data
$session->get('name');				//get session data where key is 'name'
$session->remove('name');			//unset session data where key is name
$session->clear();					//remove all session data
Figure 3. Cookies Methods

$cookie->set('name', 'value');		//set 'name' to 'value' in cookie data
$cookie->set('name', 'value', 3600, 'some/path', 'openovate.com');	//set 'name' to 'value' located in some/path in openovate.com, expires in 1 hour
$cookie->setSecure('name', 'value'); //set 'name' to 'value' in cookie data securely
$cookie->get('name');				//get cookie data where key is 'name'
$cookie->remove('name');			//unset cookie data where key is name
$cookie->clear();					//remove all cookie data

This section covered how we store and manage persistent data in PHP. Data handling from project to project usually gets really messy and an unnecessary complication towards the end of projects. One of the most weirdest yet effective solutions Eden has concocted is our Registry Class designed for better data management. Visit our next section 9. The Registry when your ready to continue.


© 2012 Openovate Labs. All rights reserved.