{"id":361,"date":"2018-01-14T15:42:45","date_gmt":"2018-01-14T15:42:45","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=361"},"modified":"2018-01-14T15:42:45","modified_gmt":"2018-01-14T15:42:45","slug":"php-cookies","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/php-cookies\/","title":{"rendered":"PHP Cookies"},"content":{"rendered":"<p><strong>What is a Cookie?<\/strong><\/p>\n<p>The cookies are often used to identify users. A cookie contains a small file that server embeds on the user&#8217;s computer when he\/she access the website through browsers. By using php cookie code you create and retrieve its value.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n$cookie_name = &quot;user&quot;;<br \/>\n$cookie_value = &quot;John Doe&quot;;<br \/>\nsetcookie($cookie_name, $cookie_value, time() + (86400 * 30), &quot;\/&quot;); \/\/ 86400 = 1 day<br \/>\n?&gt;[\/php]<\/p>\n<p>[php]&lt;?php<br \/>\nif(!isset($_COOKIE[$cookie_name])) {<br \/>\n     echo &quot;Cookie named &#8216;&quot; . $cookie_name . &quot;&#8217; is not set!&quot;;<br \/>\n} else {<br \/>\n     echo &quot;Cookie &#8216;&quot; . $cookie_name . &quot;&#8217; is set!&lt;br&gt;&quot;;<br \/>\n     echo &quot;Value is: &quot; . $_COOKIE[$cookie_name];<br \/>\n}<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code example<\/strong><\/p>\n<p>First, we set the cookies by using variables. The variable <code>$cookie_name<\/code> has the value user and <code>$cookie_value<\/code> has the value John Doe. <\/p>\n<p>Then on the next line, we are using the <code>setcookie()<\/code> function to set the cookie for the user. <\/p>\n<p><code>setcookie($cookie_name, $cookie_value, time() + (86400 * 30), \"\/\"); \/\/ 86400 = 1 day<\/code><\/p>\n<p>In the <code>setcookie()<\/code> function the first variable is used for the user. The second is used for value and finally, the time period is described by using <code>time()<\/code> function. We have declared 30 days for the cookie to set on the user computer. The &#8220;<code>\/<\/code>&#8221; is describing that we want to set the cookie for the entire website.<\/p>\n<p>Note: The cookie function must start before the HTML code.<\/p>\n<p>Once the cookie function is set, we will produce the outcome in the HTML format.<\/p>\n<p>We are using if statement with isset function to set the cookie. The global variable <code>$_COOKIE<\/code> is used in the <code>isset()<\/code> function.<br \/>\nOn the next line, we are using echo statement to check the browser whether the cookie is set or not. If it is not set then it will show is not set message. Else it will render the else statement and show the output.<\/p>\n<p>The code generates the following result.<br \/>\n<code><br \/>\nCookie 'user' is set!<br \/>\nValue is: John Doe<\/code><\/p>\n<p><strong>Delete a Cookie<\/strong><\/p>\n<p>The cookie can be deleted by using the <code>setcookie()<\/code> function with the expiration date in the past.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n\/\/ set the expiration date to one hour ago<br \/>\nsetcookie(&quot;user&quot;, &quot;&quot;, time() &#8211; 3600);<br \/>\n?&gt;[\/php]<\/p>\n<p>[php]&lt;?php<br \/>\necho &quot;Cookie &#8216;user&#8217; is deleted.&quot;;<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>In the above code, the setcookie() function is set to the past timer which in this case is &#8220;<code>-3600<\/code>&#8220;. This will delete the cookie completely.<\/p>\n<p><strong>Check if Cookies are Enabled<\/strong><\/p>\n<p>You can run the function count to check the cookies are enable or disable.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\nif(count($_COOKIE) &gt; 0) {<br \/>\n    echo &quot;Cookies are enabled.&quot;;<br \/>\n} else {<br \/>\n    echo &quot;Cookies are disabled.&quot;;<br \/>\n}<br \/>\n?&gt;[\/php]<\/p>\n<p><strong>Code Explanation;<\/strong><\/p>\n<p>By using if statement we first count the cookie. If it shows value bigger than zero then it will state the cookies are enabled. Else the next echo statement will render saying the cookies are disabled.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is a Cookie? The cookies are often used to identify users. A cookie contains a small file that server embeds on the user&#8217;s computer<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-361","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/comments?post=361"}],"version-history":[{"count":1,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/361\/revisions"}],"predecessor-version":[{"id":362,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/361\/revisions\/362"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}