PDA

View Full Version : Cookies



dog
12-14-2006, 02:26 PM
Hello everyone,

I don't really understand cookies and I'm not very experienced with javascript.

I'm trying to use cookies to determine whether a form gets shown of not.

I want the page to check for a cookie. If it doesn't find the cookie it shows a form.

When the form gets filled out correctly the cookie gets written.

I hope that makes sense.

I know how to javascript with CSS to show or hide the form with event triggers but bringing cookies into it confusses the whole thing.

Do cookies have names? I'm assuming they do, how do you give them names and then find them by their name again? Is this necessary?

Thanks to anyone that can help,

dog :D

PS I know I asked for this same help a while back but the script I ended up with wasn't doing what I wanted it to and I couldn't get any more support on that thread. :p

mwinter
12-14-2006, 10:15 PM
I don't really understand cookies ...

HTTP is stateless: a client sends a request and the server responds. As far as either is concerned, that's the end of things; each pair of requests and responses are independent. Obviously, this isn't always desired so there needs to be a way to associate one request/response with another. Passing extra information through a POST request body or the query string is one way, and cookies are another.

Generally the only extra information passed between client and server is a session identifier. Cookies shouldn't be used to store large amounts of data, nor anything sensitive. Instead, the server holds onto this and the session identifier links the client with that stored data.



I'm trying to use cookies to determine whether a form gets shown of not.

Fine.



I want the page to check for a cookie. If it doesn't find the cookie it shows a form.

When the form gets filled out correctly the cookie gets written.

If the visitor is only expected to fill out the form one, then it would be better not to send the form at all, rather than just hide it.

Have you considered how visitors that have cookies disabled will react to the document?

Mike

dog
12-14-2006, 10:28 PM
HTTP is stateless: Please excuse my ignorance but... what's HTTP exactly?:eek: ...and what does it mean to be stateless?:confused:


If the visitor is only expected to fill out the form once, then it would be better not to send the form at all, rather than just hide it. It may be. Hiding it is something I already know how to do but I'm open to acheivable suggestions.


Have you considered how visitors that have cookies disabled will react to the document?No I haven't. I've considered that if anyone goes deleting their cookies they'll be disappointed when the meet the form a second time.

mwinter
12-15-2006, 12:05 AM
Please excuse my ignorance but... what's HTTP exactly?

Hypertext Transfer Protocol. It's the network protocol used to transfer HTML documents and most other data over the Web. That's why the scheme name, http, occurs in many URIs, to indicate that HTTP should be used to retrieve the resource.



...and what does it mean to be stateless?

Without state? :D

When you log into a site that maintains information about where you are and what you're doing - such as ordering something - it isn't HTTP itself that manages this information. The client connects to the server, requests something, then disconnects[1] when it's received the response. Even if the server soon after receives another request from the same IP address, it can't be sure that it's the same client, what with dynamic IPs, NAT, and so forth. Therefore, it's necessary to introduce something else to store this information and associate it with the correct remote client.





If the visitor is only expected to fill out the form once, then it would be better not to send the form at all, rather than just hide it.

It may be. Hiding it is something I already know how to do but I'm open to acheivable suggestions.

You'd need a server-side language like PHP, ASP, and the like.



I've considered that if anyone goes deleting their cookies they'll be disappointed when the meet the form a second time.

It's not just a matter of deleting cookies. Cookies can be blocked from the outset by privacy settings.

Mike


[1] HTTP/1.1 adds persistent connections, but this is only to improve efficiency by allowing multiple requests and responses over the same connection. The connection is still dropped when there's nothing immediate left to communicate.

dog
12-15-2006, 12:41 AM
If the visitor is only expected to fill out the form once, then it would be better not to send the form at all, rather than just hide it.
It may be. Hiding it is something I already know how to do but I'm open to acheivable suggestions.
You'd need a server-side language like PHP, ASP, and the like.
I pick PHP! That's what I'm learning to send the form with.

What would you suggest I write? :D




I've considered that if anyone goes deleting their cookies they'll be disappointed when they meet the form a second time.
It's not just a matter of deleting cookies. Cookies can be blocked from the outset by privacy settings.

If people don't have cookies enabled (or they've deleted them since using the form) they could pass the form with a password which would be given on completion of the form.

As I don't have time to learn how to make accounts and passwords and deal with all the admin. involved in this I'll be cheeky [like a monkey] and give everyone the same password :p .

If they forget it they could use a simple form (that I have no idea how to write) that sends a reminder to the email address and name they supply.

I'll have a go at writing this and post my attempt in the PHP Forum later.

I'd still like to use the cookies idea for people that have them enabled.

To be honest I'm really hoping someone can offer me the code for this and help me understand how it works. And help me tweek it.

Is that asking too much? :rolleyes:

mwinter
12-15-2006, 04:50 PM
What would you suggest I write?



<?php
$cookieName = '...';

if (!isset($_COOKIE[$cookieName]) || ($_COOKIE[$cookieName] != '...')) {
?>
<form ...>
<!-- ... -->
</form>
<?php>
}
?>

That's an extremely simple approach.

The cookie should probably be renewed if it's found to be correct to prevent it expiring. Output buffering should be used to help obtain the length of the output and set the Content-Length header appropriately.

Mike

dog
12-15-2006, 05:39 PM
Hi Mike,

I'm affaid I don't understand even the extremely simple approach you've suggested.

I've managed to get the effect I'm looking for with a combination of CSS and javascript.

The PHP approach will have to wait for when the next project.

I need some help tweeking the javascript I've come up with but I think it's appropriate to start that in another thread.

Thanks for the help!