4

In php you would set a cookie by doing

Setting new cookie
=============================
<?php 
setcookie("name","value",time()+$int);
/*name is your cookie's name
value is cookie's value
$int is time of cookie expires*/
?>

Getting Cookie
=============================
<?php 
echo $_COOKIE["your cookie name"];
?>

How do you set and read cookie?

I can't seem to find any articles on the web explaining hoe to do this. In fact there is not many c web development article

Craig S. Anderson
  • 6,966
  • 4
  • 33
  • 46
matt
  • 337
  • 4
  • 14

2 Answers2

2

The G-WAN tar ball includes 2 sample source code files related to cookies: cookie.c (set a cookie)

#include "gwan.h" // G-WAN exported functions

int main(int argc, char *argv[])
{
   // "Set-Cookie: Domain=.foo.com; Path=/; Max-Age=%u\r\n"
   const char cookie[] = "Set-Cookie: Max-Age=3600\r\n" // 1 hour
                         "Location: /?served_from\r\n\r\nblah\r\n";
   http_header(HEAD_ADD, (char*)cookie, sizeof(cookie) - 1, argv);

   return 301; // return an HTTP code (301:'Moved')
}

and cookies.c (read cookies)

#include "gwan.h"
#include <stdio.h>
#include <string.h>

// ----------------------------------------------------------------------------
// where 'cookies' = "key1=value1; key2=value2;"
// ----------------------------------------------------------------------------
static kv_t parse_cookies(char *cookies)
{
  kv_t cookies_store;
  kv_init(&cookies_store, "cookies", 1024, 0, 0, 0);

  char *key, *val, *lasts = 0;
  for(;;)
  {
     key = strtok_r(cookies, "= ", &lasts);
     val = strtok_r(0, ";,", &lasts);

     if(!val) break; //no more cookies

     kv_add(&cookies_store, 
            &(kv_item){
             .key = key,
             .val = val,
             .flags = 0,
           });

     cookies = 0;
  }
  return cookies_store;
}
// ----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
  // using the client cookies (make sure that they are there)
  // http_t *http = get_env(argv, HTTP_HEADERS, 0);
  // kv_t cookies_store = parse_cookies(http->h_cookies);

  // using fixed cookies (for tests without client cookies)
  char cookies[] = "key=val;foo=bar"; 
  kv_t cookies_store = parse_cookies(cookies);

  char *val = kv_get(&cookies_store, "key", sizeof("key") - 1);

  printf("%s = %s\n", "key", val);

  kv_free(&cookies_store);
  return 200;
}
Nagi
  • 256
  • 1
  • 2
  • 4
  • Where did you get this example from – matt Jun 12 '15 at 04:54
  • What is the example cookie name here – matt Jun 12 '15 at 04:56
  • Do you need to compile c for gwan? – matt Jun 12 '15 at 06:08
  • you can find them in G-WAN tar ball: /gwan_linux32-bit/0.0.0.0_8080/#0.0.0.0/csp – Nagi Jun 12 '15 at 06:16
  • To set cookie, use the syntax you see in the [wiki page](http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#innerlink_set-cookie). Cookies.c break down the HTTP cookies header into a [G-WAN KV](http://gwan.com/api#kv). For your last question - you need gcc in order to run G-WAN C servlet. – Nagi Jun 12 '15 at 06:20
  • Yeah but every time you change the progam do you have to compile it? – matt Jun 12 '15 at 06:27
  • G-WAN let you write C code as if it's a scripting language. You don't have to compile anything - G-WAN do it for you. Just put your code in the correct place and browse (http://localhost:8080/csp/?cookie.c) – Nagi Jun 12 '15 at 08:34
1

Cookie are set like any other HTTP header : http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#innerlink_set-cookie

You can find sample code in the gwan archive, here : gwan_linux64-bit/0.0.0.0_8080/#0.0.0.0/csp

ker2x
  • 440
  • 3
  • 11