in reply to auto create cookies

So you want to create a cookie when the page loads? I know this is a Perl forum, BUT you could do this in JavaScript very easily.

(JavaScript is very similar to Perl in syntax, and in addition, you can use the $ sign anywhere in a variable name. Or you may decided to not use it at all. It's up to you. If you like to write code that resembles perl, you can start each variable name with a dollar sign. There is absolutely no difference between a ' and " signs for declaring strings. You use whichever you like. Also, instead of using a dot to merge two strings, you must use a plus sign.)

So, anyway, you just put this in a HTML file, and it will create a cookie called "MyCookie" which expires in 5 years, and then it will read that same cookie. And if the read is successful, then it prints that cookies are working fine. So, it's a simple code. Your perl program would simply print this entire block anywhere within the HTML file, and if you want to change the value of the cookie, just make sure it's a valid string. The string declaration in JavaScript is just like in C or C++. Same syntax. Also, you may not include any special characters in a cookie value other than listed in a variable named $COOKIEJAR. So, those are the only valid characters you may use in a cookie.

The difference between localStorage is that you may store any kind of binary data in localStorage. LocalStorage is usually 10 MB in size. Cookies can only store up to 5 KB, and if your website creates more than what fits in that space, then you simply won't be able to create anymore cookies. You have to delete some in order to free up some space. Cookies also have expiration, while localStorage does not have expiration. Also, whatever you put in localStorage does not get sent back to the server with each page request. So, they are hidden. Only the JavaScript program can read and write to localStorage.

Note: Some very old browsers used in old smart phones do not support localStorage. And some browsers may have cookies turned off but localStorage is enabled! So, if it is an absolute MUST for you to store something on the visitor's computer, then you should check both cookies and localStorage to see which one is available and working.

<SCRIPT> /* JAVASCRIPT CODE BEGINS HERE */ // DETECT COOKIES & LOCAL STORAGE COOKIES_AVAILABLE = navigator.cookieEnabled ? 1 : 0; STORAGE_AVAILABLE = 0; try { localStorage.getItem("X"); STORAGE_AVAILA +BLE = 1; } catch (e) {} $COOKIEJAR = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst +uvwxyz$@!?*|#&%^`~+:-., []{}()_/\\><\"'"; SetCookie("MyCookie", "Hello World!!!"); document.write( "<P>LocalStorage is " + (STORAGE_AVAILABLE ? 'ENABLED +and WORKING' : 'NOT WORKING, NOT SUPPORTED, or DISABLED.') ); document.write( "<P>Your browser says that cookies are " + (COOKIES_AV +AILABLE ? 'SUPPORTED and ENABLED' : 'DISABLED or NOT SUPPORTED') ); document.write( "<P>" ); if (GetCookie("MyCookie") == "Hello World!!!") { document.write("In reality, cookies are supported and are working fi +ne!"); } else { if (document.cookie.length > 1000) document.write("In reality, cookies aren't working, because your c +ookie storage is full. There are probably too many cookies."); else document.write("In reality, cookies aren't working!"); } document.write( "<P>The cookies are = " + document.cookie ); //////////////////////////////////// // // COOKIE & LOCAL STORAGE FUNCTIONS: // Creates a cookie that expires in 5 years. function SetCookie(NAME, VALUE) { var E = new Date(); E.setFullYear(E. +getFullYear() + 5); document.cookie = NAME + "=" + VALUE + ";Expires= +" + E.toGMTString(); } // Returns the value of a cookie. function GetCookie(NAME) { var i, e, s, K = document.cookie.split(";") +; for (i = 0; i < K.length; i++) { for (s = 0; K[i].charCodeAt(s) < 3 +3; s++); e = K[i].indexOf("="); if (K[i].substring(s, e) == NAME) ret +urn K[i].slice(++e); } return ""; } // Returns an array of cookie names. function GetCookieNames() {var i,S,L,C,K=document.cookie+";",SAVE=1,A= +[];for(L=S=i=0;i<K.length;i++){C=K.charCodeAt(i);if(C==61||C==59){if( +SAVE)A.push(K.substr(S,L));L=S=0;SAVE=(C==59)?1:0;}else if(SAVE&&C>32 +)if(L++==0)S=i;}return A;} // Completely erases a cookie. function DeleteCookie(NAME) {document.cookie=NAME+"=;Expires=Jan 01 19 +70 01:01:01 GMT";} // Deletes all cookies. function DeleteAllCookies() {var i,S,L,C,K=document.cookie+";",SAVE=1, +CC=0;for(L=S=i=0;i<K.length;i++){C=K.charCodeAt(i);if(C==61||C==59){i +f(SAVE){DeleteCookie(K.substr(S,L));CC++;}L=S=0;SAVE=(C==59)?1:0;}els +e if(SAVE&&C>32)if(L++==0)S=i;}return CC;} // Saves a value in localStorage. function SetItem(NAME, VALUE) {try{with(window.localStorage){setItem(N +AME,VALUE);if(getItem(NAME)==VALUE)return 1;}}catch(e){}return 0;} // Retrieves a value from localStorage. function GetItem(NAME) {var V=null;try{V=localStorage.getItem(NAME);}c +atch(e){}return V;} // Returns a list of names stored in localStorage. function GetItemNames() {var i,A=[];try{for(i=0;i<localStorage.leng +th;i++)A[i]=localStorage.key(i);}catch(e){}return A;} // Erases an item from localStorage. function DeleteItem(NAME) {try{with(window.localStorage){setItem(NAME, +"");removeItem(NAME);}return 1;}catch(e){}return 0;} // Empties localStorage. function DeleteAllItems() {try{localStorage.clear();return 1;}catch(e) +{}return 0;} </SCRIPT>

Replies are listed 'Best First'.
Re^2: auto create cookies
by marto (Cardinal) on Jan 28, 2019 at 09:20 UTC

    You may want to check some of your stated limits. RFC 6265 HTTP State Management Mechanism:

    6.1. Limits Practical user agent implementations have limits on the number and size of cookies that they can store. General-use user agents SHOUL +D provide each of the following minimum capabilities: o At least 4096 bytes per cookie (as measured by the sum of the length of the cookie's name, value, and attributes). o At least 50 cookies per domain. o At least 3000 cookies total. Servers SHOULD use as few and as small cookies as possible to avoid reaching these implementation limits and to minimize network bandwidth due to the Cookie header being included in every request. Servers SHOULD gracefully degrade if the user agent fails to return one or more cookies in the Cookie header because the user agent mig +ht evict any cookie at any time on orders from the user.
      Oh, you may be right on that. I didn't know the limitations were written down anywhere. I thought it changes from browser to browser. I wrote a test program one time that created a large cookie in order to see how many bytes I can store in a single cookie. Lol I have noticed that each browser had different limits, but I came to the conclusion that you can safely store up to 5KB. (I tested MSIE, Opera, Safari, QupZilla, KMeleon, Chrome, and Firefox.)