{"id":363,"date":"2018-01-14T16:06:19","date_gmt":"2018-01-14T16:06:19","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=363"},"modified":"2018-01-14T16:06:19","modified_gmt":"2018-01-14T16:06:19","slug":"php-sessions","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/php-sessions\/","title":{"rendered":"PHP Sessions"},"content":{"rendered":"<p><strong>What is a PHP Session?<\/strong><\/p>\n<p>When you work on your computer you run the application open it, make the changes and then close it. This is called one session that user has performed on the particular application. But on the internet storing such data is difficult.<\/p>\n<p>This is where the session comes in place. The user sessions last until the user closes the browser. You can store the username, favorite color etc as a variable value in the sessions. The session variable holds the information about the one single user and runs through all the pages in one application.<\/p>\n<p><strong>Start a PHP Session<\/strong><\/p>\n<p>Use php <code>session_start()<\/code> function to start the session. The session variables are set with the php global variable <code>$_SESSION<\/code><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n\/\/ Start the session<br \/>\nsession_start();<br \/>\n?&gt;[\/php]<br \/>\n<!DOCTYPE html><br \/>\n[php]&lt;html&gt;<br \/>\n&lt;body&gt;<\/p>\n<p>&lt;?php<br \/>\n\/\/ Set session variables<br \/>\n$_SESSION[&quot;favcolor&quot;] = &quot;green&quot;;<br \/>\n$_SESSION[&quot;favanimal&quot;] = &quot;cat&quot;;<br \/>\necho &quot;Session variables are set.&quot;;<br \/>\n?&gt;<\/p>\n<p>&lt;\/body&gt;<br \/>\n&lt;\/html&gt;[\/php]<\/p>\n<p><strong>Code Example:<\/strong><\/p>\n<p>First, we have set the session by using <code>session_set()<\/code> function.<\/p>\n<p>[php]&lt;?php<br \/>\nsession_start();<br \/>\n?&gt;[\/php]<\/p>\n<p>Then in the HTML, we have to use the global session variable <code>$_SESSION<\/code> to set the two session values &#8220;<code>favcolor<\/code>&#8221; and &#8220;<code>favanimal<\/code>&#8221; which has value green and cat respectively.<\/p>\n<p><strong>Destroy a PHP Session<\/strong><\/p>\n<p>To delete all global session variables use the <code>session_unset()<\/code> and <code>session_destroy()<\/code> functions to delete the session.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is a PHP Session? When you work on your computer you run the application open it, make the changes and then close it. This<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-363","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/comments?post=363"}],"version-history":[{"count":1,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/363\/revisions"}],"predecessor-version":[{"id":364,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/363\/revisions\/364"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}