Go Back   BWHacks > Development > Web Development

Web Development Website design and programming, content development. It's all here.

Reply
 
LinkBack Thread Tools

Old 11-29-2006, 01:38 AM   #1 (permalink)
Crystatic

Deviant
 
Join Date: Aug 2006
Posts: 44
Crystatic is on a distinguished road
Default Javascript cookies



I do not understand this script, please explain the following if possible:

1. Set exdate's date to be the value exdate already pocessed, plus expire days. Is this correct? Where is expiredays defined? (Yes, I see the (expiredays==null) thing in there, is that it?)

2. What does this do? What does escape do?

3.If the webpage's cookie's length is greater than 0. Is this correct?

4.What does this do? I've never seen IndexOf before. Why is the (C_name + "=") thrown in?

5.If c_start is not equal to 1. Is this correct? Where is C_start defined?

6. c_start equals whatever value it already has, plus the length of C_name +1. Is this correct?

7.What does this do?

8. Isn't this defined twice?

9.What does this do? What is unescape?

10. Return what? Nothing?

11. The variable username equals the function getCookie(). Is the parameter the string "username" or the variable username again?

12. What does this do?
Crystatic 15 0FF11|\|3   Reply With Quote
Advertisement
 
Advertisement
Advertisement Sponsored links


Old 11-29-2006, 02:30 AM   #2 (permalink)
SC_Modder
Loading javascript...
Senior Member
Moderator

Inquisitor
 
SC_Modder's Avatar
 
Join Date: Nov 2004
Posts: 4,523
SC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond repute
Send a message via AIM to SC_Modder
Default

First, answer this:

Why in the hell are you using javascript to handle cookies?
__________________
AaronOpfer.com - My music
SC_Modder 15 0FF11|\|3   Reply With Quote

Old 11-29-2006, 03:47 AM   #3 (permalink)
kds
MENOS EL OSO
Senior Member
Moderator

Saint
 
kds's Avatar
 
Join Date: Sep 2004
Location: South suburbs of Chicago, Illinois
Posts: 6,289
kds has a reputation beyond reputekds has a reputation beyond reputekds has a reputation beyond reputekds has a reputation beyond reputekds has a reputation beyond reputekds has a reputation beyond reputekds has a reputation beyond reputekds has a reputation beyond reputekds has a reputation beyond reputekds has a reputation beyond reputekds has a reputation beyond repute
Default

Yeah, cookies in PHP are SO much easier to understand...
__________________
GET THE **** OUT OF MY SIGNATURE
kds 15 0FF11|\|3   Reply With Quote

Old 11-29-2006, 06:03 AM   #4 (permalink)
YonderKnight
Senior Member
Gold Member

Evangelist
 
YonderKnight's Avatar
 
Join Date: Sep 2004
Location: USA, California
Posts: 1,392
YonderKnight is a name known to allYonderKnight is a name known to allYonderKnight is a name known to allYonderKnight is a name known to all
Send a message via AIM to YonderKnight
Default

Ok, I have some research to do for a paper so I'm answering all these questions to distract me =p. But I'd take SCM's and KDS's advice, use PHP for cookie handling. Not only is it easier, but it's more secure also.

Quote:
Originally Posted by Crystatic
1. Set exdate's date to be the value exdate already pocessed, plus expire days. Is this correct? Where is expiredays defined? (Yes, I see the (expiredays==null) thing in there, is that it?)
That's correct. Expiredays is a parameter of the function "SetCookie." Look at the first line: "function setCookie(c_name,value,expiredays)"
This is basically defining a function with those 3 parameters, a user will call it using something like "setCookie('somename','poop',20)". This will set c_name, value, and expiredays to those values accordingly.

Quote:
Originally Posted by Crystatic
2. What does this do? What does escape do?
Escape(value) replaces every character except letters with their %xx code equivilants. You've probably seen this a lot in URL's, for example "www.something.com/some%20gay%20page.htm". %20 is the hex encoding for a space.

Cookies need to be in some format like "cookieName=cookieValue;cookieName2=cookieValue2.. ..etc"
The first line of code you asked about defines the cookie to be something like
c_name=valueThatIsEscaped;expires=somegaydate

Quote:
Originally Posted by Crystatic
3.If the webpage's cookie's length is greater than 0. Is this correct?
Correct

Quote:
Originally Posted by Crystatic
4.What does this do? I've never seen IndexOf before. Why is the (C_name + "=") thrown in?
IndexOf basically searches a string for another string and returns where it found the first location of the string you wanted to search for. That line basically finds where "c_name=" is located.

For example:
var string="poopy";
stuff = string.indexOf("y");

This will return 4, because the letter y first occurs as the 4th letter of the string.

Quote:
Originally Posted by Crystatic
5.If c_start is not equal to 1. Is this correct? Where is C_start defined?
Almost. It checks if C_start is not equal to -1, not 1. C_start is defined in the previous line. It's equal to indexOf(c_name+"="). So basically, if indexOf looked through the document cookie and couldn't find the name the user specified in c_name, c_start will be -1.

This line basically means if the cookie was found, then continue.

Quote:
Originally Posted by Crystatic
6. c_start equals whatever value it already has, plus the length of C_name +1. Is this correct?
Yep. The new position that c_start contains is the position where the value of the cookie actually starts.

Quote:
Originally Posted by Crystatic
7.What does this do?
This finds the end of the cookie data by searching for a ";" (which is used to specify the end of the cookie). c_start is thrown in there to tell indexOf to start searching from the beginning of c_start, so you won't find the end of a cookie value that actually came before the one you want.

