cookie voodoo

From: CHYRON (DSMITHHFX)25 Jul 2015 15:10
To: ALL1 of 10
I just published a web site that uses cookie vars to dynamically load content via js. It was working great on three different servers (including live) as of yesterday, this morning the live version menuing stopped working. Only difference between live, staging and development servers is the site is in a "public_html" root of the live server, and the live server is zeus (others are apache).

If I put a copy into a subdirectory on the live site, it works, cookies and all.

Any iDears?
From: Peter (BOUGHTONP)25 Jul 2015 15:42
To: CHYRON (DSMITHHFX) 2 of 10
> this morning the live version menuing stopped working.
> Only difference between live, staging and development servers is ....

1. Real Staging servers use the same (or closest possible) setup as Live.

2. Computers are automatons - if their behaviour changes it's because they were told to change. If the software doesn't differentiate between environments or yesterday then something else changed - so the poor server setup is not the only difference, and your first step is to identify which other differences exist.


> If I put a copy into a subdirectory on the live site, it works, cookies and all.

Check your version control history for what changes were made - sounds possible that somebody may have set/changed the path attribute of the cookies. Then again, you would have spotted that when you compared the actual cookies for the different environments to check for differences, so maybe it's something else.

EDITED: 25 Jul 2015 15:45 by BOUGHTONP
From: CHYRON (DSMITHHFX)25 Jul 2015 16:03
To: Peter (BOUGHTONP) 3 of 10
I did make some edits to the javascript yesterday unrelated to the cookies stuffs. I'll do a diff compare to previous version on monday.
From: CHYRON (DSMITHHFX)27 Jul 2015 12:53
To: ALL4 of 10
Problem solved by deletion of a single caricature.

Before:
Code: 
document.cookie.match(/^foliosec=[a-z]+/) + '';
After:
Code: 
document.cookie.match(/foliosec=[a-z]+/) + '';
From: 99% of gargoyles look like (MR_BASTARD)27 Jul 2015 16:50
To: Peter (BOUGHTONP) 5 of 10
G'wan, tell him the difference between 'caricature' and 'character', and let him squirm at his risible use of language. You know you want to.
From: CHYRON (DSMITHHFX)27 Jul 2015 19:17
To: 99% of gargoyles look like (MR_BASTARD) 6 of 10
Caricature: you. Character: Peter.

You're welcome.
From: Peter (BOUGHTONP)27 Jul 2015 19:44
To: CHYRON (DSMITHHFX) 7 of 10
Except now it'll also match any cookies with names ending in "portfoliosec".

If JS had a decent regex engine, you'd just use a lookbehind instead (?<=^|;) but it doesn't so that wont work.

Next best solution is probably splitting and looping:

FoliosecCookie = getCookie('foliosec');

function getCookie( Name )
{
	for ( CurCookie in document.cookie.split(';') )
	{
	    var KeyValue = CurCookie.split('=',2);
	    if ( KeyValue[0] == Name )
	       return KeyValue[1];
	}
	return '';
}

You may need to use decodeURIComponent on the return value.

And for anyone wondering why the browsers don't just provide a built-in getCookie method that accepts a name and returns the value, the answer is because browser developers are fucking stupid.

From: CHYRON (DSMITHHFX)27 Jul 2015 20:02
To: Peter (BOUGHTONP) 8 of 10
I could have tried
/; foliosec=[a-z]+;/
but I was too lazy to find out if semi-colons need to be escaped (as seems likely), didn't want to start faffing around with regex voodoo, and was tickled by the one-caricature fix (apologies to bastard for that).

The likelihood of another cookie that includes "foliosec" as part of its name in this site is remote.

I'm considering whether using cookies makes the whole thing a bit too fragile, and will try url-based variables instead, which is how e.g. angularjs does it.
EDITED: 27 Jul 2015 20:08 by DSMITHHFX
From: Peter (BOUGHTONP)27 Jul 2015 23:15
To: CHYRON (DSMITHHFX) 9 of 10
Semi-colon doesn't need escaping. Only these do: ^.$*+?\|()[]{}

And it really isn't voodoo; it's a very easy language to learn.

From: CHYRON (DSMITHHFX)29 Jul 2015 00:45
To: ALL10 of 10
Got the URL-based vars coded up without too much trouble (even learned the simple trick of rewriting the URL hash on the fly) and it seems considerably more responsive than the cookie vars. Probly make the patches live tomorrow.