Jump to content

cookies


timeyyy_da_man

Recommended Posts

Regarding setCookie()1. how come when ur making a cookie you have to change it to gmt time?2. how come after

document.cookie = c_name + "="

you have to add the "=" bit3. what does the escape() function do in the setCookie function,?4. if expiredays == null what happens?5. how come after the

(( expiredays==null) ? "":

we only have to enclose the expires = bit in "" and not the rest

"expires="

6. why is there a + after the expires bit

"expires="+exdate.toGMTString())

Regarding getCookie()1. what does

(c_name + "=")

do in

c_start=document.cookie.indexOf(c_name + "=")

2. how does returning the value of c_start (which is the index of [c_name + "="]) , added on to the length of c_name + 1, return its value,

The function above (getCookie) first checks if a cookie is stored at all in the document.cookie object. If the document.cookie object holds some cookies, then check to see if our specific cookie is stored. If our cookie is found, then return the value, if not - return an empty string
3. Also what does the ";" bit do in
c_end=document.cookie.indexOf(";",c_start)

4. im geussing question 2 has something to do with return unescpe and also the substring bit5. is there a simple way to get a cookie value like a=document.cookie.c_name or something?Regarding checkCookie()1. how come when we use the function we use username for the value and the name, what does sticking the first username in the two '' do? if (expiredays!=null) { document.cookie=nome + "=" escape(value) + expires=+exdate.toGMTString() }

function checkCookie(){username=getCookie('username')if (username!=null && username!="") {alert('Welcome again '+username+'!')}else { username=prompt('Please enter your name:',"") if (username!=null && username!="") { setCookie('username',username,365) } }}All together now:<html><head><script type="text/javascript">function getCookie(c_name){if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "=") if (c_start!=-1) { c_start=c_start + c_name.length+1 c_end=document.cookie.indexOf(";",c_start) if (c_end==-1) c_end=document.cookie.length return unescape(document.cookie.substring(c_start,c_end)) } }return ""}function setCookie(c_name,value,expiredays){var exdate=new Date()exdate.setDate(exdate.getDate()+expiredays)document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())}function checkCookie(){username=getCookie('username')if (username!=null && username!="") {alert('Welcome again '+username+'!')}else { username=prompt('Please enter your name:',"") if (username!=null && username!="") { setCookie('username',username,365) } }}</script></head><body onLoad="checkCookie()"></body></html>
:)
Link to comment
Share on other sites

SetCookieThe first question is easily answered, because GMT is the basis of the server more than likely.3.] If expiredays = null, then it also equals zero or false and/or not set (Hence why you run setCookie in the first place.)6.] After we figure out that there's no cookie set, we've got to set it to the appropriate time in GMT.

Link to comment
Share on other sites

The first question is easily answered, because GMT is the basis of the server more than likely.
That is not true. The servers timezone is based on where it is and how the admin set it up. Servers don't default to GMT.
Link to comment
Share on other sites

from that site u gave me above...

Cookies were originally invented by Netscape to give 'memory' to web servers and browsers. The HTTP protocol, which arranges for the transfer of web pages to your browser and browser requests for pages to servers, is state-less, which means that once the server has sent a page to a browser requesting it, it doesn't remember a thing about it. So if you come to the same web page a second, third, hundredth or millionth time, the server once again considers it the very first time you ever came there.This can be annoying in a number of ways. The server cannot remember if you identified yourself when you want to access protected pages, it cannot remember your user preferences, it cannot remember anything. As soon as personalization was invented, this became a major problem.
Cookies were invented to solve this problem. There are other ways to solve it, but cookies are easy to maintain and very versatile.whats the other method, just curios
Link to comment
Share on other sites

from that site u gave me above...Cookies were invented to solve this problem. There are other ways to solve it, but cookies are easy to maintain and very versatile.whats the other method, just curios
You can use cookie-less Sessions. This is where a unique identifier is encoded into the URL of the request so that the server can associate the visitor with that visitor's session. Depending on the implementation, the URLs can come out pretty ugly:e.g. http://www.mysite.com/3432ec7b66ffa2390af7c73d/index.html
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...