Cookies w/ Apache - How to set from C/C++ program?

Cookies w/ Apache - How to set from C/C++ program?

Post by Kevin Bento » Tue, 14 Jan 1997 04:00:00



I'm having users execute a cgi program (C++) which sends output to
stdout that's intercepted by Apache and sent on to the client.  I want
to set a cookie which will allow me to record a number reflecting the
last time the user was on, what they did so I can pick up again where
they left off.  This will be used to help people read through the Psalms
and the Bible.

Below is a listing in source form which gives the output I desire
*except* that the cookie isn't being set.  First the output:

--- OUTPUT ---
Content-type:   text/html

Set-cookie: LPSARD=1; expires=Friday, 31-Dec-1999 23:59:59 GMT; path=/;
Set-cookie: LPSADA=13; expires=Friday, 31-Dec-1999 23:59:59 GMT; path=/;
Date: Tuesday, 14-Jan-97 02:29:40 GMT
Server: Apache/1.2b2
<HTML>
<HEAD>
<TITLE>Psalm of the day</TITLE>
</HEAD>
<BODY bgcolor="#ffffff">
<font size="+2">Psalm of the day</font><br>
1/14/1997 (GMT)<br>
<p>
<a href = "http://www.tims.net/bible/kjv/psalms-14.htm">Click here for
today's Psalm</a>
<p>
This page is changed every day.  Visitors may step through all of
the Psalms twice a year, automatically by visiting each day.
Soon, there will be an addition to allow users to read the Psalms
along with the rest of the Bible at their own pace.  Keep an eye
out for this new development.
<p>
Some users may need to reload this page every day.
If the date above is incorrect and you've been here before, you will
need to click on your browser's reload button.
<p>
<img align=left alt="BRI LOGO" height=113 width=124
src="http://www.tims.net/bible/kjv/pic/bri7.gif">
<img align=right alt="TIM LOGO" height=113 width=159
src="http://www.tims.net/bible/kjv/pic/tim2a.gif">

<p>
<center><a href="mailto:feedb...@tims.net">
  Send us E-mail - feedb...@tims.net</a></center>

<p>
<center><a href="http://www.tims.net/bri/">Back to BRI
homepage</a></center>

<p>
<center><a href="http://www.tims.net/timaudio.htm">Back to TIM Audio
Page</a></center>
<br clear=all>
argv[0]=*psalmday2*<br>
<br>
envp[0]=*HTTP_CONNECTION=Keep-Alive*<br>
envp[1]=*HTTP_USER_AGENT=Mozilla/3.01 (X11; I; Linux 1.2.13 i586)*<br>
envp[2]=*HTTP_PRAGMA=no-cache*<br>
envp[3]=*HTTP_HOST=users.sota-oh.com*<br>
envp[4]=*HTTP_ACCEPT=image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, */**<br>
envp[5]=*PATH=/home/s1kevin/.bin:/home/s1kevin/bin:/home/s1kevin/bin:/home/s1kevin/op-bin:/usr/local/bin:/usr/local/games:/usr/local/games/bin:/usr/local/samba/bin:/usr/local/Acrobat3/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11/bin:/usr/andrew/bin:/usr/openwin/bin:/usr/games:.:/sbin:/usr/sbin:/usr/local/sbin*<br>
envp[6]=*SERVER_SOFTWARE=Apache/1.2b2*<br>
envp[7]=*SERVER_NAME=users.sota-oh.com*<br>
envp[8]=*SERVER_PORT=80*<br>
envp[9]=*REMOTE_HOST=dns.sota-oh.com*<br>
envp[10]=*REMOTE_ADDR=206.21.85.196*<br>
envp[11]=*DOCUMENT_ROOT=/usr/local/etc/httpd/htdocs/sota_users*<br>
envp[12]=*SERVER_ADMIN=webmas...@sota-oh.com*<br>
envp[13]=*SCRIPT_FILENAME=/usr/local/etc/httpd/cgi-bin/psalmday2*<br>
envp[14]=*REMOTE_PORT=20996*<br>
envp[15]=*GATEWAY_INTERFACE=CGI/1.1*<br>
envp[16]=*SERVER_PROTOCOL=HTTP/1.0*<br>
envp[17]=*REQUEST_METHOD=GET*<br>
envp[18]=*QUERY_STRING=*<br>
envp[19]=*SCRIPT_NAME=/cgi-bin/psalmday2*<br>
</BODY>
</HTML>
--- Now the source Code... ---
/*
 *
 * psalmday.c
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>

const char weekday_list[7][10]= { "Sunday", "Monday", "Tuesday",
"Wednesday",
                                   "Thursday", "Friday", "Saturday" };
const char month_list[12][4] = { "Jan", "Feb", "Mar", "Apr", "May",
"Jun",
                                 "Jul", "Aug", "Sep", "Oct", "Nov",
"Dec" };

main(int argc, char *argv[], char *envp[])
{
  time_t     timer;
  struct tm *tm_ptr;
  char       zone_string[4];
  char      *last_psalm_read_ptr;
  char      *last_psalm_day_ptr;
  char      *software_ptr;
  int        last_psalm_read;
  int        last_psalm_day;

  time(&timer);
  tm_ptr=gmtime(&timer);

  last_psalm_read_ptr=getenv("LPSARD");
  last_psalm_day_ptr=getenv("LPSADA");

//  if (last_psalm_read_ptr==NULL) {
     last_psalm_read=1;
     last_psalm_day=tm_ptr->tm_yday;
//  } else {
//    last_psalm_read=atoi(strchr("LPSARD", '='));
//    if (last_psalm_day!=tm_ptr->tm_yday) {
//      last_psalm_read=(last_psalm_read%150)+1;
//    }
//    last_psalm_day=atoi(strchr("LPSADA", '=')+1);
//  }

  weekday_list[tm_ptr->tm_wday];
  weekday_list[tm_ptr->tm_mon];

  printf("Content-type: text/html\n\n");
  printf("Set-cookie: LPSARD=%d; expires=Friday,", last_psalm_read);
  printf(" 31-Dec-1999 23:59:59 GMT; path=/;\n");
  printf("Set-cookie: LPSADA=%d; expires=Friday,", tm_ptr->tm_yday);
  printf(" 31-Dec-1999 23:59:59 GMT; path=/;\n");
  printf("Date: %s, %02d-%s-%0d %02d:%02d:%02d GMT\n",
         weekday_list[tm_ptr->tm_wday], tm_ptr->tm_mday,
         month_list[tm_ptr->tm_mon], tm_ptr->tm_year%100,
         tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec);
  software_ptr=getenv("SERVER_SOFTWARE");
  printf("Server: %s\n", software_ptr);
  printf("<HTML>\n");
  printf("<HEAD>\n");
  printf("<TITLE>Psalm of the day</TITLE>\n");
  printf("</HEAD>\n");
  printf("<BODY bgcolor=\"#ffffff\">\n");

  printf("<font size=\"+2\">Psalm of the day</font><br>\n");
  printf("%d/%d/%d (GMT)<br>\n", tm_ptr->tm_mon+1, tm_ptr->tm_mday,
         tm_ptr->tm_year+1900);

  printf("<p>\n");
  printf("<a href =
\"http://www.tims.net/bible/kjv/psalms-%d.htm\">%s</a>\n",
         (tm_ptr->tm_yday%150)+1, "Click here for today's Psalm");

  printf("<p>\n");
  printf("This page is changed every day.  Visitors may step through all
of\n");
  printf("the Psalms twice a year, automatically by visiting each
day.\n");
  printf("Soon, there will be an addition to allow users to read the
Psalms\n");
  printf("along with the rest of the Bible at their own pace.  Keep an
eye\n");
  printf("out for this new development.\n");
  printf("<p>\n");
  printf("Some users may need to reload this page every day.\n");
  printf("If the date above is incorrect and you've been here before,
you ");
  printf("will need to click on your browser's reload button.\n");

  printf("<p>\n");
  printf("<img align=left alt=\"BRI LOGO\" ");
  printf("height=113 width=124
src=\"http://www.tims.net/bible/kjv/pic/bri7.gif\">\n");
  printf("<img align=right alt=\"TIM LOGO\" ");
  printf("height=113 width=159
src=\"http://www.tims.net/bible/kjv/pic/tim2a.gif\">\n");
  printf("\n");
  printf("<p>\n");
  printf("<center><a href=\"mailto:feedb...@tims.net\">\n");
  printf("  Send us E-mail - feedb...@tims.net</a></center>\n");
  printf("\n");
  printf("<p>\n");
  printf("<center><a href=\"http://www.tims.net/bri/\">");
  printf("Back to BRI homepage</a></center>\n");
  printf("\n");
  printf("<p>\n");
  printf("<center><a href=\"http://www.tims.net/timaudio.htm\">");
  printf("Back to TIM Audio Page</a></center>\n");
  printf("<br clear=all>\n");

  for (int i=0; i<argc; i++) printf("argv[%d]=*%s*<br>\n", i, argv[i]);
  printf("<br>\n");
  for (int i=0; envp[i]!=NULL; i++) printf("envp[%d]=*%s*<br>\n", i,
envp[i]);

  printf("</BODY>\n");
  printf("</HTML>\n");
  exit(0);

}

--- All done... ---

E-Mail:  s1ke...@tims.net
Web:     http://tims.net/kevin/kevin.html
Unsolicited advertisements processing fee: $50 subject to change without
notice

 
 
 

Cookies w/ Apache - How to set from C/C++ program?

Post by Robert Muhlestei » Thu, 16 Jan 1997 04:00:00


Loose the line return after Content-type: text/html and put it after the
last HTTP header line:

Set-cookie: LPSARD=1; expires=Friday, 31-Dec-1999 23:59:59 GMT; path=/;
Set-cookie: LPSADA=13; expires=Friday, 31-Dec-1999 23:59:59 GMT; path=/;
Date: Tuesday, 14-Jan-97 02:29:40 GMT
Server: Apache/1.2b2
Content-type:   text/html

<HTML>
...

You might also have to add "nph-" to the front of your CGI script name.

Rob Muhlestein
NIKE, Inc.
Opinions are my own.

 
 
 

1. My linux+Apache+mod_perl cannot recognize"print "Set-Cookie: cookie-name = Unique; "

Just solved the problem of not recognizing print "Content-type:
text/plain\n\n";
But another problem still exists.
When the perl script contains following lines:
     print "Set-Cookie: cookie-name = Unique; ";
     print "domain = .$baseurl; ";
     print "path = /; ";
     print "expires = Thursday, 11-Dec-2100 00:00:00 GMT\n";
It just print out these lines on the web.
Still the same script runs perfect under windows+apache+cygwin.
Please tell me how to change the httpd.conf file just as the last question.
Thank you very much.

2. kernel booting woes

3. apache,cookie and denying access to those whithout the cookie set

4. sigblock and system() under 4.3bsd

5. Can you log specific cookies with Apache %{cookie}i log format?

6. Free network traffic monitoring tool

7. Setting HTTP cookies with the Apache "valid-user" option...

8. f77 linker and static/dynamic libs

9. setting cookies using SSI's (Apache's mod_include)

10. CookieTracking on Apache sets cookies for EVERY file

11. Apache proxy Set-Cookie problem

12. Statistics programs that understand Apache/mod_usertrack cookies

13. Can't set cookies Apache 2.0.36