Log in

View Full Version : cookies



lainlives
10-25-2006, 05:30 PM
can anyone help me to do cookies/content of cookies??

codeexploiter
10-26-2006, 04:46 AM
You can't create or retrieve a cookie using HTML only. You need to use either a client-side scripting tool like JavaScript or a server-side tool like ASP, PHP, etc.

If you specify more details about your objective we might be able to help you

lainlives
10-27-2006, 04:17 PM
i just need a cookie that counts how many times they have been to my site
i should just post in the javascript for this proplem right

Twey
10-27-2006, 07:31 PM
No, now you've started the thread here we'll carry on. ddadmin will probably move it in a bit. Cookies in Javascript are very simple: they're stored in document.cookie.
<script type="text/javascript">
var t;
if(!isNaN(t = parseInt(document.cookie)))
t = 0;
document.write("You've visited my site " + t + " times.");
</script>

codeexploiter
10-28-2006, 05:26 AM
But you can't rely fully on a cookie that work in client-side script as there might be users who have turned off their Javascript in their browsers.

To overcome that you can develop a cookie using a server side script like ASP or PHP.

Most importantly you can't keep valuable information in a cookie as the user can remove the cookie at any time they wish.

If you specify your server side script tool i'll be able to provide the code for using the cookie then

djr33
10-28-2006, 08:59 AM
Remember, users frequently clear their cookies, so it won't be accurate for long periods of time.

Also, you may want a delay on each "visit" so that you don't count refreshes, viewing multiple pages, etc. as a new visit.


You could also look into PHP and store in a database by IP address, but since IPs change periodically and some users even share an IP (at different times), that wouldn't be perfectly accurate either.


It's a hard thing to do.
The only close-to-foolproof way would be to use a login system, and store based on that username.


But.... to keep things simple (though not perfect), I'd say a javascript method (like Twey's above) is probably the best answer.