{"id":2366,"date":"2016-03-18T13:30:01","date_gmt":"2016-03-18T20:30:01","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2366"},"modified":"2016-03-18T13:33:59","modified_gmt":"2016-03-18T20:33:59","slug":"browser-cookies-with-php-and-javascript","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2366","title":{"rendered":"Browser Cookies with PHP and JavaScript"},"content":{"rendered":"<p>I have a game that I am in the process of updating. Originally it passed scores in with the URL, but I decided to update it so that the scores are not visible. My first thought was to use session variables, but since I want to change the scores in response to calculations that are done in JavaScript, that isn\u2019t feasible. A better way is to use cookies. When I first enter a page, I set or get the cookies with PHP. I want the game levels to persist from visit to visit but the scores are only kept for the session.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n\/\/ Initialize the cookies with PHP. Keep the group and level around but make the scoring session cookies\n\/\/ If the cookie is set by the code, it is not available until the next page load\n$cookie_name = &quot;VPA_group&quot;;\n$cookie_value = &quot;MW1&quot;;\nif( !isset($_COOKIE[$cookie_name]) ) {\n&nbsp;&nbsp;&nbsp;&nbsp;setcookie($cookie_name,$cookie_value,$cookie_time,$cookie_path,$cookie_domain,$cookie_secure,$cookie_httponly);\n}\n$group&nbsp;&nbsp;= isset($_COOKIE[&quot;VPA_group&quot;])&nbsp;&nbsp;? $_COOKIE[&quot;VPA_group&quot;]&nbsp;&nbsp; : $cookie_value;\n\n$cookie_name = &quot;VPA_WFF&quot;;\n$cookie_value = &quot;Wide&quot;;\nif( !isset($_COOKIE[$cookie_name]) ) {\n&nbsp;&nbsp;&nbsp;&nbsp;setcookie($cookie_name,$cookie_value,$cookie_time,$cookie_path,$cookie_domain,$cookie_secure,$cookie_httponly);\n}\n$WFF&nbsp;&nbsp;&nbsp;&nbsp;= isset($_COOKIE[&quot;VPA_WFF&quot;])&nbsp;&nbsp;&nbsp;&nbsp;? $_COOKIE[&quot;VPA_WFF&quot;]&nbsp;&nbsp;&nbsp;&nbsp; : $cookie_value;\n\n$cookie_name = &quot;VPA_cor&quot;;\n$cookie_value = 0;\nif( !isset($_COOKIE[$cookie_name]) ) {\n&nbsp;&nbsp;&nbsp;&nbsp;setcookie($cookie_name,$cookie_value);\n}\n$cor&nbsp;&nbsp;&nbsp;&nbsp;= isset($_COOKIE[&quot;VPA_cor&quot;])&nbsp;&nbsp;&nbsp;&nbsp;? $_COOKIE[&quot;VPA_cor&quot;]&nbsp;&nbsp;&nbsp;&nbsp; : $cookie_value;\n\n$cookie_name = &quot;VPA_inc&quot;;\n$cookie_value = 0;\nif( !isset($_COOKIE[$cookie_name]) ) {\n&nbsp;&nbsp;&nbsp;&nbsp;setcookie($cookie_name,$cookie_value);\n}\n$inc&nbsp;&nbsp;&nbsp;&nbsp;= isset($_COOKIE[&quot;VPA_inc&quot;])&nbsp;&nbsp;&nbsp;&nbsp;? $_COOKIE[&quot;VPA_inc&quot;]&nbsp;&nbsp;&nbsp;&nbsp; : $cookie_value;\n\n$cookie_name = &quot;VPA_screen&quot;;\n$cookie_value = 0;\nif( !isset($_COOKIE[$cookie_name]) ) {\n setcookie($cookie_name,$cookie_value);\n}\n$screen = isset($_COOKIE[&quot;VPA_screen&quot;]) ? $_COOKIE[&quot;VPA_screen&quot;]&nbsp;&nbsp;: $cookie_value;<\/code><\/pre><\/p>\n<p>Once I have the values, I need to let JavaScript know what they are.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n&lt;script&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;?php\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;var cor = $cor;\\n&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;var inc = $inc;\\n&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;var screen = $screen;\\n&quot;;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;var group = \\&quot;$group\\&quot;;\\n&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;var WFF = \\&quot;$WFF\\&quot;;\\n&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;var cookie_days = \\&quot;$cookie_days\\&quot;;\\n&quot;;\n&nbsp;&nbsp;&nbsp;&nbsp;?&gt;\n&lt;\/script&gt;<\/code><\/pre><\/p>\n<p>Then I can use JavaScript to change them, as I described in the previous post.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nfunction goNext() {\n&nbsp;&nbsp;&nbsp;&nbsp;setCookie(&#039;VPA_cor&#039;,cor,&#039;N&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;setCookie(&#039;VPA_inc&#039;,inc,&#039;N&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;setCookie(&#039;VPA_screen&#039;,screen,&#039;N&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;window.location=&quot;ThisGame.php&quot;;\n}\n\nfunction changeLevel(cookieName, cookieValue) {\n&nbsp;&nbsp;&nbsp;&nbsp;cor = 0;\n&nbsp;&nbsp;&nbsp;&nbsp;inc = 0;\n&nbsp;&nbsp;&nbsp;&nbsp;screen = 0;\n&nbsp;&nbsp;&nbsp;&nbsp;setCookie(&#039;VPA_cor&#039;,cor,&#039;N&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;setCookie(&#039;VPA_inc&#039;,inc,&#039;N&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;setCookie(&#039;VPA_screen&#039;,screen,&#039;N&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;setCookie(cookieName, cookieValue, &#039;Y&#039;);\n}\n\nfunction setCookie(cname, cvalue, set_time) {\n&nbsp;&nbsp;&nbsp;&nbsp;var expires = &#039;&#039;;\n&nbsp;&nbsp;&nbsp;&nbsp;if ( set_time == &#039;Y&#039; ) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var d = new Date();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d.setTime(d.getTime() + (cookie_days*24*60*60*1000));\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expires = &quot;expires=&quot;+d.toUTCString();\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/alert(&#039;cname &#039; + cname + &#039;group &#039; + cvalue + &#039;time &#039; + expires);\n&nbsp;&nbsp;&nbsp;&nbsp;document.cookie = cname + &quot;=&quot; + cvalue + &quot;; &quot; + expires;\n}\n&lt;\/script&gt;<\/code><\/pre> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a game that I am in the process of updating. Originally it passed scores in with the URL, but I decided to update it so that the scores are not visible. My first thought was to use session variables, but since I want to change the scores in response to calculations that are &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2366\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Browser Cookies with PHP and JavaScript<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-2366","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\/2366","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=2366"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2366\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}