Click to See Complete Forum and Search --> : Simple Cookie
bstanic
10-02-2003, 10:51 AM
Has anyone a simple, no frills cookie I can place on my site without notifying the user of their name, how many times they visited, etc. Just need it to track how many times someone is accessing my site via Webalizer Stats. This will be my first cookie so some helpful hints appreciated.
aoeguy
10-02-2003, 10:55 AM
lol, one min after my post
Can I save the refferrer in a file when the user visits??
And how can I save how mayn times visted and there IP??
sciguyryan
10-02-2003, 10:59 AM
cookies are always specific and, are writen for a specific perpous but, i will attempt to write one foryou give us 20 mins...
sciguyryan
10-02-2003, 11:11 AM
this is a basic example:
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
function setcookie()
{
var cookadte = new Date();
cokdate.steTime(cookdate.getTime() + 1000*60*30);
document.cookie = "cookiea=My c Cookie.;expires=" + cookdate.toGMTString();
}
function getcookie()
{
var cookstring = new string(document.cookie);
var cookname = "cookiea=";
var startpos = cookstring.indexOf(cookname);
if (startpos!= -1)
{
document.form1.cookvalue.value = cook.substring (startpos + cookname.length);
}
else
{
document.form1.cookvalue = "ookie not found!";
}
<form name="form1">
<input type="button" value="Create a cookie! name="setcook" onclick="setcookie();">
<input type="button" value="Get a cookie! name="getcookie" onclick="getcookie();">
</form>
aoeguy
10-02-2003, 11:14 AM
What about visits?
bstanic
10-02-2003, 11:44 AM
Ryan, thanks for code. Few things not sure about; Can I change the name of the cookie? What is the expiry date? What does the cookie actually set? Can I change any other variables? What info does it give me when viewing Webalizer stats. Appols for all the questions.
Regards
aoeguy
10-02-2003, 11:48 AM
Thanks too, but that cookie messed up my page. Stopped it from using the CSS code correct.
aoeguy
10-02-2003, 11:59 AM
I found this code on a site for visits, dont know how to change it
<SCRIPT>
expireDate = new Date
expireDate.setMonth(expireDate.getMonth()+6)
jcount = eval(cookieVal("jaafarCounter"))
jcount++
document.cookie = "jaafarCounter="+jcount+";expires=" + expireDate.toGMTString()
function cookieVal(cookieName) {
thisCookie = document.cookie.split("; ")
for (i=0; i<thisCookie.length; i++){
if (cookieName == thisCookie[i].split("=")[0]){
return thisCookie[i].split("=")[1]
}
}
return 0
}
function page_counter(){
for (i=0;i<(3-jcount.toString().length);i++)
document.write('<span>0</span>')
for (y=0;y<(jcount.toString().length);y++)
document.write('<span>'+jcount.toString().charAt(y)+'</span>')
}
</SCRIPT>
aoeguy
10-02-2003, 12:08 PM
only problem is that it resets itself when viewed on a different page :(
Suppose i should change the cookie name everywhere
EDIT:
It works! View it at http://www.bandits-clan.tk
sciguyryan
10-02-2003, 02:00 PM
in ansewr to your question, yes you can chnge the cookie name here:
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
function setcookie()
{
var cookadte = new Date();
cookdate.steTime(cookdate.getTime() + 1000*60*30);
document.cookie = "cookiea=My Cookie.;expires=" + cookdate.toGMTString();
}
function getcookie()
{
var cookstring = new string(document.cookie);
var cookname = "cookiea=";
var startpos = cookstring.indexOf(cookname);
if (startpos!= -1)
{
document.form1.cookvalue.value = cook.substring (startpos + cookname.length);
}
else
{
document.form1.cookvalue = "Cookie not found!";
}
<form name="form1">
<input type="button" value="Create a cookie! name="setcook" onclick="setcookie();">
<input type="button" value="Get a cookie! name="getcookie" onclick="getcookie();">
</form>
1) you can change the name see red
2) an expirey date is the date at which the cookie should be renued, in this case 1800000 miliseconds + the current time from 1970 in miliseconds think!(JS default) to change this se blue
3) a cookie actually sets anything you give it! you can give it values, usernames which are recaled next time you goto the site- they are usually used to keep important information too.
4) yes, i only set those variables as a result, you can add, remove or modify them.
5) it does not give you any information but, it does give the browser the information it wants to be added to the page - it is machine code and, can only be read by the browser and, each coding method is different.
if you want a good script see atached. please note this is a complicated one and, it will do what you want.
bstanic
10-02-2003, 07:25 PM
Thanks for your help. Will give it a try.
Regards
sciguyryan
10-03-2003, 07:38 AM
its ok no problem and, glad to help.