Sessions

darkforcesjedi
07-24-2002, 01:47 PM
I want to make my web page so that when a user comes back after visiting before they get a welcome back message once, i.e. if they reload the page the message is no longer there. I don't want the message to come back unless the user closes his browser window then opens it again and returns to my site. (Sort of like Yahoo! Mail logins: when you log in and leave the site, you're still logged in when you come back unless you close the browser window). The following code postpones redisplaying the message until the session ends (20 minutes by default, I think). Is there a way to accomplish what I want?

if request.cookies("BeenHere") <> "" and Session("sessionstart") = "" then
response.write("<br><font color=red><b>Welcome back to my wonderful page!</b></font>")
else
Response.Cookies("beenhere") = "yes"
end if

Session("sessionstart") = "something"

Rezner
07-24-2002, 02:23 PM
Here's how I'd do it:<%
If Request.Cookies("BeenHere")<>"Yes" Then
Response.Cookies("BeenHere").Expires=DateAdd("y",Now,1)
Response.Cookies("BeenHere") = "Yes"

Response.write "You've never been here before"

Else
Response.write "You've been here before! Welcome back!"
End if
%>

darkforcesjedi
07-24-2002, 02:38 PM
There's a slight problem with that. All the pages on my site are generated from one ASP document, well two (the second is #included in the first). I'd really like not having persistent messages.

Rezner
07-24-2002, 02:46 PM
Well, then there is probably some type of string in the address that you can query to distinguish the "main" page from the rest of them.

Example:
if request.querystring("page") = 1 then
'Run Cookie check here
end if

darkforcesjedi
07-25-2002, 12:29 PM
Not really, because I want the message to show when the visitor enters from any page. There isn't really a 'main' page for the site. It's a photo album, so someone mught come through a link to "album.asp?image=funny.jpg" or "album.asp?image=weird.gif" and I want the message to show up once, no matter what image(s) they view.

Rezner
07-25-2002, 01:00 PM
Use a session cookie to store whether or not the message has been displayed:<%
Session.Timeout = 10 ' (default is 20)

If Session("MessageViewed") <> "Yes" Then

If Request.Cookies("BeenHere")<>"Yes" Then

Response.Cookies("BeenHere").Expires=DateAdd("y",Now,1)
Response.Cookies("BeenHere") = "Yes"

Response.write _
"You've never been here before"

Else

Response.write _
"You've been here before! Welcome back!"

End if

Session("MessageViewed") = "Yes"

End if
%>

darkforcesjedi
07-31-2002, 12:25 PM
That's pretty close to what I've been using, but it doesn't do exactly what I want.

Rezner
07-31-2002, 05:12 PM
Originally posted by darkforcesjedi
That's pretty close to what I've been using, but it doesn't do exactly what I want. Oh, well I guess you're going to have to improvise then.

darkforcesjedi
08-03-2002, 10:15 PM
I don't understand: how can sites like Yahoo! Mail know when a user has closed his browser? Something has to change when he exits. Is there a way I can make a cookie expire as soon as the browser is closed? Alternately, can I instead tell if a user has left my site, using JavaScript to process the destination URL and remove my cookie if the destination is not part of my site? By the way, thank you for all your help thus far.

Derek Stone
08-04-2002, 07:58 AM
Session cookies expire when the browser closes or after a preset duration of inactivity.

Rezner
08-05-2002, 08:50 AM
If you want to achieve the "Yahoo thing", then store a simple session cookie and set the session timeout = 1. As long as the user stays active in the site, they will be good to go. If they leave for more than 1 minute, or close their browser, then the session cookie will be destroyed and, when they return, you can do whatever you want (redisplay the message, etc).

darkforcesjedi
08-05-2002, 02:16 PM
That is what I thought. But for some reason it isn't always working right. Oh well, thanks for all the help.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum