Cookie Consent Code Example in PHP to integrate into your HTML Website

Cookie Consent Code Example in PHP to integrate into your HTML Website

Last Updated on July 27, 2023 by Subhash Jain

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 & GDPR.

Step I: CSS Styling Code Example for Cookie Consent Footer Banner

<style>
                /*--------------- BOF footer-cookie ----------*/                .footer-cookie {
                position: fixed;
                left: 0;
                bottom: 0;
                width: 100%;
                background-color:rgba(0,0,0, .85);
                color: white;
                padding:25px 0;
                font-size:.8em;
                }
                /*--------------- EOF footer-cookie ----------*/                </style>

Step II: PHP Code to Set Cookie and Check Cookie Status

We will create two PHP snippet code – one cookie header code that we will place at the top of our web page before html tag . 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’t allowed in PHP Cookie Names. We have set Cookie time to expire after 30 days. You may adjust it as per your needs.

And, second cookie footer code which will check if cookie is already set or not. If not set, then HTML code for cookie consent will be displayed at the footer of the website, otherwise not.

<?php
                            ///////////////////// BOF Cookie Header Code //////////////////////////
                            // Let us define cookie when we ONLINE and OFFLINE.
                            define('COOKIE_ONLINE', true);
                            // Dots aren't allowed In PHP Cookie Names
                            if(!isset($_COOKIE['cc']))
                            {
                            /*------------------------ BOF Set Cookie  ---------------------------------*/                                $cookie_value = md5(('ILOVEMYWEBSITE').'samyakonline.biz');
                                /*------------------------ EOF Set Cookie  ---------------------------------*/                                if(COOKIE_ONLINE)
                                {
                                // setcookie(name, value, expire, path, domain, secure, httponly);
                                setcookie('cc',  $cookie_value, time()+60*60*24*30, '/', '.samyakonline.biz', 'true', 'true');
                                }
                                else
                                {
                                setcookie('cc', $cookie_value, time()+60*60*24*30, '/');
                                
                                }
                            }
                        ?>
                        ///////////////////// EOF Cookie Header Code //////////////////////////

                        ///////////////////// BOF Cookie Footer Code //////////////////////////
                        <?php
                            if(!isset($_COOKIE['cc']))
                            {  
                            ?>
                                <div class="footer-cookie" id="cookieBar">
                                <div class="container">
                                <div class="col-md-12  align-items-center">
                                This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies policy.
                                    // Replace URL by your website Privacy Policy URL 
                                    <a href="https://samyakonline.biz/cookies-policy.html" class="btn btn-outline-light ml-2">See Details</a> 
                                <a href="#" class="btn btn-light mr-2" onclick="getElementById('cookieBar').style.display='none'">OK</a> 
                                </div></div>
                                </div>
                                <
                                <?php 
                            }
                            ?>
                        ///////////////////// BOF Cookie Footer Code ////////////////////////// 

Step III: Free Cookies Policy Generator for Your Website, App

We have created “Free Cookies Policy Generator for Your Website App”, that you can download and integrate into your website by doing small customizations.

Step IV: How to Check Cookie through Firefox Browser Developer Tool

Let us press F12 key and go to the Network Tab and select URL and go to Cookies Tab in the right navigation.

How to Check Cookie through Firefox Browser Developer Tool
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *