{"id":204,"date":"2020-12-12T21:21:18","date_gmt":"2020-12-12T21:21:18","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/php\/?p=204"},"modified":"2026-10-29T01:28:44","modified_gmt":"2026-10-29T01:28:44","slug":"php-sessions","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/php\/php-sessions\/","title":{"rendered":"PHP &#8211; Sessions"},"content":{"rendered":"<p>In this chapter, we would be learning about PHP Sessions. This is a way to to make some data available across multiple pages. Similar to cookies, sessions use <em>session variables<\/em> to store data.<\/p>\n<ol>\n<li><a href=\"#t1\">Overview of Session<\/a><\/li>\n<li><a href=\"#t2\">Starting a Session<\/a><\/li>\n<li><a href=\"#t3\">Session Without Cookies<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"1\">1. Overview of PHP Sessions<\/strong><\/h4>\n<p>A file is created in the temporary direction by a session. This file is used to store registered session variables and their values. So during a visit, this stored data would be available accross the pages during that session(or visit). The location of the temporary directory\u00a0 is set in the<strong> php.ini<\/strong> file by the <strong>session.save_path<\/strong>. This variable must be set before you can use session variables.<\/p>\n<p>Three thing when a session is being setup:<\/p>\n<ul>\n<li>a unique identifier is created for that session. This is simply a random hexadecimal number of 32 characters<\/li>\n<li>PHP sends a cookie called <strong>PHPSESSID<\/strong>\u00a0 to the the user&#8217;s computer to store the session id<\/li>\n<li>a file is also created on the server in the temporary directory with the name of the session id prefixed by &#8220;sess_&#8221;<\/li>\n<\/ul>\n<p>When a PHP script wants to get the value of a session variable, it retrieves the session identifier from the <strong>PHPSESSID<\/strong> cookie. Next it looks at the temporary directory and obtains the corresponding file. The it compares the value in the file with the value in the <strong>PHPSESSID<\/strong> cookie for validation.<\/p>\n<p>A session is terminated when the user closes the browser or leaves the site. Sometimes a session is terminated after a period of inactivity (30 minutes for example)<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Starting a Session<\/strong><\/h4>\n<p>You can start a session in PHP by calling the <strong>session_start()<\/strong> inbuilt function. It first checks if there&#8217;s an already started session, and if not, it starts one. It is better to put this function call at the beginning of the page.<\/p>\n<p>Let&#8217;s consider a program that starts a session, sets a session variable and increments the session variable each time a page is visited. The <strong>isset()<\/strong> function\u00a0 is used to check if a session variable exists.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\r\n    <span style=\"color: #888888;\">\/\/Start the session<\/span>\r\n    <span style=\"color: #007020;\">session_start<\/span>();\r\n    <span style=\"color: #888888;\">\/\/check is the session variable is set<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span>(<span style=\"color: #007020;\">isset<\/span>(<span style=\"color: #996633;\">$_SESSION<\/span>[<span style=\"background-color: #fff0f0;\">\"visits\"<\/span>])) {\r\n        <span style=\"color: #996633;\">$_SESSION<\/span>[<span style=\"background-color: #fff0f0;\">\"visits\"<\/span>] <span style=\"color: #333333;\">+=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>;\r\n    }<span style=\"color: #008800; font-weight: bold;\">else<\/span> {\r\n        <span style=\"color: #996633;\">$_SESSION<\/span>[<span style=\"background-color: #fff0f0;\">\"visits\"<\/span>] <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>;\r\n    }\r\n    \r\n<span style=\"color: #557799;\">?&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;html&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;body&gt;<\/span>\r\n    <span style=\"color: #557799;\">&lt;?php<\/span> \r\n        <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"background-color: #fff0f0;\">\"You have visited \"<\/span><span style=\"color: #333333;\">.<\/span> <span style=\"color: #996633;\">$_SESSION<\/span>[<span style=\"background-color: #fff0f0;\">\"visits\"<\/span>]<span style=\"color: #333333;\">.<\/span> <span style=\"background-color: #fff0f0;\">\"times\"<\/span> ;\r\n        <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"background-color: #fff0f0;\">\"&lt;br&gt; Session Id is \"<\/span><span style=\"color: #333333;\">.<\/span> <span style=\"color: #996633;\">$_COOKIE<\/span>[<span style=\"background-color: #fff0f0;\">\"PHPSESSID\"<\/span>];\r\n    <span style=\"color: #557799;\">?&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/body&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/html&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Destroying a Session<\/strong><\/p>\n<p>To destroy a PHP session, you use the<em><strong> session_destroy()<\/strong> <\/em>function. This function does not take any arguments and once called, can be used to destroy all session variables. However, to destroy just a single session variable, you can use the <strong><em>unset()<\/em> <\/strong>function passing the session variable as parameter<\/p>\n<p><strong>About Auto Session<\/strong><\/p>\n<p>When auto session is set, then you don&#8217;t need to explicitly call the session_start() function to start.\u00a0 This is done in the <strong>php.ini<\/strong> file by setting the\u00a0 <strong>session.auto_start<\/strong> variable to 1.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Sessions Without Cookies<\/strong><\/h4>\n<p>There are sometimes when when the client computer does not allow cookies. In situation like this, you can still send session ID to the browser anyways, using a different approach.<\/p>\n<p>To achieve this, you need to define a constant SID once the session starts. It takes the form of session_name = session_id. But if theres is not session_id existing in the client, then it would be an empty string.<\/p>\n<p>The code below shows how to pass the session id from one page to another called page2.php<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;a<\/span>  <span style=\"color: #0000cc;\">href =<\/span> <span style=\"background-color: #fff0f0;\">\"page2.php?<\/span><span style=\"color: #557799;\">&lt;?php<\/span> <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"color: #007020;\">htmlspecialchars<\/span>(SID); <span style=\"color: #557799;\">?&gt;<\/span><span style=\"background-color: #fff0f0;\">\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this chapter, we would be learning about PHP Sessions. This is a way to to make some data available across multiple pages. Similar to &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[33],"class_list":["post-204","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-session-in-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP - Sessions | Kindson The Genius<\/title>\n<meta name=\"description\" content=\"PHP - Sessions \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius. Free online.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/php\/php-sessions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP - Sessions | Kindson The Genius\" \/>\n<meta property=\"og:description\" content=\"PHP - Sessions \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius. Free online.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/php\/php-sessions\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-12T21:21:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-29T01:28:44+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-sessions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-sessions\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"headline\":\"PHP &#8211; Sessions\",\"datePublished\":\"2020-12-12T21:21:18+00:00\",\"dateModified\":\"2026-10-29T01:28:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-sessions\\\/\"},\"wordCount\":531,\"keywords\":[\"Session in PHP\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-sessions\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-sessions\\\/\",\"name\":\"PHP - Sessions | Kindson The Genius\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#website\"},\"datePublished\":\"2020-12-12T21:21:18+00:00\",\"dateModified\":\"2026-10-29T01:28:44+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"description\":\"PHP - Sessions \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius. Free online.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-sessions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-sessions\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-sessions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP &#8211; Sessions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/\",\"name\":\"PHP Tutorial\",\"description\":\"Beginner to Expert With Practicals\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP - Sessions | Kindson The Genius","description":"PHP - Sessions \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius. Free online.","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:\/\/kindsonthegenius.com\/php\/php-sessions\/","og_locale":"en_US","og_type":"article","og_title":"PHP - Sessions | Kindson The Genius","og_description":"PHP - Sessions \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius. Free online.","og_url":"https:\/\/kindsonthegenius.com\/php\/php-sessions\/","og_site_name":"PHP Tutorial","article_published_time":"2020-12-12T21:21:18+00:00","article_modified_time":"2026-10-29T01:28:44+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/php\/php-sessions\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-sessions\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"headline":"PHP &#8211; Sessions","datePublished":"2020-12-12T21:21:18+00:00","dateModified":"2026-10-29T01:28:44+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-sessions\/"},"wordCount":531,"keywords":["Session in PHP"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/php\/php-sessions\/","url":"https:\/\/kindsonthegenius.com\/php\/php-sessions\/","name":"PHP - Sessions | Kindson The Genius","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/#website"},"datePublished":"2020-12-12T21:21:18+00:00","dateModified":"2026-10-29T01:28:44+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"description":"PHP - Sessions \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius. Free online.","breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-sessions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/php\/php-sessions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/php\/php-sessions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/php\/"},{"@type":"ListItem","position":2,"name":"PHP &#8211; Sessions"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/php\/#website","url":"https:\/\/kindsonthegenius.com\/php\/","name":"PHP Tutorial","description":"Beginner to Expert With Practicals","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/kindsonthegenius.com\/php\/author\/kindsonthegenius-3\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/comments?post=204"}],"version-history":[{"count":3,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":207,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/204\/revisions\/207"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/media?parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/categories?post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/tags?post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}