View Full Version : howto: save on client comp w/out cookies.
dazappa
01-15-2006, 03:03 AM
Due to a very strange problem I am having with cookies, I'm trying to find a new way to save. What I am doing: making an rpg type game. All stats are stored in a cookie. I can set and retrieve the cookie fine, but if I completely close the browser then reopen it, the cookie returns null. This will cause quite a problem for people. So, if anybody knows another way to save and read files in JS please post. Preferribly if someone is going to post a way to do this it must be able to read multiple values for ie strength, gold, exp, etc. Also saving on client is better and I don't know if it's even possible to save on server.
I have had this problem on 3 different machines, 2 different OSes and 3 different browser, firefox on win2k, IE on win2k, and IE on mac. I have also tried other peoples cookie set and read programs and they had the same problem.
Philip M
01-15-2006, 09:26 AM
You need a server-side language for this. Any data stored in a cookie can be erased or manipulated.
brothercake
01-15-2006, 02:04 PM
Sounds to me like the cookie you're setting is only sessional, and so it's being deleted as soon as the browser is closed. But that can be fixed - what does your cookie code look like?
dazappa
01-16-2006, 01:34 AM
Ok, before I post the cookie code(because it is extremely long, maybe I will include it as an attachment) I added another argument to my SetCookie function and all seemed working. But- is there a limit in IE to how long a cookie can be, or how big it can be? I added a check every time I access the cookie that reads everything in the cookie buffer. I noticed that for some reason parts of the cookie was getting deleted and returning errors and null. So I think maybe IE restricts filesize. But everything works completely in firefox. Nothing is returning null in firefox, but my goal is to have everybody including many browsers, to work with my JS. (And yes i figured out my cookies were sessional thanks) So should I create more cookies instead of 1 large cookie? I think doing so would allow it to work on other browsers like IE.
Philip M
01-16-2006, 08:04 AM
The maximum length of a cookie is 4096 bytes (4Kb), and as far as I know this applies regardless of the browser. But I don't think that you can create more than one cookie per domain at a time.
brothercake
01-16-2006, 02:15 PM
You can create up to 20 cookies, containing up to 4K of data (in total, not each)
dazappa
01-16-2006, 02:37 PM
Ok thanks. Well I don't think cookies will work for me then, considering I will need to store a lot of data. Is there another way to save in js then?
Pyth007
01-16-2006, 03:36 PM
A better option if you need to store a lot of data would be to use a server-side language. PHP, for example, has many functions built-in that interact with different database management systems, like MySQL. The info can be stored and retrieved from a MySQL DB using PHP. The info can then be "passed" to js variables like:
var charStrength=<?= $charStrength ?>;
Another option would be to use AJAX and store the data in XML files. I'm not as familiar with this method, however, so I don't know how easily it is to update the info. stored on the XML file...
dazappa
01-16-2006, 03:56 PM
Well I can't use php because I will be using a free webhost like geocities, which does not allow saving, and if it did I still have a Very limited amount of space. Where does AJAX save to? client or server?
Pyth007
01-16-2006, 07:47 PM
Server... it interacts with server-side languages...
It's very difficult to save anything to the client due to security reasons; otherwise nefarious people could save trojans and viruses onto your computer without your knowledge! So except for cookies, you don't have much of an option.
One other posibility would be to use "super variables" This is where you store a lot of information as a number. For example, let's assume the character attributes are in the range 0-9. If a character has 6 different attribute, then the number 462739 might describe the character. Strength = 4, dex = 6, con = 2 etc. This could then be passed in a smaller space inside a cookie, at the cost of increased processing time to parse the super variables.
You may also have to resort to using hashes and dictionaries. What I mean is that if the PC is fighting an NPC, you may not need to pass on all of the info about the NPC, but rather just an index. If you passed m=1, for example, you may know that monster #1 points to a goblin. All of the other info (eg the goblin's strength, hit points, experience, treasure, etc.) would all be stored as a part of the js instead of being passed as part of the cookie.
One last trick to storing a lot of data in a small amount of space is to use higher number bases. Even though our decimal system only uses the digits 0-9 to represent the different numbers, higher based numbers can incorporate letters in addition to numbers to increase the range that one place-value can represent. For example, hexadecimal can store 0 through 15 in only one character using 0-9 plus the letters A-F. Since you'd be parsing the number yourself, you could even make higher-based numbers than that (eg a dodecimal system would be 0-9 plus the letters A-J where J=20; which would allow the character attributes to be in the more "classic" D&D range of 3-18!).
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.