Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: cookies

  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Sydney, Australia
    Posts
    281
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question cookies

    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!!

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    349
    Thanks
    2
    Thanked 0 Times in 0 Posts
    there are TONS of cookie code out there..

    try this link.

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

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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>
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •