Jump to content

need some one to explain what this is doing please


shadiadiph

Recommended Posts

sorry i know nothing about javascript i have built a php curl script my problem is this my login curl is fine but this javascript kills it everytime

 

function GetCookie(name) {
var arg=name+"=", alen=arg.length, clen=document.cookie.length, i=0;
while (i<clen) {
	var j=i+alen;
	if (document.cookie.substring(i, j)==arg) return getCookieVal (j);
	i=document.cookie.indexOf(" ", i)+1;
	if (i==0) break;
}
return null;
}
[/cookie]

can anyone explain to me exactly what this is looking for in the cookie to validate it? thanks

Link to comment
Share on other sites

I don't really understand the whole logic behind that while loop. It's so confusing. Also, I don't know what your getCookieVal() function does because you didn't post it. But there is an easier way to do all that. Try this instead:

 

function getCookieValue (cookiename) {
     if (document.cookie.length == 0) return null;
     var startp = document.cookie.indexOf(cookiename + "=");
     if (startp == -1) return null;
     startp += cookiename.length + 1;
     var endp = document.cookie.indexOf(";", startp);
     if (endp == -1) endp = document.cookie.length;
     return document.cookie.substring(startp, endp);
}

 

That should do what you want. I made some calls you may find redundant like if (startp == -1) return null; but that's just for speed and to tell the function to stop processing at that point. Also, if you want, you can unescape the returned string, which I do recommend, but it's completely up to you. So:

 

function getCookieValue (cookiename) {
     if (document.cookie.length == 0) return null;
     var startp = document.cookie.indexOf(cookiename + "=");
     if (startp == -1) return null;
     startp += cookiename.length + 1;
     var endp = document.cookie.indexOf(";", startp);
     if (endp == -1) endp = document.cookie.length;
     return unescape(document.cookie.substring(startp, endp));
}

 

Stepping through the code.

So say your cookie value is: ken2k7=blah;shadiadiph=blah2;

And let's say you want to look up the value of shadiadiph.

 

1. Checks if the cookie value is empty. If so, return null.

2. Get the first position of the string "shadiadiph=". I count 12.

3. Check if the value on step 2 is -1 (meaning the string is not found). If so, return null because the cookie doesn't exist.

4. Set the starting point to be the starting point + the length of the name + 1. The +1 is the equal sign. So in the cookie, the startp value should be sitting on top of the letter b in "blah2".

5. Get the first position of the string ";" starting at startp. If I didn't pass startp, it will return the position of the first ";", which is the one after "blah" instead of "blah2".

6. If endp is -1, meaning it's not found, then that's okay, we can just set it to be the last position of the cookie, which is document.cookie.length.

7. Return the string between startp and endp. This should be the value of the cookie. If you don't know what unescape do, Google it.

 

Hope this helps!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.