{"id":85,"date":"2020-12-12T14:36:58","date_gmt":"2020-12-12T14:36:58","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/php\/?p=85"},"modified":"2026-10-29T01:28:50","modified_gmt":"2026-10-29T01:28:50","slug":"php-working-with-cookies","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/php\/php-working-with-cookies\/","title":{"rendered":"PHP &#8211; Working With Cookies"},"content":{"rendered":"<p>In this chapter, we would learn how to work with cookies in PHP. At the end, you will be able to set cookies, retrieve the cookies and delete them. The following is covered here:<\/p>\n<ol>\n<li><a href=\"#t1\">Overview of Cookies<\/a><\/li>\n<li><a href=\"#t2\">Setting Cookie Values<\/a><\/li>\n<li><a href=\"#t3\">Retrieving Cookie Values<\/a><\/li>\n<li><a href=\"#t4\">Deleting Cookies<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Overview of PHP Cookies<\/strong><\/h4>\n<p>Cookies are bits of information placed on your computers by websites you visit. It is stored in form of text files and used for tracking and other things. PHP support cookies based on the following steps:<\/p>\n<ul>\n<li>a server script(PHP) sends some data in form of cookies to your browser. It may be name, age, time of login, some id etc<\/li>\n<li>the browser stores the information in the disk of the client machine<\/li>\n<li>next time the browser makes a request to the server, it attaches those information. So when the server gets it, it could now identify the source<\/li>\n<li>information in cookie is accessed using $_COOKIE or $_HTTP_COOKIE_VAR[] variables<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Setting Cooking Values<\/strong><\/h4>\n<p>You can set cookie values using the <strong>setcookie()<\/strong> function in PHP. This function takes up to six parameters. Moreover, it must be called before any output of your web page including the &lt;html&gt; and &lt;head&gt; tags and whitespace. The following are the parameters to the<strong> setcookie()<\/strong> function:<\/p>\n<p><strong>name<\/strong> &#8211; specifies the name of the cookie<\/p>\n<p><strong>value<\/strong> &#8211; the value of the cookie. It is recommended not to store any sensitive information. If the name the cookie is ssn, the the value can be retrieved using $COOKIE[&#8220;ssn&#8221;].<\/p>\n<p><strong>expires<\/strong> &#8211; this set the time the cookie expires.<\/p>\n<p><strong>path<\/strong> &#8211; this is the path on the server where the cookie will be stored. If this path is set to slash, then the cookie will be available to the whole domain.<\/p>\n<p><strong>domain<\/strong> &#8211; this is the domain the cookie is available to. For example <a href=\"https:\/\/www.kindsonthegenius.com\"><em>www.kindsonthegenius.com<\/em><\/a><\/p>\n<p><strong>secure<\/strong> &#8211; this is a boolean value that specifies if the cookie should be transmitted over a secure HTTPS connection<\/p>\n<p>The code below stores the value of name and score as cookies<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\r\n    <span style=\"color: #888888;\">\/\/Setting cookie values<\/span>\r\n    setcookie(<span style=\"background-color: #fff0f0;\">\"name\"<\/span>, <span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span>, <span style=\"color: #007020;\">time<\/span>()<span style=\"color: #333333;\">+<\/span><span style=\"color: #0000dd; font-weight: bold;\">6000<\/span>, <span style=\"background-color: #fff0f0;\">\"\/\"<\/span>,<span style=\"background-color: #fff0f0;\">\"\"<\/span>,<span style=\"color: #008800; font-weight: bold;\">TRUE<\/span>,<span style=\"color: #008800; font-weight: bold;\">FALSE<\/span>);\r\n    setcookie(<span style=\"background-color: #fff0f0;\">\"score\"<\/span>, <span style=\"background-color: #fff0f0;\">\"99.9\"<\/span>, <span style=\"color: #007020;\">time<\/span>()<span style=\"color: #333333;\">+<\/span><span style=\"color: #0000dd; font-weight: bold;\">3400<\/span>, <span style=\"background-color: #fff0f0;\">\"\/\"<\/span>,<span style=\"background-color: #fff0f0;\">\"\"<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">0<\/span>,<span style=\"color: #0000dd; font-weight: bold;\">1<\/span>);\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;\">\"Cookie values of name and score is set\"<\/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<h4><strong id=\"t3\">3. Retrieving Cookie Values<\/strong><\/h4>\n<p>PHP provides a number of ways to access cookie values. You can use either the $_COOKIE or the $HTTP_COOKIE_VARS variable. The example below illustrates this<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><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: #888888;\">\/\/the name cookie<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"background-color: #fff0f0;\">\"Cookie value of name \"<\/span><span style=\"color: #333333;\">.<\/span> <span style=\"color: #996633;\">$_COOKIE<\/span>[<span style=\"background-color: #fff0f0;\">\"name\"<\/span>];\r\n        <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"color: #996633;\">$HTTP_COOKIE_VARS<\/span>[<span style=\"background-color: #fff0f0;\">\"name\"<\/span>]; <span style=\"color: #888888;\">\/\/same as above<\/span>\r\n\r\n        <span style=\"color: #888888;\">\/\/ the score cookie<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">echo<\/span> <span style=\"background-color: #fff0f0;\">\"Score is \"<\/span><span style=\"color: #333333;\">.<\/span> <span style=\"color: #996633;\">$_COOKIE<\/span>[<span style=\"background-color: #fff0f0;\">\"score\"<\/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>The code would output the value of the name and score cookies.<\/p>\n<p>The<strong> isset()<\/strong> function is used to check is a cookie is set or not. It returns true is the cookie is set, otherwise it returns false.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Deleting Cookies<\/strong><\/h4>\n<p>According to the official documentation, you can delete a cookie using the <strong>setcookie()<\/strong> function and providing the name argument only. However, some experts say theres is a better way. And that is to force the cookie to expire by setting it also using an already expired date.<\/p>\n<p>An example is given below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">&lt;?php<\/span>\r\n    <span style=\"color: #888888;\">\/\/Setting cookie values to expired date<\/span>\r\n    setcookie(<span style=\"background-color: #fff0f0;\">\"name\"<\/span>, <span style=\"background-color: #fff0f0;\">\"\"<\/span>, <span style=\"color: #007020;\">time<\/span>()<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">60<\/span>, <span style=\"background-color: #fff0f0;\">\"\/\"<\/span>,<span style=\"background-color: #fff0f0;\">\"\"<\/span>,<span style=\"color: #008800; font-weight: bold;\">FALSE<\/span>);\r\n    setcookie(<span style=\"background-color: #fff0f0;\">\"score\"<\/span>, <span style=\"background-color: #fff0f0;\">\"\"<\/span>, <span style=\"color: #007020;\">time<\/span>()<span style=\"color: #333333;\">-<\/span><span style=\"color: #0000dd; font-weight: bold;\">60<\/span>, <span style=\"background-color: #fff0f0;\">\"\/\"<\/span>,<span style=\"background-color: #fff0f0;\">\"\"<\/span>,<span style=\"color: #008800; font-weight: bold;\">FALSE<\/span>);\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;\">\"Deleting cookies\"<\/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","protected":false},"excerpt":{"rendered":"<p>In this chapter, we would learn how to work with cookies in PHP. At the end, you will be able to set cookies, retrieve the &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":[32],"class_list":["post-85","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-php-cookies"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP - Working With Cookies | Kindson The Genius<\/title>\n<meta name=\"description\" content=\"PHP - Working With Cookies \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\" \/>\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-working-with-cookies\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP - Working With Cookies | Kindson The Genius\" \/>\n<meta property=\"og:description\" content=\"PHP - Working With Cookies \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/php\/php-working-with-cookies\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-12T14:36:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-29T01:28:50+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-working-with-cookies\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-cookies\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"headline\":\"PHP &#8211; Working With Cookies\",\"datePublished\":\"2020-12-12T14:36:58+00:00\",\"dateModified\":\"2026-10-29T01:28:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-cookies\\\/\"},\"wordCount\":475,\"keywords\":[\"PHP Cookies\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-cookies\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-cookies\\\/\",\"name\":\"PHP - Working With Cookies | Kindson The Genius\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#website\"},\"datePublished\":\"2020-12-12T14:36:58+00:00\",\"dateModified\":\"2026-10-29T01:28:50+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/#\\\/schema\\\/person\\\/a2dcea4c76668e90e5842583fd5a5f60\"},\"description\":\"PHP - Working With Cookies \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-cookies\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-cookies\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/php-working-with-cookies\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/php\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP &#8211; Working With Cookies\"}]},{\"@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 - Working With Cookies | Kindson The Genius","description":"PHP - Working With Cookies \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","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-working-with-cookies\/","og_locale":"en_US","og_type":"article","og_title":"PHP - Working With Cookies | Kindson The Genius","og_description":"PHP - Working With Cookies \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","og_url":"https:\/\/kindsonthegenius.com\/php\/php-working-with-cookies\/","og_site_name":"PHP Tutorial","article_published_time":"2020-12-12T14:36:58+00:00","article_modified_time":"2026-10-29T01:28:50+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-working-with-cookies\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-working-with-cookies\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"headline":"PHP &#8211; Working With Cookies","datePublished":"2020-12-12T14:36:58+00:00","dateModified":"2026-10-29T01:28:50+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-working-with-cookies\/"},"wordCount":475,"keywords":["PHP Cookies"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/php\/php-working-with-cookies\/","url":"https:\/\/kindsonthegenius.com\/php\/php-working-with-cookies\/","name":"PHP - Working With Cookies | Kindson The Genius","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/php\/#website"},"datePublished":"2020-12-12T14:36:58+00:00","dateModified":"2026-10-29T01:28:50+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/php\/#\/schema\/person\/a2dcea4c76668e90e5842583fd5a5f60"},"description":"PHP - Working With Cookies \u2014 free PHP tutorial by Kindson Munonye with clear examples. Step-by-step for beginners. Part of Kindson The Genius.","breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/php\/php-working-with-cookies\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/php\/php-working-with-cookies\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/php\/php-working-with-cookies\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/php\/"},{"@type":"ListItem","position":2,"name":"PHP &#8211; Working With Cookies"}]},{"@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\/85","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=85"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":203,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/posts\/85\/revisions\/203"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/php\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}