Link to home
Start Free TrialLog in
Avatar of marcus_carey
marcus_carey

asked on

Using the set cookie header

I wrote the following code to test for cookie support.  My browser accepts cookies but this code does not detect the cookie it sets.  What is the correct method for checking if a browser accepts cookies?



int main(int argc, char *argv[])
{
     char *pszCookie;
     char *pszQueryString;
     char *szLocation = "http://yoursite/cgi-bin/cookie.exe?TEST";
     char reply[256];

      
     pszQueryString = getenv("QUERY_STRING");

     if(pszQueryString)
     {

          if(strncmp(pszQueryString,"TEST",4) == 0)
          {
                 pszCookie = getenv("HTTP_COOKIE");

                 if(pszCookie)
                 {
                      if(strcmp(pszCookie,"Cookie=test") == 0)
                      {
                            sprintf(reply,"%s","<HTML><TITLE>Good Cookie!</TITLE><BODY>Your browser supports "\
                                            "the Netscape HTTP Cookie Specification as set by including a Set-Cookie "\
                                            "HTTP Header!</BODY></HTML>");
                      }
                 }
                 else
                 {
                      sprintf(reply,"%s","<HTML><TITLE>Bad Cookie</TITLE><BODY>Sorry, Your browser "\
                                           "doesn't appear to support the cookie protocol. If you believe you have "\
                                           "gotten this message in error... Don't eat cookies</BODY></HTML>");
       }

                 printf("Content-Type: text/html\n\n");
                 printf("%s",reply);

         }

     }
     else
     {
          printf("HTTP/1.0 302 Moved Temporarily\r\n");
          printf("Location: %s\n\n",szLocation);
          printf("Set-Cookie: Cookie=Test\n\n");
     }


     return(0);
}
ASKER CERTIFIED SOLUTION
Avatar of CoolATIGuy
CoolATIGuy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marcus_carey
marcus_carey

ASKER

This, http://asp.programmershelp.co.uk/chkcookies.php, example works but I guess I worded the question wrong.  I already know that IE supports cookies.  The idea is to test rather cookie support is enabled by setting a cookie value then redirecting to another page to see if the browser saved it.  


<%
'declare our variables
Dim blnCookies , objBrowser
'create an instance of browsertype object
Set objBrowser = Server.CreateObject("MSWC.BrowserType")
'set blnCookies to either true or false
blnCookies = objBrowser.cookies
'if true the display this message
If blnCookies = True Then
Response.Write "Cookies are supported in this browser"
'if false display this message
Else
Response.Write "Cookies are not supported in this browser"
End If
'destroy the object
Set objBrowser = nothing
%>