Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Making Cookies - Page 5

January 8, 2001

Working with Cookies, ASP and Voting!

We all hear a lot about cookies, especially from Internet privacy pundits, but what exactly are they? I do not want to go into too much detail in this article but let's get the basics down.

Cookies are small text files that are generated by a Web page and stored on the client's hard drive. These cookies can then be accessed by the server the next time a visitor returns to the site. They can store personal information, payment information, remember the fields you filled out in a form, what ads you clicked on, or just about anything you as the designer want.

Please note that cookies are not stored on the server so they cannot be personally identifiable unless you (as a visitor) provide personal information on the Web site. For whatever reason, there is this huge fear among users that they will be personally identified and watched based on their behavior. It is indeed possible to track behavior, but to personally identify is impossible if you never tell the site who you are. So ... I just told you this, now tell your users. We would not have half of the problems with privacy that we have if Web sites would be open and honest about what they do with visitor information. Just tell the people that you are going to use a cookie to make sure that they do not vote twice and provide them with a way of obtaining more information about cookies and how you intend to respect their privacy.

Seems I found that soapbox again. Oh well, let's make these cookies happen! We are going to use a cookie to ensure that the user (who will not be personally identifiable) does not screw up the results by casting multiple votes for the same poll. Achieving our goal will require two separate code modifications; first to create the cookie on the client's hard drive and second to check for a cookie on the client's hard drive and prevent action.

Cookies are easy to set up, so let's do that first. Take a look at the following code:

Response.Cookies("PollCookie").Expires = Now() + 365
Response.Cookies("PollCookie").Domain = "mydomain.com"
Response.Cookies("PollCookie")("PollID") = PollID

The code shown above sets up a cookie called PollCookie, using the ASP Cookies Collection. The first line sets up the expiration date of the cookie to today's date plus 365 days - or one year. The second line tells the cookie what domain name or IP, etc. for which it is being stored. Finally, Response.Cookies("PollCookie")("PollID") = PollID sets an attribute of the cookie, named PollID equal to the ID of the poll displayed on the Web page. Easy, no?

Where the code actually goes is also pretty easy, it should go right after the code that updates the database, but still inside the if statement. Take a look:

If Request.QueryString("isSubmitted") = "yes" then
strSQL = "UPDATE tbl_Responses " & _
    "SET VoteCount = VoteCount + 1 " & _
    "WHERE PollOptionID= " & _
    Request.QueryString("pollopt") & ";"

'Update the count for the PollOption
'yet another way to run a query against a database
'with ASP Conn.Execute
Conn.Execute strSQL

Response.Cookies("PollCookie").Expires = Now() + 365
Response.Cookies("PollCookie").Domain = "mydomain.com"
Response.Cookies("PollCookie")("PollID") = PollID

End if

Finally, let's add a simple conditional statement to make sure that some sneaky user does not vote twice.

If Request.QueryString("isSubmitted") = "yes" then
 strSQL = "UPDATE tbl_Responses " & _
     "SET VoteCount = VoteCount + 1 " & _
     "WHERE PollOptionID= " & _
     Request.QueryString("pollopt") & ";"

 'Update the count for the PollOption
 'yet another way to run a query against a database
 'with ASP Conn.Execute

 If Int(Request.Cookies("PollCookie")("PollID"))
 	<> PollID then
   Conn.Execute strSQL
   Response.Cookies("PollCookie").Expires = Now() + 365
   Response.Cookies("PollCookie").Domain = "mydomain.com"
   Response.Cookies("PollCookie")("PollID") = PollID
 End if

End if
[Lines 10 and 11 (the first two in red) are one line. They have been split for formatting purposes.]

Again, not very hard. Before executing the SQL Update statement and creating the cookie, the code uses a simple if conditional statement to see if the cookie exists with the same PollID already (notice how the value in the cookie is converted to an interger using the Int() function? That is so you can compare the variables without a type conflict). If it does, then nothing happens, the code is not executed. If they are not equal then the vote is accepted and a cookie is created. If you want to yell at users who try to vote more then once, just add an else statement with some sort of ErrorMsg and display that in the HTML.

A Functional Polling System - Page 4
Poll Your Visitors with ASP
Tallying the Results - Page 6


Up to => Home / Authoring / ASP / Polls




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers