{"id":1431,"date":"2017-03-21T14:58:25","date_gmt":"2017-03-21T09:28:25","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1431"},"modified":"2018-10-30T11:33:12","modified_gmt":"2018-10-30T06:03:12","slug":"php-check-php-session-already-started","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/php-check-php-session-already-started\/","title":{"rendered":"Check if PHP session has already started"},"content":{"rendered":"<h3 id=\"solution-1\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 1<\/label><\/span><\/h3>\n<p>As you probably already know, <strong>session_start<\/strong> is an in-built <a href=\"https:\/\/www.wikitechy.com\/php\/php-functions\" target=\"_blank\" rel=\"noopener\">PHP function<\/a> that will start a new session.<\/p>\n<h4 id=\"the-problem-is\"><span style=\"color: #800000;\"><strong>The problem is:<\/strong><\/span><\/h4>\n<ul>\n<li>If you call it more than once, your script will throw an <strong>E_NOTICE<\/strong> error.<\/li>\n<li>Although the solution seems pretty straight forward (don\u2019t call it more than once), in certain scenarios, you won\u2019t be entirely sure if a session has already been started or not.<\/li>\n<li>In some cases, it might be out of your control.<\/li>\n<li>There are two ways to approach this.<\/li>\n<\/ul>\n<p>If you are using a PHP version that is <strong>lower than 5.4.0,<\/strong> you can do the following:<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0Aif(session_id()%20%3D%3D%20\u201d)%7B%0A%20%20%20%20%2F%2Fsession%20has%20not%20started%0A%20%20%20%20session_start()%3B%0A%7D%0A%20%0Avar_dump(session_id())%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>If you run the code above, you\u2019ll see that a session is always started. This is because:<\/li>\n<li>We check to see if the function <strong>session_id<\/strong> returns an empty string.<\/li>\n<li>If session_id returns an <strong>empty string<\/strong>, we can conclude that the <a href=\"https:\/\/www.wikitechy.com\/tutorials\/laravel\/laravel-session\" target=\"_blank\" rel=\"noopener\">session<\/a> has <strong>not<\/strong> been <strong>started<\/strong> yet.<\/li>\n<li>If this is the case, we can start the session by calling the<strong> function session_start.<\/strong><\/li>\n<li>In PHP version 5.4.0 and above, we can make use of the <strong>function session_status<\/strong>, which returns the status of the current session.<\/li>\n<li>This function can return three different integer values, all of which are available as predefined constants.<\/li>\n<\/ul>\n<h4 id=\"these-are\"><span style=\"color: #800000;\"><strong> These are:<\/strong><\/span><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0Aif(session_status()%20%3D%3D%20PHP_SESSION_NONE)%7B%0A%20%20%20%20%2F%2Fsession%20has%20not%20started%0A%20%20%20%20session_start()%3B%0A%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"top: 286px;\">[ad type=\u201dbanner\u201d]\n<p>As you can see, using this function\u00a0makes your code a little\u00a0more self-explanatory!<\/p>\n<p>Note that you can also check to see if the <strong>$_SESSION<\/strong> array has been set. However, it is worth noting that <a href=\"https:\/\/www.wikitechy.com\/php\/php-login-form-with-sessions\" target=\"_blank\" rel=\"noopener\">$_SESSION<\/a> can be manually initialized like so:<\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%24_SESSION%20%3D%20array(%0A%20%20%20%20\u2019test\u2019%20%3D%3E%20true%0A)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>i.e. The $_SESSION array might exist, even if a session hasn\u2019t already been started.<\/p>\n<h3 id=\"solution-2\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 2<\/label><\/span><\/h3>\n[pastacode lang=\u201dphp\u201d manual=\u201dif%20(version_compare(phpversion()%2C%20\u20195.4.0\u2019%2C%20\u2019%3C\u2019))%20%7B%0A%20%20%20%20%20if(session_id()%20%3D%3D%20\u201d)%20%7B%0A%20%20%20%20%20%20%20%20session_start()%3B%0A%20%20%20%20%20%7D%0A%20%7D%0A%20else%0A%20%7B%0A%20%20%20%20if%20(session_status()%20%3D%3D%20PHP_SESSION_NONE)%20%7B%0A%20%20%20%20%20%20%20%20session_start()%3B%0A%20%20%20%20%7D%0A%20%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"solution-3\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 3<\/label><\/span><\/h3>\n<p>Prior to PHP 5.4 there is no reliable way of knowing other than <strong>setting a global flag<\/strong>.<\/p>\n<p><span style=\"color: #800000;\"><strong>Consider:<\/strong><\/span><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dvar_dump(%24_SESSION)%3B%20%2F%2F%20null%0Asession_start()%3B%0Avar_dump(%24_SESSION)%3B%20%2F%2F%20array%0Asession_destroy()%3B%0Avar_dump(%24_SESSION)%3B%20%2F%2F%20array%2C%20but%20session%20isn\u2019t%20active.%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201dphp\u201d manual=\u201dsession_id()%3B%20%2F%2F%20returns%20empty%20string%0Asession_start()%3B%0Asession_id()%3B%20%2F%2F%20returns%20session%20hash%0Asession_destroy()%3B%0Asession_id()%3B%20%2F%2F%20returns%20empty%20string%2C%20ok%2C%20but%20then%0Asession_id(\u2018foo\u2019)%3B%20%2F%2F%20tell%20php%20the%20session%20id%20to%20use%0Asession_id()%3B%20%2F%2F%20returns%20\u2019foo\u2019%2C%20but%20no%20session%20is%20active.%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"top: 286px;\">[ad type=\u201dbanner\u201d]\n<h3 id=\"solution-4\"><span style=\"color: #008000;\"><label class=\"label label-info\">SOLUTION 4<\/label><\/span><\/h3>\n<p><span style=\"color: #800000;\"><strong>Sample code:<\/strong><\/span><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201dif%20(!isset(%24_SESSION))%20session_start()%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n","protected":false},"excerpt":{"rendered":"<p>SOLUTION 1 As you probably already know, session_start is an in-built PHP function that will start a new session. The problem is: If you call it more than once, your script will throw an E_NOTICE error. Although the solution seems pretty straight forward (don\u2019t call it more than once), in certain scenarios, you won\u2019t be [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[2802,2799,2797,2795,2800,2796,2801,2798,2803],"class_list":["post-1431","post","type-post","status-publish","format-standard","hentry","category-php","tag-php-session-inaccessible-on-other-pages","tag-php-session-behaving-strangely","tag-php-session-storing-behavior-into-variables","tag-php-session-variable-isset-1-after-session_start","tag-php-sessions-from-one-php-page-to-another","tag-php-sessions-that-have-already-been-started","tag-random-session-data-loss-in-php","tag-start-php-session-only-on-specific-pages","tag-why-does-this-php-script-called-by-ajax-randomly-not-load-session-correctly"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1431"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1431\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}