I want to create a cookie everytime a user deletes it, using Javascript.
For example, I have many cookies, even some cookies which set a ban time to the user who abuses the website.
Basically I want to set a cookie which will be set whenever all of the cookies are deleted.
Btw, I do not want to create the same cookie back. But, I just want to create an indication cookie.
Advertisement
Answer
Here is how I create a cookie every time the user deletes it:
<?php if ($_COOKIE["CookieIsThere"] == "no") { echo "Why did you delete the cookies? Come on, we wanted to help you!"; setcookie("CookieIsThere", "yes"); } else { setcookie("CookieIsThere", "yes"); } echo "<br>". $_COOKIE["CookieIsThere"]; ?> <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script> <script> function getCookie(name) { var dc = document.cookie; var prefix = name + "="; var begin = dc.indexOf("; " + prefix); if (begin == -1) { begin = dc.indexOf(prefix); if (begin != 0) return null; } else { begin += 2; var end = document.cookie.indexOf(";", begin); if (end == -1) { end = dc.length; } } // because unescape has been deprecated, replaced with decodeURI //return unescape(dc.substring(begin + prefix.length, end)); return decodeURI(dc.substring(begin + prefix.length, end)); } function doSomething() { var myCookie = getCookie("CookieIsThere"); if (myCookie == null) { // do cookie doesn't exist stuff; document.cookie = "CookieIsThere=no"; location.reload(); console.log(myCookie); } else { // do cookie exists stuff console.log(myCookie); } } setInterval(function () { doSomething(); }, 1000); </script>