{"id":211348,"date":"2025-07-04T10:10:10","date_gmt":"2025-07-04T10:10:10","guid":{"rendered":"https:\/\/www.softwaretestinghelp.com\/?page_id=211348"},"modified":"2025-07-08T14:00:31","modified_gmt":"2025-07-08T14:00:31","slug":"php-cookies","status":"publish","type":"page","link":"https:\/\/www.softwaretestinghelp.com\/php\/php-cookies\/","title":{"rendered":"PHP Cookies: How to Create, Modify &amp; Delete Cookies"},"content":{"rendered":"\n<p><strong>Manage web sessions effectively with PHP&#8217;s powerful cookie handling capabilities. Explore these programming examples to understand how to Create, modify<\/strong> <strong>or delete PHP Cookies:<\/strong><\/p>\n\n\n\n<p>In this tutorial, you will learn what a cookie is, how to create, modify &amp; delete PHP cookies, and how to verify if cookies are enabled. Also, some frequently asked questions (FAQs) related to this topic are covered here.<\/p>\n\n\n\n<p>Please note that we have used PHP version 7 in all examples.<\/p>\n\n\n\n<p>Let\u2019s begin!<\/p>\n\n\n\n<p><strong>=&gt; <a href=\"https:\/\/www.softwaretestinghelp.com\/php\/\">Complete PHP Guide for ALL<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PHP Cookies: A Practical Guide with Examples<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/02\/PHP-Cookies.png\"><img decoding=\"async\" width=\"700\" height=\"394\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/02\/PHP-Cookies.png\" alt=\"PHP Cookies\" class=\"wp-image-211602\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/02\/PHP-Cookies.png 700w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/02\/PHP-Cookies-300x169.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Cookie<\/h2>\n\n\n\n<p>A cookie is a small file that the server embeds on the client or the computer to identify a user.<\/p>\n\n\n\n<p><strong>A cookie can store information like<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>IP addresses<\/li>\n\n\n\n<li>Email addresses<\/li>\n\n\n\n<li>Usernames<\/li>\n\n\n\n<li>Passwords<\/li>\n\n\n\n<li>Previous purchase details<\/li>\n\n\n\n<li>Items viewed<\/li>\n\n\n\n<li>Preferred settings like themes, etc.<\/li>\n<\/ul>\n\n\n\n<p>Cookies are not used to identify you personally, but some information that cookies collect may be linked. A cookie can only hold a maximum of 4KB of data.<\/p>\n\n\n\n<p><strong>Furthermore, cookies are very useful for developers as well as computer users. Some of the advantages of cookies include:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Light-weight and occupies less memory<\/li>\n\n\n\n<li>Easily configurable<\/li>\n\n\n\n<li>Make browsing easier<\/li>\n\n\n\n<li>Users can delete cookies<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a PHP Cookie<\/h2>\n\n\n\n<p>The PHP <strong>setcookie()<\/strong> function is used to create\/set a PHP cookie. It has seven parameters that are listed below. Only the <strong>name<\/strong> parameter is mandatory.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>name<\/strong> &#8211; The name of the cookie<\/li>\n\n\n\n<li><strong>value<\/strong> &#8211; The value of the cookie<\/li>\n\n\n\n<li><strong>expires_or_options<\/strong> &#8211; the time the cookie expires<\/li>\n\n\n\n<li><strong>path<\/strong> &#8211; The path (directory) which the cookie will be available on<\/li>\n\n\n\n<li><strong>domain<\/strong> &#8211; the domain in which the cookie is available to<\/li>\n\n\n\n<li><strong>secure<\/strong> &#8211; indicates that the cookie should be sent over a secure HTTPS connection from the client<\/li>\n\n\n\n<li><strong>httponly<\/strong> &#8211; if true, the cookie is accessible only through the HTTP protocol<\/li>\n<\/ol>\n\n\n\n<p>Furthermore, <strong>setcookie() <\/strong>must appear before <strong>&lt;html&gt;<\/strong>.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nsetcookie(\n    string $name,\n    string $value = \"\",\n    int $expires_or_options = 0,\n    string $path = \"\",\n    string $domain = \"\",\n    bool $secure = false,\n    bool $httponly = false\n): bool\n<\/pre><\/div>\n\n\n<p>The <strong>isset()<\/strong> function is used to check whether or not the cookie is set.<\/p>\n\n\n\n<p>The superglobal, <strong>$_COOKIE<\/strong> is used to retrieve the cookie details like name, value, etc.<\/p>\n\n\n\n<p><strong>Let\u2019s see an example. You can practice this example by running the following programming code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n \n$cookie_name = &quot;Student&quot;;\n$cookie_value = &quot;David&quot;;\nsetcookie($cookie_name, $cookie_value, time() + (86400 * 7), &quot;\/&quot;); \n \n?&gt;\n&lt;html&gt;\n&lt;body&gt;\n \n&lt;?php\n \nif(isset($_COOKIE&#x5B;$cookie_name])) {\n    echo &quot;Cookie is set! Cookie name is $cookie_name and value is $cookie_value.&quot;;\n} else {\n    echo &quot;Cookie is not set!&quot;;\n}\n \n?&gt;\n \n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n<p><strong>Note 1<\/strong>: In the above cookie example, we have used three parameters:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>name<\/li>\n\n\n\n<li>value<\/li>\n\n\n\n<li>expiration date &#8211; <strong>86400<\/strong> which indicates that the cookie will expire after 1 day. Similarly, you can use <strong>86400 * 7<\/strong> for 7 days, <strong>3600<\/strong> for one hour, <strong>60<\/strong> for one minute, etc.<\/li>\n<\/ol>\n\n\n\n<p>The forward slash character (<strong>\/<\/strong>) is used to indicate that the cookie is available on the whole website. If not, you have to specify the relevant path.<\/p>\n\n\n\n<p><strong>Note 2:<\/strong> After running the above programming code, refresh the page.<\/p>\n\n\n\n<p><strong>The below text shows the browser output of the above programming code (after refreshing the page).<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Cookie is set! The cookie name is Student and the value is David.<\/pre>\n\n\n\n<p><strong>Modifying a Cookie<\/strong><\/p>\n\n\n\n<p>Modifying a cookie is straightforward. You just need to set the cookie again to modify a cookie.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Deleting a Cookie<\/h2>\n\n\n\n<p>A cookie can be deleted using the <strong>setcookie()<\/strong> function with a past expiration date.<\/p>\n\n\n\n<p><strong>Let\u2019s see an example. You can practice this example by running the following programming code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n \n\/\/delete cookie\nsetcookie(&quot;student&quot;, time() - 60); \n \n?&gt;\n&lt;html&gt;\n&lt;body&gt;\n \n&lt;?php\n \necho &quot;Cookie is deleted!&quot;;\n \n?&gt;\n \n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">How to Verify If Cookies are Enabled<\/h2>\n\n\n\n<p>The <strong>count()<\/strong> function and the <strong>$_COOKIE<\/strong> superglobal are used to verify whether cookies are enabled or not.<\/p>\n\n\n\n<p><strong>Let\u2019s see an example. You can practice this example by running the following programming code:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php \n \nsetcookie(&quot;test_cookie&quot;,&quot;test&quot;);\n \n?&gt;\n&lt;html&gt; \n&lt;body&gt;\n \n&lt;?php \n \nif(count($_COOKIE)&gt;0){\n    echo &quot;Cookies are enabled!&quot;;\n}else{\n    echo &quot;Cookies are disabled!&quot;;\n}\n \n?&gt;\n \n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre><\/div>\n\n\n<p><strong>Note<\/strong>: After running the above programming code, refresh the page.<\/p>\n\n\n\n<p><strong>The below text shows the browser output of the above programming code (after refreshing the page):<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Cookies are enabled!<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1751380064297\" class=\"rank-math-list-item\">\n<p class=\"rank-math-question \"><strong>1. What is a PHP cookie?<\/strong><\/p>\n<div class=\"rank-math-answer \">\n\n<p>A PHP cookie is a small file that the server embeds on the client or the computer. It is used to identify users.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1751380089150\" class=\"rank-math-list-item\">\n<p class=\"rank-math-question \"><strong>2. Where do cookies reside?<\/strong><\/p>\n<div class=\"rank-math-answer \">\n\n<p>Cookies reside in your browser folder or subfolder.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1751380107316\" class=\"rank-math-list-item\">\n<p class=\"rank-math-question \"><strong>3. Can cookies identify you personally?<\/strong><\/p>\n<div class=\"rank-math-answer \">\n\n<p>Cookies do not identify you personally, but some information that cookies collect may be linked.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1751380121739\" class=\"rank-math-list-item\">\n<p class=\"rank-math-question \"><strong>4. Do cookies collect IP addresses?<\/strong><\/p>\n<div class=\"rank-math-answer \">\n\n<p>Yes, cookies can be used to collect IP addresses.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1751380137354\" class=\"rank-math-list-item\">\n<p class=\"rank-math-question \"><strong>5. How much data can a cookie hold?<\/strong><\/p>\n<div class=\"rank-math-answer \">\n\n<p> A cookie can only hold a maximum data capacity of 4KB.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1751380153681\" class=\"rank-math-list-item\">\n<p class=\"rank-math-question \"><strong>6. How do you set cookies?<\/strong><\/p>\n<div class=\"rank-math-answer \">\n\n<p>You can set a cookie using the PHP setcookie() function as shown below.<br \/><strong>Example:<\/strong><br \/>&lt;?php<br \/>setcookie(&#8220;user&#8221;, &#8220;\/&#8221;);<br \/>?&gt;<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1751380263392\" class=\"rank-math-list-item\">\n<p class=\"rank-math-question \"><strong>7. What information is stored in PHP cookies?<\/strong><\/p>\n<div class=\"rank-math-answer \">\n\n<p>Cookies can store information like email addresses, usernames, passwords, previous purchase details, items viewed, preferred settings like theme, etc.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>A PHP cookie is a small file that the server embeds on the client or the computer to identify a user. <\/p>\n\n\n\n<p>It can store information like IP addresses, email addresses, usernames, passwords, previous purchase details, items viewed, preferred settings like theme, etc. You can create, modify and delete PHP cookies.<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/www.softwaretestinghelp.com\/php\/php-file-handling\/\">PREV Tutorial<\/a> | <a href=\"https:\/\/www.softwaretestinghelp.com\/php\/php-sessions\/\">NEXT Tutorial<\/a><\/strong><\/p>\n\r\n\t\t\t<div id=\"daexthefup-container\"\r\n\t\t\t\tclass=\"daexthefup-container daexthefup-layout-stacked daexthefup-alignment-center\"\r\n\t\t\t\tdata-post-id=\"211348\">\r\n\r\n\t\t\t\t<div class=\"daexthefup-feedback\">\r\n\t\t\t\t\t<div class=\"daexthefup-text\">\r\n\t\t\t\t\t\t<h3 class=\"daexthefup-title\">Was this helpful?<\/h3>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<div class=\"daexthefup-buttons-container\">\r\n\t\t\t\t\t\t<div class=\"daexthefup-buttons\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-yes daexthefup-button daexthefup-button-type-icon\" data-value=\"1\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-up-cls-1{fill:#c9c9c9;}.thumb-up-cls-2{fill:#e1e1e1;}.thumb-up-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_up\">\r\n                        <path class=\"thumb-up-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-up-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"20\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,9.06l-.49-.1c-.81-.17-1.61.35-1.78,1.16l-5.3,11.74c-.17.81,3.16,1.61,3.97,1.78l1.96.41c.81.17,1.61-.35,1.78-1.16l2.18-10.27c.34-1.61-.7-3.21-2.31-3.56Z\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,20h-18.67c-.83,0-1.5.67-1.5,1.5v12c0,.83.67,1.5,1.5,1.5h16.27c.71,0,1.33-.5,1.47-1.21l2.4-12c.19-.93-.53-1.8-1.47-1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-no daexthefup-button daexthefup-button-type-icon\" data-value=\"0\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-down-cls-1{fill:#c9c9c9;}.thumb-down-cls-2{fill:#e1e1e1;}.thumb-down-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_down\">\r\n                        <path class=\"thumb-down-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-down-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"13\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,38.94l-.49.1c-.81.17-1.61-.35-1.78-1.16l-5.3-11.74c-.17-.81,3.16-1.61,3.97-1.78l1.96-.41c.81-.17,1.61.35,1.78,1.16l2.18,10.27c.34,1.61-.7,3.21-2.31,3.56Z\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,28h-18.67c-.83,0-1.5-.67-1.5-1.5v-12c0-.83.67-1.5,1.5-1.5h16.27c.71,0,1.33.5,1.47,1.21l2.4,12c.19.93-.53,1.8-1.47,1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-comment\">\r\n\t\t\t\t\t<div class=\"daexthefup-comment-top-container\">\r\n\t\t\t\t\t\t<label id=\"daexthefup-comment-label\" class=\"daexthefup-comment-label\"><\/label>\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-container\">\r\n\t\t\t\t\t\t\t\t<div id=\"daexthefup-comment-character-counter-number\"\r\n\t\t\t\t\t\t\t\t\tclass=\"daexthefup-comment-character-counter-number\"><\/div>\r\n\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-text\"><\/div>\r\n\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<textarea id=\"daexthefup-comment-textarea\" class=\"daexthefup-comment-textarea\"\r\n\t\t\t\t\t\t\t\tplaceholder=\"Type your message\"\r\n\t\t\t\t\t\t\t\tmaxlength=\"\r\n\t\t\t\t\t\t\t\t400\t\t\t\t\t\t\t\t\t\"><\/textarea>\r\n\t\t\t\t\t<div class=\"daexthefup-comment-buttons-container\">\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-submit daexthefup-button\">Submit<\/button>\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-cancel daexthefup-button\">Cancel<\/button>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-successful-submission-text\">Thanks for your feedback!<\/div>\r\n\r\n\t\t\t<\/div>\r\n\r\n\t\t\t","protected":false},"excerpt":{"rendered":"<p>Manage web sessions effectively with PHP&#8217;s powerful cookie handling capabilities. Explore these programming examples to understand how to Create, modify or delete PHP Cookies: In this tutorial, you will learn what a cookie is, how to create, modify &amp; delete PHP cookies, and how to verify if cookies are enabled. &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"PHP Cookies: How to Create, Modify &amp; Delete Cookies\" class=\"read-more button\" href=\"https:\/\/www.softwaretestinghelp.com\/php\/php-cookies\/#more-211348\" aria-label=\"Read more about PHP Cookies: How to Create, Modify &amp; Delete Cookies\">Read more<\/a><\/p>\n","protected":false},"author":9,"featured_media":211602,"parent":392052,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_helpful_pro_status":1,"footnotes":""},"categories":[],"tags":[],"class_list":["post-211348","page","type-page","status-publish","has-post-thumbnail"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/211348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/comments?post=211348"}],"version-history":[{"count":0,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/211348\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/392052"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media\/211602"}],"wp:attachment":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media?parent=211348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/categories?post=211348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/tags?post=211348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}