• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 857
  • Last Modified:

about cookie...

hi:


i set a cookie in my ASP script..
Response.Cookies("UserName") = sUserName

Now,

Look at this javacript:


---------------------this script will auto refresh the page every 10 seconds..
<SCRIPT  LANGUAGE=javascript >

window.setTimeout("RefreshMe();", 10000);

 function RefreshMe()
 {
      var sURL;
      
      sURL="";
      sURL="test.asp";
      window.location=sURL;
            
 }
</script>
----------------

SO CAN I CHANGE TO the following??

<SCRIPT  LANGUAGE=javascript >

window.setTimeout("RefreshMe();", 10000);

 function RefreshMe()
 {
      var sURL;
      sURL="";            //in this part..i want to code if that cookie(UserName)=null
      sURL="test.asp";     //then the sURL="relogin.asp"
      window.location=sURL;
            
 }

</script>

--
so is that possible? How? or any others suggestion?



q





0
qfren
Asked:
qfren
  • 5
  • 4
  • 3
  • +3
3 Solutions
 
monvelasquezCommented:
try the code below...

--[CODE]-----------------------------------------------------------------------
function RefreshMe()
 {
     var sURL;
     <%If  IsNull(Request.Cookies("UserName") ) Then %>
     sURL="relogin.asp";
     <%Else%>
     sURL="test.asp";    
     <%End If%>
     window.location=sURL;
         
 }
--[/CODE]----------------------------------------------------------------------

or if you want a pure javascript solution, see this page..
   http://www.webreference.com/js/column8/property.html
0
 
ZontarCommented:
You can read cookies with JavaScript, but it's a bit of a pain. It's much simpler to do on the server.

Put this in the very top of your page(s):
<%
  If Request.Cookies.Key("UserName") = "" Then
    Response.Redirect "relogin.asp"
    Response.End
  End If
%>
0
 
Michel PlungjanIT ExpertCommented:
If you have an external cookie script on your webserver then reading cookies with javascript is no longer a pain
Just have
<script src="cookie.js" language="JavaScript" type="text/javascript"></script>
in the pages that needs to read the cookie
and use
function RefreshMe() {
  window.location= (getCookie('UserName'))?"test.asp":"relogin.asp"
 }

Here is the source of cookie.js

//----------------------------------
today      = new Date();
expirydate = new Date(today.getTime() + (30 * 86400000)); // 1 month from now

function getCookie(Name) {
  var search = Name + "="
  if (document.cookie.length > 0) {
     offset = document.cookie.indexOf(search)
     if (offset != -1) {
        offset += search.length
        end = document.cookie.indexOf(";", offset)
        if (end == -1) end = document.cookie.length
        return unescape(document.cookie.substring(offset, end))
     }
  }
}

function setCookie (name,value,expires,path,theDomain,secure) {
  var theCookie = name + "=" + escape (value) +
  ((expires)    ? "; expires=" + expires.toGMTString() : "") +
  ((path)       ? "; path="    + path   : "") +
  ((theDomain)  ? "; domain="  + theDomain : "") +
  ((secure)     ? "; secure"            : "");
  document.cookie = theCookie;
}

function delCookie(name,path,domain) {
  if (getCookie(name)) document.cookie = name + "=" +
     ((path)   ? ";path="   + path   : "") +
     ((domain) ? ";domain=" + domain : "") +
     ";expires=Thu, 01-Jan-70 00:00:01 GMT";
}
//--------------------------------
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
devicCommented:
hi qfren,

i don't understand why the user must wait 10 sec.?

Zontar's solution is the best in your situation.


anyway here is js solution:
====================================
<script>
window.setTimeout("RefreshMe();", 10000);

function RefreshMe()
{
    var sURL;
      if(getCookie("UserName"))
      {
            sURL="test.asp";
      }
    else
      {
            sURL="relogin.asp";
      }
    window.location=sURL;
         
}
function getCookie(name)
{
  var dc = document.cookie;var prefix = name + "=";var begin = dc.indexOf("; " + prefix);
  if (begin == -1){begin = dc.indexOf(prefix);if (begin != 0){return null;}}else{begin += 2;}
  var end = document.cookie.indexOf(";", begin);if(end == -1)end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
</script>
0
 
Michel PlungjanIT ExpertCommented:
And the difference is??? A different cookie script...
0
 
devicCommented:
hi mplungjan,

i didn't refresh the page, before posting :)
0
 
NushiCommented:
hi there.

more information can be found here.
http://hotwired.lycos.com/webmonkey/98/29/index1a_page7.html?tw=programming

Nushi.

0
 
ZontarCommented:
I think most of us who've been doing JS for very long have written or picked up a cookie script somewhere. IMO a Cookie class is one feature that's long been missing from clientside JS, and it surprises me that none of the browser vendors have ever made any kind of an effeort in this direciton.

Besides, I figured since ASP set the cookie in the first place, might as well use it to get it again. ;-)
0
 
