...

View Full Version : cookies



itsonlybarney
11-12-2004, 05:32 AM
can someone please help me to create a login that places a cookie in the users computer and then verifies that the cookie exists so that they can see the page and if it is not present they are redirected to the login page. also wat code would i need to remove the cookie when the person hits the logout button/link??

any help would be appreciated!! :)

BrightNail
11-13-2004, 02:00 AM
there are TONS of cookie code out there..

try this link.

http://www.webreference.com/js/column8/functions.html

Mr J
11-13-2004, 06:39 PM
See if the following helps you out


<HTML>
<HEAD>
<TITLE>Document Title</TITLE>
<script language=javascript>
<!--
cookie_name="cookie_name"

function get_cookie (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 get_cookie_val (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function get_cookie_val(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function set_cookie (name, value,expires) {
var argv = set_cookie.arguments;
var argc = set_cookie.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" : "");
}

function delete_cookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = get_cookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function chk_cookie() {
var cookie_count = get_cookie(cookie_name);

if (cookie_count == 1) {

//location="page.htm"
alert("The cookie exists")

}
else{

alert("The cookie does not exist")

}

}

function create_cookie() {
var cookie_count = get_cookie(cookie_name);
if (cookie_count == null) {
cookie_count=1
var exp = new Date();
exp.setTime(exp.getTime() + (1*24*60*60*1000));
set_cookie(cookie_name,cookie_count, exp);

alert("The cookie has been created")

}

}

// -->
</script>


</HEAD>
<BODY>

<a href="#null" onclick="chk_cookie()">Check for Cookie</a>
<p><a class="lined" href="#null" onclick="create_cookie()">Create Cookie</a></p>
<p><a class="lined" href="#null" onclick="delete_cookie('cookie_name')">Delete Cookie</a></p>
</BODY>
</HTML>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum