Cookie Expiration Workaround for 2013r2

Here’s a method you can put in your Session class in your project which will work around the cookie expiration issue in 2013r2. I’m providing it free of charge here for use in your own projects. However, I ask that this posting or Web Essentials be the exclusive sources for distributing this code at this time. I will be adding it to the next release of Studio Stable Web Essentials. If you’re already a Web Essentials customer, thank you again for purchasing. If you’re not, and this gets you back on track until Xojo can fix the problem, you can encourage me to share more critical fixes like this in the future by becoming a customer. No obligation though, it’s up to you :-).

http://www.studiostable.com/webessentials

This workaround is provided without warranty of any kind. Works well enough for me where I’ve verified the 2013r2 problem and deployed this workaround. To use it… Instead of calling Session.Cookies.Set, call Session.SetCookie_2013r2 instead.

[code]Sub SetCookie_2013r2(Name as String, Value As String, Expiration As Date = nil, Domain As String = “”, Path As String = “”)
#if RBVersion = 2013.02

dim script as string
dim cookieValue, etc as string
dim now as Date
dim secs as Double

// document.cookie = 'name=value; Max-Age=12345; Domain=example.com; Path=/special/abc/def/'
// document.cookie = '[name]=' + escape('[value]') + '; Max-Age=12345; Domain=example.com; Path=/special/abc/def/'

value = value.ReplaceAll("'", "\\'")
cookieValue = "escape('" + value + "')"
etc = ""

if Expiration <> nil then
  // cookieValue = cookieValue + "; expires=" + Expiration.UTCString
  
  now = new Date
  secs = Expiration.TotalSeconds - now.TotalSeconds
  if secs > 0 then
    etc = etc + "; Max-Age=" + str(secs, "0")
  end if
end if

if Domain <> "" then
  etc = etc + "; domain=" + Domain
end if

if Path <> "" then
  etc = etc + "; path=" + Path
end if

script = "document.cookie = '" + name + "=' + " + cookieValue
if etc <> "" then
  script = script + "+ '" + etc + "'"
end if
script = script + ";"

// MsgBox(script)
ExecuteJavaScript(script)

#else

self.Cookies.Set(name, value, Expiration, Domain, Path)

#endif
End Sub
[/code]

Thank you Brad. I love Web Essentials and your support has been phenomenal.

Thanks you very much!

No prob. The next Web Essentials is going to contain something even better, localStorage and sessionStorage controls. I’ve found these to be much easier to develop with than cookies. Coming soon…

Sounds really interesting! Keep up the good work :wink: