You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Lukas Ruf <ru...@rawip.org> on 2003/01/02 02:41:31 UTC

Re: [users@httpd] Identifying a session

* Rich Bowen <rb...@rcbowen.com> [2002-12-28 17:27]:

> On Sat, 28 Dec 2002, Lukas Ruf wrote:
>
> > > A cookie is just a means of setting a variable in a persistent
> > > manner.  You send a Set-Cookie header, and you receive it back
> > > again the next time that the client visits. You use them as you
> > > would any other configuration variable. Most CGI libraries,
> > > for various languages, have some means of setting and reading
> > > cookies in a simplified manner. What language are you using?
> >
> > C and perl
>
> OK, in C, I don't know, having never done CGI in C. But in Perl,
> there are functions in CGI.pm to set and retrieve cookies. See the
> documentation for CGI.pm (perldoc CGI) for more details here.
>

maybe the following text is commodity knowledge -- however, I figured
out what to do for cookie-programming.  Thanks to your hints!

Searching the web, I found:
<http://wp.netscape.com/newsref/std/cookie_spec.html> which explains
the cookies fairly well!

However, what cost me another couple of hours figuring out is the
"header".  After loads of trials -- it is really trivial, if you know
:-)

My Test program.

-- snip
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv, char **envp)
{
  int i = 0;
  char *cookie = 0; /* NULL required unistd.h if I was not mistaken */
  /* Standard Header as to be sent by the cgi script */
  fprintf(stdout, "Content-Type: text/html\n");
  fprintf(stdout, "Set-Cookie: NAME=NAME_VALUE;\n");
  fprintf(stdout, "\n");  /* this line ends the header! */
  fprintf(stdout, 
    "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML Level 3//EN//3.0\"> \n");
  fprintf(stdout, "<HTML>");  
  fprintf(stdout, "<HEAD>");  
  fprintf(stdout, "<TITLE>");  
  fprintf(stdout, "A Sample Cookie-Application");  
  fprintf(stdout, "</TITLE>");  
  fprintf(stdout, "</HEAD>\n");  
  fprintf(stdout, "<BODY>\n");  

  /* If the client accepted the cookie, the cookie would be visible in the
   * same cgiScript then. 
   */

  if (NULL != (cookie = getenv("HTTP-COOKIE")))
    fprintf(stdout, "Our funny cookie: %s\n<BR>", cookie);
  else
    fprintf(stdout, "Our funny cookie has not been accepted.\n<BR>");
  /* Dump the complete set of environment variables */
  for (i = 0; envp[i] && fprintf(stdout, "%s<BR>\n", envp[i]) > 0; i++);

  fprintf(stdout, "</BODY>\n");  
  fprintf(stdout, "</HTML>");  
}
-- snip

Well, as mentioned above -- maybe everyone knows this already.
However, I had been happy did I find such an example code on the web
two hours ago!  So, it might be the case that someone in the future
will find this script helpful.  Drop me a line at <ru...@maremma.ch> if
the example helped understanding,-)

wbr,
Lukas
PS: Some keywords for searching:
    cookie programming c code cgi header dynamic
-- 
Lukas Ruf
http://www.lpr.ch
"Once you have solved the problem, the issue has become trivial!", lpr

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org