I ahve this JS Code but it is not setting up the h...
# cfml-general
g
I ahve this JS Code but it is not setting up the httpcookie to true, but settings only https to trye, i want both to be true how can i do it
Copy code
<script>
    document.getElementById("acceptButton").addEventListener("click", function() {
        var now = new Date();
        now.setMonth(now.getMonth() + 3); // Set expiration for 90 days from now
        var cID = document.getElementById("acceptButton").getAttribute("data-ID");
 	 
    	document.cookie = "cookieAccepted_" + cID + "=true;path=/;secure";
        document.getElementById("cookieAlert").style.display = "none";
    });
</script>
i tried in cf too but its the same
Copy code
<cfscript>
    // Assuming you have assigned the ID to a ColdFusion variable named cID
    var now = createObject("java", "java.util.Date").init();
    now.setMonth(now.getMonth() + 3);

    var cookieValue = "cookieAccepted_" & cID & "=true; expires=" & now.toUTCString() & "; path=/; secure";
    cfcookie(name="cookieAccepted_#cID#", value="true", expires="#now.toUTCString()#");
</cfscript>

<script>
    // Assuming you have set the appropriate ID for the cookie alert element
    document.getElementById("cookieAlert").style.display = "none";
</script>
want this both to be true how it can done
m
Does this help? https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies
A cookie with the
Secure
attribute is only sent to the server with an encrypted request over the HTTPS protocol. It's never sent with unsecured HTTP (except on localhost), which means man-in-the-middle attackers can't access it easily. Insecure sites (with
http:
in the URL) can't set cookies with the
Secure
attribute
Yours has "secure" therefore it's never sent with unsecured http.
g
but how bath can be true n
in one case is true and anotehr case it is false
in my screenshot
m
well, in your CF, you use "secure" in
cookieValue
but don't use that variable when actually setting your cookie.
☝🏻 1
I'm no expert on cookies (except chocolate chip), but what happens if you try to set it twice, once with secure and once without? But I guess I don't see the point in doing both.
g
so do i need to do from server side, what is the expected out should do do, do you mean to add both using secure and non secure
b
Suggestion: update the date object - and not the month - by 3 months.
m
You could try setting your cookie both with and without "secure" , but, again, not a cookie expert, I don't see the point in sending both.
g
if i do with coldfusion, i see the cookie but httpOnly and secure both are false here is my code
Copy code
<cfscript>
    nowDate = createObject("java", "java.util.Date").init();
    nowDate.setMonth(nowDate.getMonth() + 3);

    // Format the date to a string
    DateFormat = createObject("java", "java.text.SimpleDateFormat");
    UTCFormat = DateFormat.init("EEE, dd MMM yyyy HH:mm:ss 'GMT'", createObject("java", "java.util.Locale").init("US"));
    UTCFormat.setTimeZone(createObject("java", "java.util.TimeZone").getTimeZone("UTC"));
    nowDateString = UTCFormat.format(nowDate);

    cookieValue = "cookieAccepted_" & cID & "=true; expires=" & nowDateString & "; path=/; httpOnly; secure";
    cfcookie(name="cookieAccepted_#cID#", value="true", expires="#nowDateString#");
</cfscript>

<script>
    // Assuming you have set the appropriate ID for the cookie alert element
    document.getElementById("cookieAlert").style.display = "none";
</script>