Click to See Complete Forum and Search --> : Redirect with cookie (help)


BlazedRyan
04-18-2003, 08:05 PM
Ok, I checked many sites looking for this open source code but I came up with nothing. I was hoping maybe someone else can have some better luck or maybe know the code themselves... Here is an example of the code I am looking for...

A users goes to the homepage http://www.loserlabeled.com (http://www.loserlabeled.com/index.html), if they have already viewed the intro and the page itself it redirects them to http://www.loserlabeled.com/index.shtml, if it hasnt they remain on the homepage viewing the intro (htt://www.loserlabeled.com/index.html)

DrDaMour
04-18-2003, 08:29 PM
get adn set cookie functions are pretty self explanitory, and they are appeneded at the bottom. The 3rd variable of set cookie is the time in seconds that the cookie should last

What you do is

on the main index first get the cookie

var before = getCookie("beenhere");

if (before != null){
window.location.href = "url to redirect to"
}
else {
setcookie("beenhere","yes",100000);
}



<!-- Hide from non-jscript browsers
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}

BlazedRyan
04-18-2003, 08:32 PM
where would I add this? in the body or under the head?

DrDaMour
04-18-2003, 08:46 PM
inbetween <script> tags, reallly doens't matter, although i guess the header would be more appropriate. yo know head and body don't really count for anythign right?

BlazedRyan
04-18-2003, 09:02 PM
It works up to the part when I refreshed to test if it would redirect me and it didnt... showed a blank page, this is what I added under the <head> tag.

<script>
var before = getCookie("beenhere");

if (before != null){
window.location.href = "http://www.loserlabeled.com/index.shtml"
}
else {
setcookie("beenhere","yes",100000);
}



<!-- Hide from non-jscript browsers
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
</script>

DrDaMour
04-19-2003, 01:14 AM
first your punctuation is wrong it's GetCookie not getCookie, adn SetCookie, not setCookie, second, function definitinos have to go BEFORE their calls. Finally i kind of messed up on describing what the third parameter is, it's seconds after a specific date, so you had to get that and add more seconds.
anyways, just cut past this code, should be fine


<script>


<!-- Hide from non-jscript browsers
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {

var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}


function SetCookie (name, value,expires) {

var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;

document.cookie = name + "=" + escape (value) + "; expires=" + expires.toGMTString() + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
alert(before);
}



var before = GetCookie("beenhere");

if (before != null){
window.location.href = "http://www.loserlabeled.com/index.shtml"
}
else {
var expdate = new Date()
expdate.setTime (expdate.getTime() + 60*(24 * 60 * 60 * 1000)) // dead in 2 months

SetCookie("beenhere","yes",expdate);

}

</script>

i attatched as file too

BlazedRyan
04-19-2003, 09:18 AM
Thanks for the help man!!!

BlazedRyan
04-19-2003, 09:23 AM
Ok, i spoke before i tried it... The page just freezes and doesnt show the intro or nothing... I added it to under the body tag, and i tried it under the head tag, didnt work.

DrDaMour
04-19-2003, 10:20 AM
well post a link to your page, because that worked fine for me.