그저 그런 널널 한 얘기 들 . ....... : Cookies Make the Web Go ' Round 
 Cookies Make the Web Go ' Round IT 영문 자료 
 2008 / 03 / 06 13 : 34 
 engjjang . egloos . com / 480503 
 Cookies Make the Web Go ' Round 
 by Adam DuVander 29 Jun 2006 
 Here at Webmonkey , we get to tackle all the latest breakthroughs in web development technology . We play around with the newest bits of code and we tell you about them . Sometimes , the newest stuff isn ' t really so new . Take cookies , for example , the little bits of information that websites store on your computer . We ' ve been writing about them for over a decade . 
 Cookies let you perform basic identity - based tasks on the web , like logging in to your email account or online banking system . Cookies are essential for maintaining identities during online sessions . You want to make sure that transfer goes to the right off - shore account , after all . 
 Back in the 1990 s , Mark wrote an introduction to cookies and Len talked about what cookies can do for you . Really , cookies haven ' t changed that much since then . What has happened is that they are used a lot more . .. by almost every site out there . 
 Any site that requires a username and password needs a way to remember who you are . Your browser helps websites know who you are after you have logged in . Can you imagine having to enter your password on every page ? No fun . 
 Privacy concerns have kept some users from accepting cookies . Sometimes , problems arise from complex rules to accept cookies from this site , but not that site , or only in certain situations . If your site requires a cookie and the cookie isn ' t accepted by the browser , the user needs to know . Otherwise , you might leave them logging in repeatedly and never getting anywhere . When that happens , users usually bolt . And you don ' t want them to leave , because then who ' s going to fill up your bank account with money and enable you make those transfers to your off - shore accounts ? 
 The best thing to do is check whether the user is accepting cookies right before you actually need them , like on a login page . This way , you can give a nice , human error message that might even explain how to enable cookies . 
 Sometimes this pre - check is called " cookie sniffing , " but I think that sounds dirty . But for lack of a better term , that ' s what I ' ll call it . On the following pages , I ' ll show how to check for whether cookies are enabled in a few popular programming languages : JavaScript , PHP , and ASP . NET . 
 http : / / www . webmonkey . com / webmonkey / 06 / 26 / index 3 a . html 
 Page 2 — JavaScript cookie sniffer 
 There ' s good news for JavaScripters : Webmonkey has ready - made JavaScript cookie access code that will make this pretty easy . JavaScript works in every modern browser ( assuming the user hasn ' t disabled it , but that ' s another story ) and you don ' t need a special server to use it . 
 Once you download the Webmonkey code , I recommend putting it into its own file . I called mine WM _ cookies . js . Then , include it in the beginning of your HTML code , like so : 
 Next , simply check for cookies using this code : 
 if ( ! WM _ acceptsCookies ) 
 Put this anywhere after the body tag . If the visitor isn ' t able to use cookies ( for whatever reason ) , they will receive a nice message and a link to more information . Right now , that goes to a community site I setup to have non - tech - oriented information , but you might want to point to your own explanatory verbage . 
 Lastly , include the rest of your page . This will be seen by all visitors , whether or not they have cookies enabled : 
 < h 1 > The title of my page </ h 1 > 
 Hello and welcome to this page , blah blah . .. 
 You can see the whole thing in action here . My same site mentioned above also shows how to disable cookies . This will help you test your cookie sniffing solutions . 
 Page 3 — PHP cookie sniffer 
 As I discussed in the introduction , it is important to find out whether a user can use cookies before starting anything that requires their use . Since cookies are used to remember stuff from one page to another , we can ' t have page two thinking that page one did something it didn ' t — and couldn ' t — do . 
 This PHP function is used to create a new cookie . This one we named " check " and " ok " is its value . 
 Testing a cookie in PHP is also easy : 
 / / stuff to do if we find the cookie 
 Here we just make sure that the cookie we created above is really there . PHP uses the $_ COOKIE variable to access cookies . The cookie name ( we called it " check ") goes within the brackets . Then we are checking its value against our expected value ( " ok "). 
 But there ' s a catch ! In order to set a cookie , we need to make a round - trip to the server . PHP reads in the headers ( where the cookies live ) as soon as the page is loaded . Since we created the cookie after the page was loaded , we can ' t know whether it takes until the next time a page is loaded . Egads ! 
 Here ' s the game plan . The first time we load the page , we ' ll try to set a cookie . Easy enough . Then , we ' ll forward the user to the same page , only with a little something extra at the end of the URL ( called the " query string "). That way we ' ll know it ' s the second time the user has been to the page . 
 When we see that extra stuff in the query string , we try to read in the cookie . If the cookie is there , success . If not , we have to give the user a nice message and a link to more information . 
 Here ' s the code to include at the very top of any PHP page that will need to use cookies : 
 else / / this is the first page . Let ' s set a cookie and send the user to the second page 
 Then , you can start the real HTML page : 
 Immediately after the body tag comes the cookie error . If there was no error , it will be blank and not show up at all . 
 And lastly , the rest of your page : 
 Now let ' s move on to sniffing cookies in ASP . NET 
 Page 4 — ASP . NET cookie sniffer 
 Setting a cookie in ASP . NET is pretty simple . Within the Page _ Load section ( or any other server code ) , use these lines : 
 Dim c As New System . Web . HttpCookie ( " check ", " ok ") 
 The first line creates a new cookie named " check " and sets " ok " as its value . Then , we add it to the the collection of cookies that ASP . NET tracks . 
 Testing a cookie in ASP . NET is as easy as this If statement : 
 If ( Not Request . Cookies ( " check ") is Nothing ) Then 
 ' stuff to do if we find the cookie 
 Here , we just make sure that the cookie we created above is really there . ASP . NET uses the Request . Cookies collection to access cookies . The cookie name ( we called it " check ") goes within the parentheses . We are checking that it is Not Nothing . Double negatives are perfectly OK in ASP . NET . 
 Just like in PHP , there ' s a catch to this cookie business ! In order to set a cookie , we need to make a round - trip to the server . ASP . NET reads in the headers ( where the cookies live ) as soon as the page is loaded . Since we created the cookie after the page was loaded , we can ' t know whether it takes until the next time a page is loaded . 
 Our game plan here is the same as before . The first time we load the page , we ' ll try to set a cookie . Then , we ' ll forward the user to the same page , only with a little something extra at the end of the URL ( called the " query string "). That way we ' ll know it ' s the second time the user has been to the page . 
 Here ' s the code to include at the top of an ASP . NET page that will need to use cookies : 
 If ( Request . Cookies ( " check ") is Nothing ) Then 
 Response . Redirect ( Request . ServerVariables ( " SCRIPT _ NAME ") & sCheckCode ) 
 Immediately after the body tag comes the spot for the cookie error . I ' m using a Literal tag , which the Page _ Load code fills in with the error . If there was no error , it will be blank and not show up at all . 
 And that ' s that . A little cookie sniffing , plus a humane error message , makes a less frustrated user . Even better , you ' re helping everyone by explaining the importance of cookies , all the while moving more dough into that off - shore account . 
 Did you love this article ? Did you hate it ? Think you can do better ? Send us your Feedback . Feedback submitted here will be considered for publication on Webmonkey or Wired News , so if you don ' t want us to print your comments , please say so in your email . 
 다음 글 : PHP / MySQL Tutorial 
 이전 글 : Server - Side Scripting Shootout