PDA

View Full Version : JS cookies: need help with redirecting



deacon
Dec 7th, 2002, 04:24 AM
I am using a CGI form to collect info from users and in turn, they are able to download useful information from a 'Thank You' page.

My CGI form works fine but after sending users to the 'Thank You' page, I would like to use a cookie to allow users to bypass the form on future visits and go directly to a third page which simply allows users to download the desired info.

I found a script that sets a cookie & redirects users if they have visited the page but if I use it on the form page it will set the cookie & allow users to bypass the page even if they did not complete the form properly.

I am not a JS writer but I think I need to set the cookie on the 'thank you' page and on the CGI form page have a "get cookie" & "redirect" function to the third page.

Long story, hope it is clear.
Thanks for any help.

BrightNail
Dec 7th, 2002, 06:45 PM
why don't you set the cookie on the 'thankyou' page, and do checks on the form page?

you don't have to do the 'set and redirect' on the same page..

just set the cookie on thank you page......and do the check on the 'form' page OR do a check on the page prior to the form..check there...have the link to the form call a function..if the cookie exists redirect to the 'nonform' page...

deacon
Dec 7th, 2002, 08:09 PM
The problem is, I don't know how to separate out the two functions from the existing code I have without trashing it.

I guess I simply need the code to set a cookie and another to check & redirect.

When I tried separating the functions and placing in pages I got errors since I don't know enough about JS to get everything right.

Below is the code I downloaded that sets a cookie, checks for the cookie & redirects if it is found (to allow second time users to bypass a specific page) but does not weed out users who are trying to bypass a visit to a required page.
Thanks for helping.

<script>
<!--

// page to go to if cookie exists
go_to = "thanks.html";

// number of days cookie lives for
num_days = 60;
function ged(noDays){
var today = new Date();
var expr = new Date(today.getTime() + noDays*24*60*60*1000);
return expr.toGMTString();
}

function readCookie(cookieName){
var start = document.cookie.indexOf(cookieName);
if (start == -1){
document.cookie = "seenit=yes; expires=" + ged(num_days);
} else {
window.location = go_to;
}
}

readCookie("seenit");
// -->
</script>