Solved

about cookie...

Posted on 2003-11-09
16
819 Views
Last Modified: 2011-10-03
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
Comment
Question by:qfren
  • 5
  • 4
  • 3
  • +3
16 Comments
 
LVL 3

Expert Comment

by:monvelasquez
Comment Utility
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
 
LVL 11

Assisted Solution

by:Zontar
Zontar earned 50 total points
Comment Utility
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
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 60 total points
Comment Utility
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
 
LVL 25

Expert Comment

by:devic
Comment Utility
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
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
And the difference is??? A different cookie script...
0
 
LVL 25

Expert Comment

by:devic
Comment Utility
hi mplungjan,

i didn't refresh the page, before posting :)
0
 
LVL 10

Expert Comment

by:Nushi
Comment Utility
hi there.

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

Nushi.

0
 
LVL 11

Expert Comment

by:Zontar
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Author Comment

by:qfren
Comment Utility
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
 
LVL 25

Accepted Solution

by:
devic earned 90 total points
Comment Utility
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
 
LVL 11

Expert Comment

by:Zontar
Comment Utility
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
 
LVL 8

Author Comment

by:qfren
Comment Utility
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
 
LVL 11

Expert Comment

by:Zontar
Comment Utility
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
 
LVL 8

Author Comment

by:qfren
Comment Utility
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
 
LVL 11

Expert Comment

by:Zontar
Comment Utility
> 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
 
LVL 8

Author Comment

by:qfren
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now