[web] CGI "Set-Cookie" is big meanie! :(

Started by
3 comments, last by Kylotan 18 years, 10 months ago
I'm having a great deal of difficulty reading and writing cookies using CGI. My code to write the cookie is this...
printf( "%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10);
printf("Set-cookie: 487657 GM Tyrael                     \n"
       "<html><head><style type=\"text/css\">");
My code to read the cookie is this...
void sCheckLoggedOn()
{
	char *bufCookie;
	int intNumber;
	char chrName[30];

	bufCookie = getenv("HTTP_COOKIE");
	if(bufCookie == NULL){bytLoggedOn = 0; return;}
	else
	{
		/* Extract the account number and password. */
		sscanf(bufCookie,"%d",&intNumber);
		memcpy(chrName,&bufCookie[7], 30);
		chrName[29] = 0;

		/* Lets view the data. */
		printf("%s\n", chrName);
	}
}
The output is really confusing. Sometimes it outputs stuff, and some times it doesn't. Inaddition, it outputs the "Set-cookie" to the screen, I don't want it too! :( Whats happening?
Advertisement
Quote:
printf("Set-cookie: 487657 GM Tyrael                     \n"       "<html><head><style type=\"text/css\">");

I'm not sure if this is the issue, but you need two newlines after the last header. What webserver are you running it on?
Free Mac Mini (I know, I'm a tool)
IIS 5.1.

I added "\n\n" to the end of the HTTP header, and it didn't make a difference. =/
I found the problem.

printf("Set-Cookie: Apples=Oranges\n");printf( "%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10);


The "Set-Cookie" comes before the text/html declaration.

Thanks anyways. :D
By the way, there's no point using printf with 10 and 13, just use \r and \n .

This topic is closed to new replies.

Advertisement