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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post

    Writing a simple cookie

    Hey guys, this is probbily a really simple script but I have searched the site and google but cant seen to find the answers im looking for.

    I have a flash intro on my site, and i want to have some javascript on the page so that it will set a cookie and next time the intro is accessed, it redirects to the index.php file. But i want the cookie to expire in a week.

    Can someone put me straight on where im going wrong as i have no clue what to do

    Thanks for your time,
    Anthony

    EDIT:

    I managed to find this script

    Code:
    function createCookie(name,value,days)
    {
    	if (days)
    	{
    		var date = new Date();
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
    	}
    	else var expires = "";
    	document.cookie = name+"="+value+expires+"; path=/";
    }
    
    function readCookie(name)
    {
    	var nameEQ = name + "=";
    	var ca = document.cookie.split(';');
    	for(var i=0;i < ca.length;i++)
    	{
    		var c = ca[i];
    		while (c.charAt(0)==' ') c = c.substring(1,c.length);
    		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    	}
    	return null;
    }
    but I dont think its finished. How do i tell it to redirect to index.php if the cookie is found etc. If anyone is willing, could they please finish this script?
    Last edited by Anthony2oo4; 01-26-2005 at 02:47 PM.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Please try the following



    <SCRIPT language=JavaScript>
    <!--
    cookie_name="poponce"
    expDays = 7; // number of days the cookie should last

    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 checkCount() {
    var count = get_cookie('poponce');
    if (count == null) {
    count=1;
    var exp = new Date();
    exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
    set_cookie('poponce', count, exp);

    dothis()
    }

    // add onload="checkCount()" to the opening BODY tag

    }

    function dothis(){
    window.location="index.php"
    }
    // -->
    </SCRIPT>

    Insert the onLoad event handler into your BODY tag.

    <BODY onload="checkCount()">


    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.

  • #3
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    Hey,

    Thanks for yout help, I tried that code, but it seemed to have the opisite approach. The first time i visited the page, it went to google. Then from there after, it stayed at the page.

    I uploaded it here.

    www.whereisit.org/hello.htm

  • #4
    Regular Coder
    Join Date
    Jan 2005
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Try this code:

    <html>
    <head>
    <title></title>
    <SCRIPT LANGUAGE="JavaScript">
    <!--

    cookie_name = ""; //enter Cookie name

    function doCookie() {
    gettimes();
    if(document.cookie)
    {index = document.cookie.indexOf(cookie_name);}
    else
    {index = -1;}

    var expires = "Wednesday, 02-Feb-2005 05:00:00 GMT" //set your expiration date

    if (index == -1)
    {
    document.cookie=cookie_name+"=1; expires=" + expires;
    }
    else
    {
    countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    if (countend == -1)
    {
    countend = document.cookie.length;
    }
    count = eval(document.cookie.substring(countbegin, countend)) + 1;
    document.cookie=cookie_name+"="+count+"; expires=" + expires;
    }
    }

    function gettimes()
    {
    if(document.cookie)
    {
    index = document.cookie.indexOf(cookie_name);
    if (index != -1)
    {
    countbegin = (document.cookie.indexOf("=", index) + 1);
    countend = document.cookie.indexOf(";", index);
    if (countend == -1)
    {
    countend = document.cookie.length;
    }
    count = document.cookie.substring(countbegin, countend);
    if (count >= 1) //change the 1 to the # of times before you are forwarded to index.php
    {
    redirect();
    return ("");
    }
    else
    {
    return ("");
    }
    }
    }

    return ("");
    }

    function redirect()
    {
    location.href="index.php";
    }

    //-->
    </SCRIPT>
    </head>

    <body onLoad="doCookie()">

    Flash Intro

    </body>
    </html>

    I did this today after I saw your question because I realized that I could use this script on a webpage I made.

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    My applogies, looks like I posted the wrong script, please try the following


    <HTML>
    <HEAD>
    <TITLE>Document Title</TITLE>

    <script language=javascript>
    <!--
    days=7
    cookie_name="redirect"

    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 count = get_cookie(cookie_name);

    if (count == null) {
    count=1
    var exp = new Date();
    exp.setTime(exp.getTime() + (days*24*60*60*1000));
    set_cookie(cookie_name,count, exp);
    }
    else{
    window.location="http://www.google.com"
    }

    }

    // -->
    </script>
    </HEAD>
    <BODY onload="chk_cookie()">
    Index

    </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.

  • #6
    Regular Coder
    Join Date
    Jul 2004
    Posts
    201
    Thanks
    8
    Thanked 1 Time in 1 Post
    Thank you both of you. Worked Great


  •  

    Posting Permissions

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