{"id":2352,"date":"2016-03-17T10:41:58","date_gmt":"2016-03-17T17:41:58","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2352"},"modified":"2016-03-18T12:28:54","modified_gmt":"2016-03-18T19:28:54","slug":"browser-cookies","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2352","title":{"rendered":"Browser Cookies with PHP"},"content":{"rendered":"<p><b>PHP<\/b><br \/>\n<i>Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.<\/i> <a href=\"http:\/\/php.net\/manual\/en\/function.setcookie.php\">PHP.net<\/a><\/p>\n<p>Here is an example of a cookie that checks to see how many times a person has visited the site. Note that it expires in 30 days.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n$cookie_name = &quot;VPA&quot;;\n$cookie_days = 30;\n$cookie_time = time()+60*60*24*$cookie_days;\n$cookie_path = &#039;&#039;;\n$cookie_domain = &#039;&#039;;\n$cookie_secure = false;\n$cookie_httponly = true;\n\nif( !isset($_COOKIE[$cookie_name]) ) {\n&nbsp;&nbsp;&nbsp;&nbsp;$cookie_value = 0;\n} else {\n&nbsp;&nbsp;$cookie_value = $_COOKIE[$cookie_name] + 1;\n}\nsetcookie($cookie_name,$cookie_value,$cookie_time,$cookie_path,$cookie_domain,$cookie_secure,$cookie_httponly);<\/code><\/pre><\/p>\n<p>In your code you can check for the value of the cookie like this:<br \/>\n<pre><code class=\"preserve-code-formatting\">\necho &quot;Cookie VPA is $_COOKIE[VPA]&quot;;<\/code><\/pre><\/p>\n<p>The value can be a number or text. Once a cookie is created, you can update the value in the body of your code.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nsetcookie(&quot;VPA&quot;,&quot;red&quot;,$cookie_time);\necho $_COOKIE[&quot;VPA&quot;];<\/code><\/pre><br \/>\nNot that I switched from a number to alpha in this example. The cookies don\u2019t care what the value is or if the type changes. Note that you cannot get the cookie expiration time. And if you leave it off in your setcookie, it will be reset to <i>Session<\/i>.<\/p>\n<p>To get rid of a cookie, set its value to null and time to something in the past\u2014zero works.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nsetcookie($cookie_name,&#039;&#039;,0);\ne.g.\nset cookie(&#039;VPA&#039;,&#039;&#039;,0);\n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace. PHP.net Here is an example of a cookie that checks to see how many times &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2352\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Browser Cookies with PHP<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-2352","post","type-post","status-publish","format-standard","hentry","category-html-and-css"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2352"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2352\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}