Quote:
Originally Posted by Crystatic
8. Isn't this defined twice?
No. This line checks if c_end is equal to -1. There is a MAJOR difference between
something=1; < this line sets something equal to 1.
and
something==1; < this line checks if something is equal to 1.

Quote:
Originally Posted by Crystatic
9.What does this do? What is unescape?
Ok, if you remember when we actually set the cookie, we had to use escape(value). This replaces all the spaces and other characters and stuff with things like %20. Now to actually read it, we'll have to unescape it, which will convert it back into regular characters.

Substr basically "cuts" out everything between c_start and c_end. This is the actual value of the cookie.

This line returns that new value. So if the user calls
asdf = getCookie("poop")
asdf will now be equal to that value that the cookie holds.

Quote:
Originally Posted by Crystatic
10. Return what? Nothing?
Yes, if you look way back up to the if() statement, if the cookie search didn't find anything, the function will return nothing. If it did, it'll return the actual value of the cookie.

Quote:
Originally Posted by Crystatic
11. The variable username equals the function getCookie(). Is the parameter the string "username" or the variable username again?
The parameter sent to getCookie is the string "username". The variable username will hold the value that the cookie holds under "username".

Quote:
Originally Posted by Crystatic
12. What does this do?
This sets the cookie "username" to be equal to the value in the variable username. And it sets the cookie to expire in 365 more days (a year).


Hope that helps. You didn't actually need to know all of that to set/get cookies. You really only had to know how to call the function that they provided you (the last part is just an example). The actual functions may seem confusing, but most of this stuff is just string manipulation (just searching through strings and cutting stuff out).
YonderKnight 15 0FF11|\|3   Reply With Quote

Old 11-29-2006, 07:59 PM   #5 (permalink)
Crystatic

Deviant
 
Join Date: Aug 2006
Posts: 44
Crystatic is on a distinguished road
Default

Quote:
Originally Posted by SC_Modder View Post
First, answer this:

Why in the hell are you using javascript to handle cookies?
I was reading a tutorial for javascript, and cookies were the next lesson.


So, can javascript do anything that PHP can't?
Crystatic 15 0FF11|\|3   Reply With Quote

Old 11-29-2006, 08:42 PM   #6 (permalink)
lpxxfaintxx
I hate Steve Jobs.
Senior Member
Gold Member

Enlightened
 
Join Date: Jun 2005
Location: Fullerton, CA
Posts: 3,132
lpxxfaintxx has much to be proud oflpxxfaintxx has much to be proud oflpxxfaintxx has much to be proud oflpxxfaintxx has much to be proud oflpxxfaintxx has much to be proud oflpxxfaintxx has much to be proud of
Send a message via AIM to lpxxfaintxx Send a message via MSN to lpxxfaintxx Send a message via Skype™ to lpxxfaintxx
Default

Javascript can do loads of stuff that PHP can't... but you can use javascript WITH php.
__________________
lpxxfaintxx 15 0FF11|\|3   Reply With Quote

Old 11-30-2006, 01:57 AM   #7 (permalink)
Ganondorf
Premium Brawler Account
Senior Member

Enlightened
 
Ganondorf's Avatar
 
Join Date: Feb 2005
Posts: 2,575
Ganondorf has a brilliant futureGanondorf has a brilliant futureGanondorf has a brilliant futureGanondorf has a brilliant futureGanondorf has a brilliant futureGanondorf has a brilliant futureGanondorf has a brilliant future
Send a message via AIM to Ganondorf Send a message via Yahoo to Ganondorf Send a message via Skype™ to Ganondorf
Default

Quote:
Originally Posted by lpxxfaintxx View Post
Javascript can do loads of stuff that PHP can't... but you can use javascript WITH php.
And vice versa.
__________________
Ganondorf 15 0FF11|\|3   Reply With Quote

Old 11-30-2006, 03:24 AM   #8 (permalink)
SC_Modder
Loading javascript...
Senior Member
Moderator

Inquisitor
 
SC_Modder's Avatar
 
Join Date: Nov 2004
Posts: 4,523
SC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond reputeSC_Modder has a reputation beyond repute
Send a message via AIM to SC_Modder
Default

There are a lot of differences between PHP and javascript that they are barely comparable. Yeah, sure, they are both interpreted languages and they both have C style programming, but their purposes are completely different.
__________________
AaronOpfer.com - My music
SC_Modder 15 0FF11|\|3   Reply With Quote

Old 12-07-2006, 12:21 AM   #9 (permalink)
Crystatic

Deviant
 
Join Date: Aug 2006
Posts: 44
Crystatic is on a distinguished road
Default

[Bump]

What if the place i'm working with is not PHP enabled? Is there something I could embed into an Html page that would be just as active with the server as PHP is?
Crystatic 15 0FF11|\|3   Reply With Quote

Old 12-09-2006, 05:13 PM   #10 (permalink)
Ganondorf
Premium Brawler Account
Senior Member

Enlightened
 
Ganondorf's Avatar
 
Join Date: Feb 2005
Posts: 2,575
Ganondorf has a brilliant futureGanondorf has a brilliant futureGanondorf has a brilliant futureGanondorf has a brilliant futureGanondorf has a brilliant futureGanondorf has a brilliant futureGanondorf has a brilliant future
Send a message via AIM to Ganondorf Send a message via Yahoo to Ganondorf Send a message via Skype™ to Ganondorf
Default

You can install PHP manually if your allowed to.
__________________
Ganondorf 15 0FF11|\|3   Reply With Quote
Sponsored links
Advertisement
 
Advertisement
Advertisement

Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:15 PM.


vBulletin style developed by Transverse Styles

Powered by vBulletin Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright © 2004-2008 BWHacksAd Management by RedTyger