bigup401 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: auto create cookies
by hippo (Archbishop) on Jan 25, 2019 at 11:30 UTC
    is it possible to create cookies automatically when i open the page and replace the cookies with other value every time the page is opened

    Yes.

      ok here is my try. but the script keeps redirecting and getting error in browser. i want to set cookies then continue doing other thing on same page

      my $val = "xttsyyd6774"; my $link = "http://localhost/test.pl?val=$val"; # main link if ($val) { my $cookie = $cgi->cookie(-name => 'demo', -value => $val, -expires => + '+20m'); print $cgi->redirect(-location =>$link, -cookie =>$cookie); next; # after creating our cookie with one time redirect then continue + do other staffs on page } <p>so after creating cookie and page comes back from redirect then i c +ontinue doing other things on page. but am getting this error</p> <code> This page isn’t working localhost.com redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS

      the main solution i want. is to redirect once. if the page is opened

        i want to set cookies then continue doing other thing on same page

        In which case, don't redirect.

Re: auto create cookies
by marto (Cardinal) on Jan 25, 2019 at 11:14 UTC

      i thought this. cookies are created on submit. not auto creating itself when page opens

        "i thought this. cookies are created on submit. not auto creating itself when page opens"

        You wrote Re^2: delete cookie (cgi101). If you don't want to wait for a submit, why would you? Before posting re-read your previous questions on the same topics, I've already given you links to documents teaching you how all of the works, maybe you should spend time reading that. Again your strategy for working is fundamentally flawed.

Re: auto create cookies
by harangzsolt33 (Deacon) on Jan 27, 2019 at 20:27 UTC
    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>

      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.)