{"id":1330,"date":"2022-07-18T10:47:54","date_gmt":"2022-07-18T10:47:54","guid":{"rendered":"https:\/\/samyakonline.biz\/blog\/?p=1330"},"modified":"2023-07-27T09:13:01","modified_gmt":"2023-07-27T09:13:01","slug":"cookie-consent-code-in-php-for-your-website","status":"publish","type":"post","link":"https:\/\/samyakonline.biz\/blog\/cookie-consent-code-in-php-for-your-website.html","title":{"rendered":"Cookie Consent Code Example in PHP to integrate into your HTML Website"},"content":{"rendered":"<p class=\"post-modified-info\">Last Updated on July 27, 2023 by <a href=\"https:\/\/samyakonline.biz\/blog\/author\/samyakonline\" class=\"last-modified-author\">Subhash Jain<\/a><\/p>\n\n\n<p>Do you want Cookie Consent Code free open source code to integrate into your website? We put down here Cookie Consent Example Code in PHP which is compliant with Privacy Policy &amp; GDPR.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #000000;color:#000000\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #000000;color:#000000\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/samyakonline.biz\/blog\/cookie-consent-code-in-php-for-your-website.html\/#Step_I_CSS_Styling_Code_Example_for_Cookie_Consent_Footer_Banner\" >Step I: CSS Styling Code Example for Cookie Consent Footer Banner<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/samyakonline.biz\/blog\/cookie-consent-code-in-php-for-your-website.html\/#Step_II_PHP_Code_to_Set_Cookie_and_Check_Cookie_Status\" >Step II: PHP Code to Set Cookie and Check Cookie Status<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/samyakonline.biz\/blog\/cookie-consent-code-in-php-for-your-website.html\/#Step_III_Free_Cookies_Policy_Generator_for_Your_Website_App\" >Step III: Free Cookies Policy Generator for Your Website, App<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/samyakonline.biz\/blog\/cookie-consent-code-in-php-for-your-website.html\/#Step_IV_How_to_Check_Cookie_through_Firefox_Browser_Developer_Tool\" >Step IV: How to Check Cookie through Firefox Browser Developer Tool<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_I_CSS_Styling_Code_Example_for_Cookie_Consent_Footer_Banner\"><\/span>Step I: CSS Styling Code Example for Cookie Consent Footer Banner<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;style&gt;\n                \/*--------------- BOF footer-cookie ----------*\/\n                .footer-cookie {\n                position: fixed;\n                left: 0;\n                bottom: 0;\n                width: 100%;\n                background-color:rgba(0,0,0, .85);\n                color: white;\n                padding:25px 0;\n                font-size:.8em;\n                }\n                \/*--------------- EOF footer-cookie ----------*\/\n                &lt;\/style&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_II_PHP_Code_to_Set_Cookie_and_Check_Cookie_Status\"><\/span>Step II: PHP Code to Set Cookie and Check Cookie Status<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We will create&nbsp;two PHP snippet code&nbsp;&#8211;&nbsp;<code>one cookie header code<\/code>&nbsp;that we will place at the top of our web page before&nbsp;<code>html tag&nbsp;<\/code>. It is important to note that there should be no blank space, no code before this code. Otherwise, there would be an error and code will not work. Cookies must be sent before any output from our script in the web page. Please note that dots aren&#8217;t allowed in&nbsp;<code>PHP Cookie Names<\/code>. We have set&nbsp;<code>Cookie time to expire after 30 days<\/code>. You may adjust it as per your needs.<\/p>\n\n\n\n<p>And,&nbsp;<code>second cookie footer code<\/code>&nbsp;which will check if&nbsp;<code>cookie<\/code>&nbsp;is already set or not. If not set, then HTML code for&nbsp;<code>cookie consent<\/code>&nbsp;will be displayed at the footer of the website, otherwise not.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n                            \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ BOF Cookie Header Code \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n                            \/\/ Let us define cookie when we ONLINE and OFFLINE.\n                            define('COOKIE_ONLINE', true);\n                            \/\/ Dots aren't allowed In PHP Cookie Names\n                            if(!isset($_COOKIE&#91;'cc']))\n                            {\n                            \/*------------------------ BOF Set Cookie  ---------------------------------*\/\n                                $cookie_value = md5(('ILOVEMYWEBSITE').'samyakonline.biz');\n                                \/*------------------------ EOF Set Cookie  ---------------------------------*\/\n                                if(COOKIE_ONLINE)\n                                {\n                                \/\/ setcookie(name, value, expire, path, domain, secure, httponly);\n                                setcookie('cc',  $cookie_value, time()+60*60*24*30, '\/', '.samyakonline.biz', 'true', 'true');\n                                }\n                                else\n                                {\n                                setcookie('cc', $cookie_value, time()+60*60*24*30, '\/');\n                                \n                                }\n                            }\n                        ?&gt;\n                        \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ EOF Cookie Header Code \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n                        \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ BOF Cookie Footer Code \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n                        &lt;?php\n                            if(!isset($_COOKIE&#91;'cc']))\n                            {  \n                            ?&gt;\n                                &lt;div class=\"footer-cookie\" id=\"cookieBar\"&gt;\n                                &lt;div class=\"container\"&gt;\n                                &lt;div class=\"col-md-12  align-items-center\"&gt;\n                                This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies policy.\n                                    \/\/ Replace URL by your website Privacy Policy URL \n                                    &lt;a href=\"https:\/\/samyakonline.biz\/cookies-policy.html\" class=\"btn btn-outline-light ml-2\"&gt;See Details&lt;\/a&gt; \n                                &lt;a href=\"#\" class=\"btn btn-light mr-2\" onclick=\"getElementById('cookieBar').style.display='none'\"&gt;OK&lt;\/a&gt; \n                                &lt;\/div&gt;&lt;\/div&gt;\n                                &lt;\/div&gt;\n                                &lt;\n                                &lt;?php \n                            }\n                            ?&gt;\n                        \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ BOF Cookie Footer Code \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_III_Free_Cookies_Policy_Generator_for_Your_Website_App\"><\/span>Step III: Free Cookies Policy Generator for Your Website, App<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We have created&nbsp;<a href=\"https:\/\/samyakonline.biz\/free-cookies-policy-template.html\">&#8220;Free Cookies Policy Generator for Your Website App&#8221;<\/a>, that you can download and integrate into your website by doing small customizations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_IV_How_to_Check_Cookie_through_Firefox_Browser_Developer_Tool\"><\/span>Step IV: How to Check Cookie through Firefox Browser Developer Tool<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let us press&nbsp;<code>F12 key<\/code>&nbsp;and go to the&nbsp;<code>Network Tab<\/code>&nbsp;and select URL and go to&nbsp;<code>Cookies Tab<\/code>&nbsp;in the right navigation.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"410\" src=\"https:\/\/samyakonline.biz\/blog\/wp-content\/uploads\/2022\/07\/image-2-1030x410.png\" alt=\"How to Check Cookie through Firefox Browser Developer Tool\" class=\"wp-image-1332\" srcset=\"https:\/\/samyakonline.biz\/blog\/wp-content\/uploads\/2022\/07\/image-2-1030x410.png 1030w, https:\/\/samyakonline.biz\/blog\/wp-content\/uploads\/2022\/07\/image-2-300x119.png 300w, https:\/\/samyakonline.biz\/blog\/wp-content\/uploads\/2022\/07\/image-2-768x305.png 768w, https:\/\/samyakonline.biz\/blog\/wp-content\/uploads\/2022\/07\/image-2-705x280.png 705w, https:\/\/samyakonline.biz\/blog\/wp-content\/uploads\/2022\/07\/image-2-450x179.png 450w, https:\/\/samyakonline.biz\/blog\/wp-content\/uploads\/2022\/07\/image-2.png 1343w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button aligncenter\"><a class=\"wp-block-button__link has-white-color has-vivid-cyan-blue-background-color has-text-color has-background wp-element-button\" href=\"https:\/\/samyakonline.biz\/scripts\/demo\/downloads\/Cookie-Consent-Code-Example-in-PHP.zip\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Download Cade For Cookie Consent Code Example in PHP to integrate into your HTML Website<\/strong><\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Do you want Cookie Consent Code free open source code to integrate into your website? We put down here Cookie Consent Example Code in PHP which is compliant with Privacy Policy &amp; GDPR. Step I: CSS Styling Code Example for Cookie Consent Footer Banner Step II: PHP Code to Set Cookie and Check Cookie Status [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1333,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1330","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-scripts"],"modified_by":"Anil","_links":{"self":[{"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/posts\/1330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/comments?post=1330"}],"version-history":[{"count":0,"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/posts\/1330\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/media\/1333"}],"wp:attachment":[{"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/media?parent=1330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/categories?post=1330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/samyakonline.biz\/blog\/wp-json\/wp\/v2\/tags?post=1330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}