phpcookiesadsense

Can't delete or change the value of a cookie set on website with PHP


So normally I have no problems with setting and unsetting cookies for my website. But for some reason I can't delete this cookie that was set on my website by Google AdSense. Basically I was asking my users for consent, so I could show them ads. But I made a fatal mistake and I would like to ask the users again. But AdSense placed a cookie on my website called FCCDCF.

I use the following code to check for cookies and their value

if(isset($_COOKIE)) {
    var_dump($_COOKIE);
    foreach($_COOKIE as  $key => $val)
    {
        echo "cookie name: ".$key."<br /><br />Value: ".$val;
    }
}

This returns the following (I changed the the numbers/letters for safety)

cookie name: FCCDCF

Value: [null,null,null,["WJGBHDKJKNKSFAAEPA1326467585AkCBIAAgAUgQAgFIIAwAIFAAAAA42567869675425AAAAQQAAAAAIAAAAAAAAEAQ7567464563AAAAABAAAAAAAAAAAAAAAAAAAAgAA","1~","CP764359-1234-4321-1246-2SDFSDF34566"],null,null,[]]

Now I would normally think that I could easily delete this cookie by simply using setcookie("FCCDCF", "", time()-3600); - but this does not seem to be the case.

Apparently I do not know enough about cookies to find a solution for this. I have searched for hours now and tried a bunch of different solutions for deleting cookies for your website - but nothing worked so far.

My problem is that my mistake cost my 60% of my revenue for this particular site and if this does not get fixed, the website will close, because it's an expensive website to keep running.

Thank you

EDIT with more details about the cookie and the code I'm using to get rid of it (also see image)

Cookie details:

Name: FCCDCF

Value: [null,null,null,["CPx4kYAPx4kYAEsACBDADWCgAAAAAEPAAAZQAAAQaQD2F2K2kKFgPi2QWYAQBCujIEAhUAAAAkCBIAAgAUgQAgFIIAwAIFAAAAAAAAAQEgCQAAQABAAAIACAAAAAAAIAAAAAAAQQAAAAAIAAAAAAAAEAQAAIAAQAAAAIAABEhCAAQQAEAAAAAAAQAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAgAA","1~","34A656F2-7A38-4C75-88E5-06DDD813736B"],null,null,[]]

Domain: mydomain.com

Path: /

Expires / Max-Age: 2024-10-04T20:35:11.000Z

Size: 343

Priority: Medium

Important: I have tried to overwrite the cookie by creating a new one

I have tried to create a new cookie to see if this would automatically overwrite the current one. It doesn't - it just creates a new cookie with the exact same name (see image further down). I created this cookie like this: setcookie("FCCDCF", "testcookie...", time() + (86400 * 30), "/");

So what code am I using to delete the cookies

First i simply tried this code: setcookie("FCCDCF", "", time() - (86400 * 30), "/"); - that didn't work.

Next @ProfessorAbronsius asked me to set the cookie with the same value but with the time in the past which I did like this:

$CookieValue = $_COOKIE['FCCDCF'];
setcookie("FCCDCF", $CookieValue, time() - (86400 * 30), "/");

This is not working either. The cookie is still there. BUT it deletes the testcookie I set.

Image: enter image description here

THANK you for your time


Solution

  • I found a solution including both PHP and JavaScript.

    January 16th, 2024 Google AdSense will introduce this new consent message and force it to all its accepted websites.

    I don't know why PHP can't delete this cookie

    As the user @Pmpkn already addressed in this question; people are already experiencing this problem and can't find a solution. See the thread here

    Be aware!!

    I only use this script because I made a fatal error at the beginning of the consent message. I won't force any of my users to consent. But I don't run a charity, so I can't let them use up resources that cost me a lot of money. So they can consent or leave the website.

    As you might know, Google let this cookie expire after 13 months. So it's a lot of time with no possibility to ask the users for consent again.

    I don't know if this is allowed in the ToS of Google AdSense - so beware of this. Use at own risk. On the other hand. Deleting a cookie on your OWN website should not be a problem in my opinion. I don't know though.

    I introduce the solution

    As simple as this script might seem - it's the only one working. I have tried every other PHP script - and my conclusion is clear: It just can't delete this specific cookie.

    <script>
    var domainName = window.location.hostname;
    document.cookie = "FCCDCF=...; expires=Tue, 16 Apr 2019 11:31:56 GMT; path=/; domain=." + domainName;
    </script>
    

    var domainName = window.location.hostname; Creates a variable called domainName which will get the exact domain name of the current website.

    In the next line you will actually create the cookie with the values you wish. document.cookie is telling the browser to create a cookie. The following is just values for the specific cookie. I wan't to delete the Cookie called FCCDCF and therefore I name the new cookie exactly that with the value ... like this: FCCDCF=...;

    I end the value with a semicolon. After the semicolon, I can then put in another detail of the cookie. In this case I put in expires=Tue, 16 Apr 2019 11:31:56 GMT; So the date is 4 years ago and therefore the cookie should delete itself.

    Next I put in the path like this: path=/;

    This next step is very important. I need to use the variable I created and put it in the cookie. But! Look out for the . before the variable. The . before the domain is very much needed, because AdSense creates the cookie with a dot before it, so the cookie also is included in sub domains.

    So it should look like this: domain=." + domainName;

    The problem with this script alone

    So this script successfully deletes the cookie called FCCDCF and that's great. But the thing is that it deletes the cookie every single time the page is refreshed or the visitor is clicking around on the website. This could be pretty annoying to accept cookies on every page view.

    I solved this by looking at the value of the FCCDCF-cookie when someone did consent and when someone didn't consent.

    The value of the cookie when someone consents, is around ~800 characters long - give or take.

    When someone does not consent** the value of the cookie is only around ~300 characters.

    So with PHP I would simply take the value of the cookie and count the characters of this exact cookie. If the characters of the cookie are lower than 600 characters (which means that they did not consent) - I would show the consent message again.

    The complete script that I made

    <?php
        $CookieValue = $_COOKIE['FCCDCF'];
        
        $CookieChar = strlen($CookieValue);
        
        if($CookieChar < "600") {
    ?>
    <script>
        var domainName = window.location.hostname;
        document.cookie = "FCCDCF=...; expires=Tue, 16 Apr 2019 11:31:56 GMT; path=/; domain=." + domainName;
    </script>
    <?php
        }
    ?>