View Full Version : Cookies - The Random 14?
techno_race
04-06-2007, 09:16 PM
I need to know how to generate a user number. For example, when a user visits a certain page on my site, they see a 14- or 10-digit (depending on the page:p) number with any sequence of the following 9 characters: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. I would prefer random against incrementing (for example, 7291862947, 5629857392... instad of 0000000001, 0000000002...). I could do this, but here comes the catch.
If a user visits the page, they see the number. But then if the user visits the page again 784 days later, it has the same number. Note that 784 days is just an example, it would also have the same number 4, 334 or 2,896 days later. And, a different user has a different number.
If anyone knows how to do this, please tell me right away. ;) ;) ;)
You can't, past the time the browser keeps cookies. If the browser keeps cookies forever, that's fine: just generate it and store it in a cookie. Otherwise, however, there's no way to store it forever short of a user account system and server-side code.
techno_race
04-06-2007, 09:39 PM
OK, I suppose: to keep the cookie the longest possible regarding the configuration.
Edit: Yay! This is my 100th post!
As I said, just generate it and store it in a cookie, and don't set an expiry date.
techno_race
04-10-2007, 11:09 PM
How do I make a cookie?
In Javascript, all cookie data is stored in a string, document.cookie. You can use it just like any other string, except that it persists across page visits.
boxxertrumps
04-11-2007, 01:17 AM
Can you create/read/destroy a JS cookie with php?
(and vice versa?)
thetestingsite
04-11-2007, 01:20 AM
To my understanding, yes. I have done this in several scripts (while I was just playing around). Due to the fact that the cookie gets stored in the browser (using both PHP and JS), both can read/edit/delete these cookies (as long as you have the cookie name.
Hope this helps.
techno_race
08-23-2007, 03:12 AM
I know this thread is 4 months old, but I still haven't been able to make a cookie... :p
Sorry, mods. :)
Cookies are slightly weird I think. Theres some special format. Its like
name=value;expiredate=aGMTstring;path=generaly/;
something like that. You have to read and write though. Google it. That'll give you stuff.
techno_race
08-23-2007, 05:31 AM
var core = new Date();
var num = core.getTime();
document.cookie = num;
document.write(document.cookie);
Would that work?
tech_support
08-23-2007, 07:07 AM
http://www.quirksmode.org/js/cookies.html
I made some Cookie code. I haven't tested it in IE, and I'm not sure how well it works, but it might help.
var Cookie = function Cookie (type,name,value,expireDate,path) {
if (type == "get") {
this.getCookieByName (name);
}
if (type == "set") {
this.setCookie (name,value,expireDate,path);
}
}
Cookie.prototype.name = null;
Cookie.prototype.value = null;
Cookie.prototype.expireDate = null;
Cookie.prototype.path = null;
Cookie.prototype.storeCookie = function storeCookie () {
document.cookie = this.name + "=" + this.value + "; expires=" + this.expireDate + "; path=" + this.path + "; ";
}
Cookie.prototype.setCookie = function setCookie (name,value,expireDate,path) {
if (name != null) {
this.name = name;
}
if (value != null) {
this.value = value;
}
if (typeof expireDate == "string") {
this.expireDate = expireDate;
}
if (typeof expireDate == "date") {
this.expireDate = expireDate.toUTCString ();
}
if (path != null) {
this.path = path;
}
}
Cookie.prototype.getCookieByName = function getCookieByName (name) {
var nvPairs = document.cookie.split ("; ");
for (var i = 0; i < nvPairs.length; i = i + 1) {
var seped = nvPairs [i].split ("=");
if (seped [0] == name) {
this.name = seped [0];
this.value = seped [1];
}
}
}
Cookie.prototype.getCookieByValue = function getCookieByValue (value) {
var toReturn = new Array ();
var nvPairs = document.cookie.split ("; ");
for (var i = 0; i < nvPairs.length; i = i + 1) {
var seped = nvPairs [i].split ("=");
if (seped [1] == value) {
toReturn [toReturn.length] = new Array ();
toReturn [toReturn.length] [0] = seped [0];
toReturn [toReturn.length] [1] = seped [1];
}
}
return toReturn;
}
Cookie.prototype.deleteCookie = function deleteCookie (name,path) {
var theDate = new Date ();
theDate.setFullYear (theDate.getYear - 1);
setCookie (name,"",theDate,path);
storeCookie ();
setCookie (null,null,null,null);
}
Also, can someone evaluate it?
Edit: Sorry, my delete doesn't work yet.
techno_race
08-23-2007, 03:43 PM
I haven't tested it in IE
Once I had a website say "Sorry, you're using Internet Explorer..."
:p
jscheuer1
08-23-2007, 04:12 PM
http://www.quirksmode.org/js/cookies.html
That's very good cookie code.
Can someone help me make my delete work? Also, every thing works in IE (except delete). Or at least I'm pretty sure.
jscheuer1
08-24-2007, 06:49 AM
All you have to do is to set the cookie with an expiration date in the past. All that other stuff (null, null,null, etc.) is messing it up.
How is it messing it up? I have to do storeCookie for it to actually put the cookie in document.cookie. I just want to clear the variables.
jscheuer1
08-24-2007, 03:14 PM
How is it messing it up? I have to do storeCookie for it to actually put the cookie in document.cookie. I just want to clear the variables.
Maybe it isn't then. I thought you said that it was. In any case, the way to erase a cookie is to set its expiration date to the past, period. Once you do that, the browser erases it. Anything that you do immediately after that has the potential of creating a new cookie, and, if it is done soon enough, perhaps even of 'resurrecting' the old cookie by confusing the browser.
Also, when working with cookies, it can be tricky. Perhaps your erase code is working OK, it's just that a new cookie is getting set when you refresh the page.
Thanks. I'm not sure. I think it has to do with the set cookie and the nulls. because when i tried it a different way it worked.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.