<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":4523,"date":"2022-08-01T06:11:36","date_gmt":"2022-08-01T06:11:36","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=4523"},"modified":"2022-08-01T06:11:36","modified_gmt":"2022-08-01T06:11:36","slug":"what-are-cookies-how-to-create-cookies-in-php","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/","title":{"rendered":"What are cookies ? How to create cookies in PHP ?"},"content":{"rendered":"<ul>\n<li style=\"text-align: justify;\">In PHP cookie is a small file with a maximum size of 4KB that the web server stores on the client computer.<\/li>\n<li style=\"text-align: justify;\">Cookies are typically used to keep track of information such as a username that the site can retrieve to personalize the page when the user visits the website next time.<\/li>\n<li style=\"text-align: justify;\">They can only be read from the domain that it has been issued from.<\/li>\n<li style=\"text-align: justify;\">In browser cookies are usually set in an HTTP header but JavaScript can also set a cookie directly.<\/li>\n<li style=\"text-align: justify;\">In PHP setcookie () function is used to set a cookie.<\/li>\n<li style=\"text-align: justify;\">This function needs to be called prior to any output generated by the script otherwise the cookie will not be set.<\/li>\n<li style=\"text-align: justify;\">In setcookie () function name parameter is used to set the name of the cookie and value parameter is used to set the value of the cookie.<\/li>\n<li style=\"text-align: justify;\">In setcookie () function expire parameter is used to set the expiry timestamp of the cookie after which the cookie can\u2019t be accessed.<\/li>\n<li style=\"text-align: justify;\">In setcookie () function path parameter is used to specify the path on the server for which the cookie will be available.<\/li>\n<li style=\"text-align: justify;\">In setcookie () function domain parameter is used to specify the domain for which the cookie is available.<\/li>\n<li style=\"text-align: justify;\">In setcookie () function security parameter is used to indicate that the cookie should be sent only if a secure HTTPS connection exists.<\/li>\n<\/ul>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-4524 aligncenter\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies.png\" alt=\"\" width=\"1550\" height=\"583\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies.png 1550w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies-300x113.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies-1024x385.png 1024w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies-768x289.png 768w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies-1536x578.png 1536w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies-390x147.png 390w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies-820x308.png 820w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies-1180x444.png 1180w\" sizes=\"(max-width: 1550px) 100vw, 1550px\" \/><\/p>\n<h2 id=\"syntax\">Syntax<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">setcookie (name, value, expire, path, domain, security);<\/code><\/pre> <\/div>\n<h2 id=\"sample-code\">Sample Code<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">&lt;!DOCTYPE html&gt;<br\/>&lt;?php<br\/>setcookie(&quot;test_cookie&quot;, &quot;test&quot;, time() + 3600, &#039;\/&#039;);<br\/>?&gt;<br\/>&lt;html&gt;<br\/>&lt;body&gt;<br\/><br\/>&lt;?php<br\/>if(count($_COOKIE) &gt; 0) {<br\/>    echo &quot;Cookies are enabled.&quot;;<br\/>} else {<br\/>    echo &quot;Cookies are disabled.&quot;;<br\/>}<br\/>?&gt;<br\/><br\/>&lt;\/body&gt;<br\/>&lt;\/html&gt;<\/code><\/pre> <\/div>\n<h2 id=\"output\">Output<\/h2>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-4525\" src=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/cookies.png\" alt=\"\" width=\"1286\" height=\"533\" srcset=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/cookies.png 1286w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/cookies-300x124.png 300w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/cookies-1024x424.png 1024w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/cookies-768x318.png 768w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/cookies-390x162.png 390w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/cookies-820x340.png 820w, https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/cookies-1180x489.png 1180w\" sizes=\"(max-width: 1286px) 100vw, 1286px\" \/><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PHP cookie is a small file with a maximum size of 4KB that the web server stores on the client computer. Cookies are typically used to keep track of information such as a username that the site can retrieve to personalize the page when the user visits the website next time. They can only [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[8182],"tags":[17641,17649,17640,17643,17644,17648,17645,17639,17642,17647,17646],"class_list":["post-4523","post","type-post","status-publish","format-standard","hentry","category-php","tag-access-and-delete-cookies-in-php","tag-get-cookie-in-php","tag-how-to-create","tag-how-to-create-cookies-in-php","tag-how-to-delete-cookies-in-php","tag-how-to-modify-cookies-in-php","tag-how-to-use-cookies-in-php","tag-php-cookies","tag-php-cookies-with-example","tag-sessin-and-cookies-in-php-with-example","tag-types-of-cookies-in-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What are cookies ? How to create cookies in PHP ?<\/title>\n<meta name=\"description\" content=\"What are cookies ? How to create cookies in PHP ? - In PHP cookie is a small file with a maximum size of 4KB that the web server stores on the client computer.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What are cookies ? How to create cookies in PHP ?\" \/>\n<meta property=\"og:description\" content=\"What are cookies ? How to create cookies in PHP ? - In PHP cookie is a small file with a maximum size of 4KB that the web server stores on the client computer.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-01T06:11:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies.png\" \/>\n<meta name=\"author\" content=\"webmaster\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"webmaster\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/\",\"name\":\"What are cookies ? How to create cookies in PHP ?\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies.png\",\"datePublished\":\"2022-08-01T06:11:36+00:00\",\"dateModified\":\"2022-08-01T06:11:36+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4\"},\"description\":\"What are cookies ? How to create cookies in PHP ? - In PHP cookie is a small file with a maximum size of 4KB that the web server stores on the client computer.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/#primaryimage\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies.png\",\"contentUrl\":\"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies.png\",\"width\":1550,\"height\":583},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4\",\"name\":\"webmaster\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g\",\"caption\":\"webmaster\"},\"sameAs\":[\"https:\/\/www.wikitechy.com\/interview-questions\"],\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/webmaster\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What are cookies ? How to create cookies in PHP ?","description":"What are cookies ? How to create cookies in PHP ? - In PHP cookie is a small file with a maximum size of 4KB that the web server stores on the client computer.","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:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/","og_locale":"en_US","og_type":"article","og_title":"What are cookies ? How to create cookies in PHP ?","og_description":"What are cookies ? How to create cookies in PHP ? - In PHP cookie is a small file with a maximum size of 4KB that the web server stores on the client computer.","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/","og_site_name":"Wikitechy","article_published_time":"2022-08-01T06:11:36+00:00","og_image":[{"url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies.png"}],"author":"webmaster","twitter_card":"summary_large_image","twitter_misc":{"Written by":"webmaster","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/","name":"What are cookies ? How to create cookies in PHP ?","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/#primaryimage"},"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies.png","datePublished":"2022-08-01T06:11:36+00:00","dateModified":"2022-08-01T06:11:36+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4"},"description":"What are cookies ? How to create cookies in PHP ? - In PHP cookie is a small file with a maximum size of 4KB that the web server stores on the client computer.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/php\/what-are-cookies-how-to-create-cookies-in-php\/#primaryimage","url":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies.png","contentUrl":"https:\/\/www.wikitechy.com\/interview-questions\/wp-content\/uploads\/2022\/08\/what-is-cookies.png","width":1550,"height":583},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/f785ba3ecc599133e65ab6138042a3e4","name":"webmaster","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/155b77fd8cdda3d0913fcb7e7ee63543b0c345d2d8f6dcebda5b0583ab61f967?s=96&d=mm&r=g","caption":"webmaster"},"sameAs":["https:\/\/www.wikitechy.com\/interview-questions"],"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/webmaster\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=4523"}],"version-history":[{"count":1,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4523\/revisions"}],"predecessor-version":[{"id":4526,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/4523\/revisions\/4526"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=4523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=4523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=4523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}