{"id":446,"date":"2012-12-01T17:15:33","date_gmt":"2012-12-01T16:15:33","guid":{"rendered":"http:\/\/codingexplained.com\/?p=446"},"modified":"2017-06-11T22:23:01","modified_gmt":"2017-06-11T20:23:01","slug":"using-sessions-in-zend-framework-2","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2","title":{"rendered":"Using Sessions in Zend Framework 2"},"content":{"rendered":"<p>As of this writing, there is no official documentation regarding how to use sessions in Zend Framework 2. This article is in no way aiming to be a substitution, but merely a quick introduction on how to get started with sessions in Zend Framework 2.<\/p>\n<p>Let us say, for instance, that you want to keep the username of a user in a session when they log in so that you can access it whenever you need it. This is very simple to accomplish.<\/p>\n<pre><code class=\"php\">namespace MyApplication\\Controller;\r\n\r\nuse Zend\\Mvc\\Controller\\AbstractActionController;\r\nuse Zend\\Session\\Container; \/\/ We need this when using sessions\r\n\r\nclass UserController extends AbstractActionController {\r\n\tpublic function loginAction() {\r\n\t\t\/\/ Store username in session\r\n\t\t$user_session = new Container('user');\r\n\t\t$user_session->username = 'Andy0708';\r\n\r\n\t\treturn $this->redirect()->toRoute('welcome');\r\n\t}\r\n\r\n\tpublic function welcomeAction() {\r\n\t\t\/\/ Retrieve username from session\r\n\t\t$user_session = new Container('user');\r\n\t\t$username = $user_session->username; \/\/ $username now contains 'Andy0708'\r\n\t}\r\n}<\/code><\/pre>\n<p>It could barely be any easier, right? Let us take a look at what is actually happening. First we instantiate the <span class=\"code\">Zend\\Session\\Container<\/span> class and pass a string (a name). This name is optional; if one is not specified, &#8220;Default&#8221; will be used as per the class&#8217; constructor. You can think of this name as a namespace; in this way, it is possible to use identical session keys in different containers (or &#8220;namespaces&#8221;). For example, you may have an &#8220;id&#8221; key on both &#8220;product&#8221; and &#8220;user&#8221;. In this case, you could create two containers &#8211; &#8220;product&#8221; and &#8220;user&#8221; respectively &#8211; and then have &#8220;id&#8221; keys in both. In this way, the keys do not interfere with each another.<\/p>\n<p>The <span class=\"code\">Zend\\Session\\Container<\/span> class extends PHP&#8217;s <span class=\"code\">ArrayObject<\/span> and uses the <span class=\"code\">ArrayObject::ARRAY_AS_PROPS<\/span> &#8220;option&#8221;. What this means is that key values can be read and set as properties like in the example above. What actually happens is that the <span class=\"code\">Container<\/span> class&#8217; <span class=\"code\">offsetGet<\/span> and <span class=\"code\">offsetSet<\/span> methods are called. These methods are defined in the <span class=\"code\">ArrayAccess<\/span> interface, which is implemented by the <span class=\"code\">ArrayObject<\/span> class. They are then overridden by the <span class=\"code\">Container<\/span> class. Apologies if that is confusing; in reality, you do not have to know this to make use of sessions; but if you were interested, then that is what happens internally.<\/p>\n<p>Something else interesting happens; the <span class=\"code\">$_SESSION<\/span> superglobal array is replaced with an instance of <span class=\"code\">Zend\\Session\\Storage\\SessionStorage<\/span>. Let us take a look at how the <span class=\"code\">$_SESSION<\/span> variable looks like.<\/p>\n<pre><code class=\"php\">Zend\\Session\\Storage\\SessionStorage Object\r\n(\r\n    [isImmutable:protected] => \r\n    [storage:ArrayObject:private] => Array\r\n        (\r\n            [__ZF] => Array\r\n                (\r\n                    [_REQUEST_ACCESS_TIME] => 1354373227.1111\r\n                )\r\n\r\n            [user] => ArrayObject Object\r\n                (\r\n                    [storage:ArrayObject:private] => Array\r\n                        (\r\n                            [username] => Andy0708\r\n                        )\r\n\r\n                )\r\n\r\n        )\r\n\r\n)<\/code><\/pre>\n<p>The <span class=\"code\">SessionStorage<\/span> class also extends the <span class=\"code\">ArrayObject<\/span> class (actually, it extends <span class=\"code\">ArrayStorage<\/span>, which extends <span class=\"code\">ArrayObject<\/span>), and thus we can use the object as an array. Because of this, the two ways of accessing a session key below are equivalent.<\/p>\n<pre><code class=\"php\">$username = $user_session->username;\r\n$username = $_SESSION['user']['username'];<\/code><\/pre>\n<p>Hopefully this has helped you learn how to use sessions in Zend Framework 2 and, if you were interested, what happens behind the scenes as well.<\/p>\n<p>Thank you for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article gives an introduction on how to use sessions in Zend Framework 2. As it turns out, it is actually very easy. Information on what happens behind the scenes is also provided, so if you are the kind of person who likes to know the details, then this article is also for you.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[38],"tags":[65,40,64,39,66],"series":[],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using Sessions in Zend Framework 2<\/title>\n<meta name=\"description\" content=\"Learn how to easily use sessions in Zend Framework 2. The article also covers what happens when you create or access a session in ZF2.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Sessions in Zend Framework 2\" \/>\n<meta property=\"og:description\" content=\"Learn how to easily use sessions in Zend Framework 2. The article also covers what happens when you create or access a session in ZF2.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2\" \/>\n<meta property=\"og:site_name\" content=\"Coding Explained\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:published_time\" content=\"2012-12-01T16:15:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-11T20:23:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"444\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Bo Andersen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:site\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bo Andersen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2\",\"url\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2\",\"name\":\"Using Sessions in Zend Framework 2\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2012-12-01T16:15:33+00:00\",\"dateModified\":\"2017-06-11T20:23:01+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"Learn how to easily use sessions in Zend Framework 2. The article also covers what happens when you create or access a session in ZF2.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Sessions in Zend Framework 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codingexplained.com\/#website\",\"url\":\"https:\/\/codingexplained.com\/\",\"name\":\"Coding Explained\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codingexplained.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\",\"name\":\"Bo Andersen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"caption\":\"Bo Andersen\"},\"description\":\"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!\",\"sameAs\":[\"https:\/\/codingexplained.com\",\"https:\/\/www.facebook.com\/codingexplained\",\"https:\/\/www.linkedin.com\/in\/ba0708\",\"https:\/\/twitter.com\/codingexplained\",\"https:\/\/www.youtube.com\/c\/codingexplained\"],\"url\":\"https:\/\/codingexplained.com\/author\/andy\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using Sessions in Zend Framework 2","description":"Learn how to easily use sessions in Zend Framework 2. The article also covers what happens when you create or access a session in ZF2.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2","og_locale":"en_US","og_type":"article","og_title":"Using Sessions in Zend Framework 2","og_description":"Learn how to easily use sessions in Zend Framework 2. The article also covers what happens when you create or access a session in ZF2.","og_url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2012-12-01T16:15:33+00:00","article_modified_time":"2017-06-11T20:23:01+00:00","og_image":[{"width":1200,"height":444,"url":"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png","type":"image\/png"}],"author":"Bo Andersen","twitter_card":"summary_large_image","twitter_creator":"@codingexplained","twitter_site":"@codingexplained","twitter_misc":{"Written by":"Bo Andersen","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2","url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2","name":"Using Sessions in Zend Framework 2","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2012-12-01T16:15:33+00:00","dateModified":"2017-06-11T20:23:01+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"Learn how to easily use sessions in Zend Framework 2. The article also covers what happens when you create or access a session in ZF2.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/using-sessions-in-zend-framework-2#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Using Sessions in Zend Framework 2"}]},{"@type":"WebSite","@id":"https:\/\/codingexplained.com\/#website","url":"https:\/\/codingexplained.com\/","name":"Coding Explained","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codingexplained.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d","name":"Bo Andersen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","caption":"Bo Andersen"},"description":"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!","sameAs":["https:\/\/codingexplained.com","https:\/\/www.facebook.com\/codingexplained","https:\/\/www.linkedin.com\/in\/ba0708","https:\/\/twitter.com\/codingexplained","https:\/\/www.youtube.com\/c\/codingexplained"],"url":"https:\/\/codingexplained.com\/author\/andy"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3mJkW-7c","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/446"}],"collection":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/comments?post=446"}],"version-history":[{"count":14,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/446\/revisions"}],"predecessor-version":[{"id":3049,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/446\/revisions\/3049"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=446"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}