简体   繁体   中英

How and where should I set the parameters for cookies?

First I want to apologize for the spelling, I must use the translator. Hi, I'm trying to create the code that controls the validity time of one, so that the browser remembers the web and does not have to load it, but I did not do that in a course I did. I have searched for information about it and not with the code that I create, I see that the browser does not remember the web. I have read something about the parameters expires and max-age, but I do not know how or where I should put them. I show what has been achieved so far, but it does not do the function that I expected for the browser to remember my website. If you can help me, there is some place where I can find a way to learn to write the code and where to put it, or explain how to deboprocede. Thank you.

 function getCookie(c_name) { var c_value = document.cookie; var c_start = c_value.indexOf(" " + c_name + "="); if (c_start == -1) { c_start = c_value.indexOf(c_name + "="); } if (c_start == -1) { c_value = null; } else { c_start = c_value.indexOf("=", c_start) + 1; var c_end = c_value.indexOf(";", c_start); if (c_end == -1) { c_end = c_value.length; } c_value = unescape(c_value.substring(c_start, c_end)); } return c_value; } function setCookie(c_name, value, exdays) { var exdate = new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()); document.cookie = c_name + "=" + c_value; } if (getCookie('tiendaaviso') != "1") { document.getElementById("barraaceptacion").style.display = "block"; } function PonerCookie() { setCookie('tiendaaviso', '1', 365); document.getElementById("barraaceptacion").style.display = "none"; } 
 #cookies { background-color: #333; display:none; position:fixed; left:0px; right:0px; bottom:0px; padding-bottom:60px; width:100%; text-align:center; min-height:40px; background-color: rgba(0, 0, 0, 0.5); color:#fff; z-index:99999; } .inner { width:100%; position:absolute; padding-left:5px; font-family:verdana; font-size:12px; top:30%; } .inner a.ok {padding:4px;color:#00ff2e;text-decoration:none;} .inner a.info {padding-left:5px;text-decoration:none;color:#faff00;} 
 <!DOCTYPE html> <html lang="es"> <head> <title>cokies</title> </head> <body> <div id="cookies"> <div class="inner"> Esta web utiliza cookies como datos estad&iacute;sticos de su navegaci&oacute; Si contin&uacute;a navegando consideramos que acepta el uso de cookies. <a href="javascript:void(0);" class="ok" onclick="PonerCookie();"> <b>OK</b> </a> | <a href="politica-cookies.html" target="_blank" class="info">M&aacute;s informaci&oacute;n</a> </div> </div> </body> </html> 

Did you already try using php for cookies. I managed to handle cookies quiet easily with php.

See the documentation .

set cookie:

<?php 
    setcookie("name","value",time()+$int);
    /*name is your cookie's name
    value is cookie's value
    $int is time of cookie expires*/
?>

get cookie:

<?php 
    echo $_COOKIE["your cookie name"];
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM