Bay Six Software Forum Index Bay Six Software
Beyond the Basics
 
 FAQFAQ   SearchSearch   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[DEMO] RB Cookies by Javascript, revised

 
Post new topic   Reply to topic    Bay Six Software Forum Index -> Internet Programming
View previous topic :: View next topic  
Author Message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: Sep 12th, 2008, 7:32pm    Post subject: [DEMO] RB Cookies by Javascript, revised Reply with quote

Code:
' Cookies by Javascript
' By Brent D. Thorn, 2/2008
' Revised 9/2008 for RB 1.01
' Revised 1/2009 to simplify the code

[start]
    Cls
    Call enableCookies

    HTML "<h1>Please log in.</h1>"

    Print "Username:"
    TextBox #user, ""
    Print

    Print "Password:"
    PasswordBox #pass, ""
    Print

    Checkbox #reme, "Remember me", 0
    Print
    Button #login, "Log in", [logIn]

   HTML "
<script type=""text/javascript"">
<!--
  if (getCookie('reme')) {
    var user = document.getElementById('#user');
    var pass = document.getElementById('#pass');
    var reme = document.getElementById('#reme');

    user.value = getCookie('user');
    pass.value = getCookie('pass');
    reme.checked = true;
  }
//-->
</script>"

    Wait

[logIn]
    user$ = #user Contents$()
    pass$ = #pass Contents$()
    reme = #reme Value()
 
    'TODO: Authenticate user.
 
    Cls
    Call enableCookies
 
    HTML "<h1>Welcome, "+user$+"!</h1>"
 
    If reme Then
        ' Save info in a cookie.
        HTML "
<script type=""text/javascript"">
<!--
  var path = ''; // root
  var expire = days(30);
  setCookie('reme', true, path, expire);
  setCookie('user', '"+user$+"', path, expire);
  setCookie('pass', '"+pass$+"', path, expire);
//-->
</script>"
 
    Else
        ' Delete info from cookie.
        HTML "
<script type=""text/javascript"">
<!--
  var path = ''; // root
  delCookie('reme', path);
  delCookie('user', path);
  delCookie('pass', path);
//-->
</script>"
    End If
 
    Link #logout, "Log out", [logOut]
 
    Wait
 
[logOut]
    Cls
 
    HTML "<h1>Come back soon!</h1>"
    Link #again, "Log in again", [start]
 
    Wait
 
Sub enableCookies
    HTML "
<script type=""text/javascript"">
<!--
function getCookie(name) {
  var dc = document.cookie;
  var cname = name + ""="";
  var clen = dc.length;
  var cbegin = 0;
  while (cbegin < clen) {
    var vbegin = cbegin + cname.length;
    if (dc.substring(cbegin, vbegin) == cname) {
      var vend = dc.indexOf("";"", vbegin);
      if (vend == -1) vend = clen;
      return unescape(dc.substring(vbegin, vend));
    }
    cbegin = dc.indexOf("" "", cbegin) + 1;
    if (cbegin == 0) break;
  }
  return null;
}
function setCookie(name, value, path, expires) {
  if (!expires) expires = new Date();
  document.cookie = name+""=""+escape(value)+""; " +_
                    "expires=""+expires.toGMTString()+""; " +_
                    "path=/""+path;
}
function delCookie(name, path) {
  document.cookie = name+""=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/""+path;
}
function days(num) {
  var d = new Date();
  d.setTime(d.getTime()+(86400000*num));
  return d;
}
//-->
</script>"
 
End Sub

_________________
Brent


Last edited by Brent on Jan 18th, 2009, 7:17pm; edited 2 times in total
Back to top
View user's profile Send private message Send e-mail
STPendl
Full Member


Joined: 20 Aug 2007
Posts: 161
Location: Austria

PostPosted: Sep 13th, 2008, 7:20am    Post subject: Re: [DEMO] RB Cookies by Javascript, revised Reply with quote

To delete the cookie it should be sufficient to use
Code:
function delCookie(name, path) {
  expires = days(-1);
  document.cookie = name+""=; expires=""+expires.toGMTString()+""; path=/""+path;
}

_________________
Stefan

Any code I post can be freely used, just give credit.
Back to top
View user's profile Send private message
Alyce
Full Member


Joined: 04 Jul 2005
Posts: 91

PostPosted: Sep 15th, 2008, 1:45pm    Post subject: Re: [DEMO] RB Cookies by Javascript, revised Reply with quote

Wow. Cool. Thanks for this one.
_________________
- Alyce
Back to top
View user's profile Send private message Visit poster's website
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: Jan 7th, 2009, 9:34pm    Post subject: Re: [DEMO] RB Cookies by Javascript, revised Reply with quote

I just updated the code, eliminating the table on the login screen in order to simplify the demo and make it much clearer. If no one finds any problems, I'll update the wikispaces page in a few days.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Bay Six Software Forum Index -> Internet Programming All times are GMT
Page 1 of 1
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum



Lo-Fi Version
Powered by phpBB © 2001, 2005 phpBB Group