qfrenAuthor Commented:
hi:

sorry , maybe i didnt make my Q clearly...

what i want to do is actually,
for that javascript i want to code it so that it will refresh every 10 or 15 sec , and when it refresh, it will check that cookie,UserName(where i get from Response.Cookies("UserName")..). If that cookie is null,then it will redirect to relogin.asp,else if that cookie got thing..then nothing happen...


q
0
 
devicCommented:
qfren,

you don't need to refresh to check cookie.
this script will check every 10 sec cookie, if cookie equal null, then will locate to relogin.asp
=================================================
<script>
function CheckCookie()
{
   var sURL;
    if(!getCookie("UserName"))
    {
         window.location="relogin.asp";
    }
   window.setTimeout("CheckCookie();", 10000);
}
function getCookie(name)
{
 var dc = document.cookie;var prefix = name + "=";var begin = dc.indexOf("; " + prefix);
 if (begin == -1){begin = dc.indexOf(prefix);if (begin != 0){return null;}}else{begin += 2;}
 var end = document.cookie.indexOf(";", begin);if(end == -1)end = dc.length;
 return unescape(dc.substring(begin + prefix.length, end));
}
CheckCookie();
</script>

@ Zontar
>>>it surprises me that none of the browser vendors have ever made any kind of an effeort in this direciton
yea, you are right ;)
0
 
ZontarCommented:
Why do you want to check the same serverside cookie repeatedly from the clientside on the same page?

I find this puzzling since, once the page has loaded, you can't set cookies from the server.

Cookies are set using HTTP headers... You have to send headers before sending content... Yes, I'm aware of output buffering, but still... Once the page has loaded, you can't get or set cookies on the server, only on the client... Please explain what your purpose is, if there's something here that I'm missing, I'd like to know what it is, thanks.
0
 
qfrenAuthor Commented:
devic:
i will check it out the code and let u know...

Zontar :
in a login.asp page, i set the cookie....Response.Cookies("UserName") = xxxxxxxx
so....now...in other page,let say abc.asp..i wan to check every 10 or 15 seconds to make sure the cookie not null.

i want to do this because..maybe the user open 2 window ......one is the let say hello.asp..another is abc.asp..(both in same system)..so in hello.asp if the user click Logout (which i will set the Cookies("UserName") =null ). Now on abc.asp ,because it will check the value of cookie every 10 seconds...so when it found that the cookie =null(because already logout in hello.asp), it will redirect to relogin.asp to ask use to relogin again so that he/she can continue that abc.asp...


Let me know if u all still not understand my Q...sorry for the confusing...


thanks...


q
0
 
ZontarCommented:
Why not just check the cookie whenever a new page loads? That's the way it's usually done. If you unset a cookie in window A, then the user attempts to navigate elsewhere on your site using Window B, the cookie will still be unset, it own't magickally return to life or anything like that. What you want to do just seems unnecessary to me, that's all.
0
 
qfrenAuthor Commented:
Zontar:
glad to read ur suggestion....

>>Why not just check the cookie whenever a new page loads
yes..this is what i did before this..

>>If you unset a cookie in window A, then the user attempts to navigate elsewhere on your site using Window B, the cookie will still be unset

my problems is: both window A and B is openning...and user logout at A while B still opening....so in  my situation..B still openning and still can use that userID .
That y i now need to add the coding using javasript so that it will check every 10 seconds....


q


0
 
ZontarCommented:
> B still openning and still can use that userID .

For what? As soon the user tries to do anything meaningful, they'll have to load a page and then they'll be logged out again, so whatever they tried to do basically won't happen, right?
0
 
qfrenAuthor Commented:
hi:
Zontar :
but in my case the user still can view or explorer something in the pages...i also dont know why......i will try to figure out....

Anyway, devic 's comments works in my situation....

But some others experts also providing others userfull suggestion...
so i will increase my points and split the points to them...

Thansk again..

q
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 5
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now