Forum Moderators: DixonJones

Message Too Old, No Replies

Setting a cookie when the user leaves the site

         

Donboy

11:09 pm on Apr 8, 2003 (gmt 0)

10+ Year Member



When the user arrives at my site, I want to read a cookie and have it display content based on the time and date when they last visited the site.

Therefore, I need to set the cookie when they leave the site... not before. If the cookie is set when they first arrive, it will show them content based on the current time, which is not what I want. I want to be able to read the cookie and use the time and date to display content during their entire visit. Then when they exit the site, the time and date will be written again for later use when they return.

I had thought of setting the cookie using Javascript's onUnload event, but I'm trying to stick to setting and reading the cookies using Perl's CGI.pm module. I had thought of doing an onUnload and calling a URL to my perl script that would set the cookie, but that isn't working out very well.

I guess I'm looking for alternative solutions that I haven't considered yet. Maybe I could set more than one cookie? I don't know. Any input would be welcome.

graywolf

1:36 am on Apr 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would set more than one cookie and name it something like Last_accesed with the date/time. It will be the last time the page was served, not when they left the site, don't know how precise you need to be.

msr986

1:41 am on Apr 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can do both when they enter the site. First, read the cookie so you have the old time and date, then set the cookie with the current date and time. You don't have to wait until they leave.

Donboy

3:34 am on Apr 9, 2003 (gmt 0)

10+ Year Member



Right, but when they are browsing my site, each page of the site will be referring to the cookie information that is stored on their PC to determine what content is displayed. So if I write the cookie back to their computer with a new date, they will not be seeing any of the new stuff, as the date will be only a few minutes old instead of being days old as it should be.

For example... a user comes to my site and looks at page #1. The page opens and calls the cookie info to determine what page content gets displayed. The user clicks on page #2 and the cookie is read again and uses the date to determine what content is displayed.

If I write the cookie with a new date when they arrive at page #1.... when the user visits page #2 (and subsequent pages) will not show the new content, since no new content has been added in the few minutes (or seconds) since the cookie was last written.

See what I mean?

graywolf

10:35 am on Apr 10, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Then use two cookies in conjunction. Last_session and last_page. If a new user comes to your site with no cookies write them both with the same time. On each page if the date from last_session is today leave it alone just rewrite the last_page cookie. If the date on last_session is prior to today you know they are coming back. You can use the time from last_page to check if they were browsing when the server date rolled over (ie 11:59 pm day 1 to 12:00 am day 2).

The only way you would miss someone is if they come on day 1 at any time and then leave, and come back on day 2 early in the morning. You could solve this with a 3rd cookie but seems way more trouble than its worth to me. The above should cover 99% of the conditions.