{"id":18,"date":"2009-10-21T01:21:52","date_gmt":"2009-10-21T06:21:52","guid":{"rendered":"http:\/\/www.theslinky.com\/?p=18"},"modified":"2009-10-21T01:21:52","modified_gmt":"2009-10-21T06:21:52","slug":"check-if-php-session-has-already-been-started","status":"publish","type":"post","link":"https:\/\/theslinky.com\/?p=18","title":{"rendered":"Check if PHP session has already been started"},"content":{"rendered":"<p>One of the challenges I came across is trying to figure out if a PHP session has already been started. I have an admin section on my site which operates using a PHP session. For the rest of the site, I wanted to display an &#8220;admin toolbar&#8221; if an admin was logged in. However, I did not want to call start_session on every page because that would initialize unnecessary sessions (hence using up resources on my server every time someone viewed a page). I only wanted to call session_start if a session was started\/initialized in the admin portion of the site.<\/p>\n<p>Turns out the solution is very simple. A typical setup of PHP sessions uses cookies to store the session ID on the person&#8217;s computer. All we need to do is check to see if this cookie is set on someone&#8217;s computer. If it is, call session_start () and check to see if the person has a successful login in the admin.<\/p>\n<pre>if (isset ($_COOKIE[session_name ()])) {\n    session_start ();\n    if (isset ($_SESSION['logged_in'])) {\n        echo \"You are an admin!\";\n    }\n}<\/pre>\n<p>Of course in our admin section, we&#8217;d set the session variable $_SESSION[&#8216;logged_in&#8217;] if there was a successful admin login.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you want to check if a PHP session has been started on another page before you call session_start () on the rest of your pages. This will prevent initializing unnecessary sessions thus saving you server resources. Solution: check for the session cookie.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/theslinky.com\/index.php?rest_route=\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theslinky.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theslinky.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theslinky.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theslinky.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18"}],"version-history":[{"count":0,"href":"https:\/\/theslinky.com\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions"}],"wp:attachment":[{"href":"https:\/\/theslinky.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theslinky.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theslinky.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}