php - How to set and read cookies using C and the G-wan web server -


in php set cookie doing

setting new cookie ============================= <?php  setcookie("name","value",time()+$int); /*name cookie's name value cookie's value $int time of cookie expires*/ ?>  getting cookie ============================= <?php  echo $_cookie["your cookie name"]; ?> 

how set , read cookie?

i can't seem find articles on web explaining hoe this. in fact there not many c web development article

the g-wan tar ball includes 2 sample source code files related cookies: cookie.c (set 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 http code (301:'moved') } 

and cookies.c (read cookies)

#include "gwan.h" #include <stdio.h> #include <string.h>  // ---------------------------------------------------------------------------- // '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 client cookies (make sure 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; } 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -