如何使用FastCGI,nginx和C访问cookie?

时间:2014-04-29 10:44:53

标签: c cookies nginx fastcgi

如何访问浏览器发送的cookie?

代码设置Cookie:

#include "fcgi_stdio.h"
#include <stdlib.h>

int main(void)
{
    while(FCGI_Accept() >= 0){
        printf("Content-type: text/html\r\nSet-Cookie: name=value\r\n\r\n");
        printf("REQUEST_METHOD = %s\n<br>", getenv("REQUEST_METHOD"));
        printf("QUERY_STRING = %s\n<br>", getenv("QUERY_STRING"));
        printf("CONTENT_TYPE = %s\n<br>", getenv("CONTENT_TYPE"));
        printf("CONTENT_LENGTH = %s\n<br>", getenv("CONTENT_LENGTH"));
        printf("SCRIPT_FILENAME = %s\n<br>", getenv("SCRIPT_FILENAME"));
        printf("SCRIPT_NAME = %s\n<br>", getenv("SCRIPT_NAME"));
        printf("REQUEST_URI = %s\n<br>", getenv("REQUEST_URI"));
        printf("DOCUMENT_URI = %s\n<br>", getenv("DOCUMENT_URI"));
        printf("DOCUMENT_ROOT = %s\n<br>", getenv("DOCUMENT_ROOT"));
        printf("SERVER_PROTOCOL = %s\n<br>", getenv("SERVER_PROTOCOL"));
        printf("REMOTE_ADDR = %s\n<br>", getenv("REMOTE_ADDR"));
        printf("REMOTE_PORT = %s\n<br>", getenv("REMOTE_PORT"));
        printf("SERVER_ADDR = %s\n<br>", getenv("SERVER_ADDR"));
        printf("SERVER_PORT = %s\n<br>", getenv("SERVER_PORT"));
    }
}

服务器配置:

events {
  worker_connections 1024;
}

http {
  server {
    listen 80;
    server_name localhost;

    location /login {
      fastcgi_pass   127.0.0.1:9000;

      fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
      fastcgi_param  SERVER_SOFTWARE    nginx;
      fastcgi_param  QUERY_STRING       $query_string;
      fastcgi_param  REQUEST_METHOD     $request_method;
      fastcgi_param  CONTENT_TYPE       $content_type;
      fastcgi_param  CONTENT_LENGTH     $content_length;
      fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
      fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
      fastcgi_param  REQUEST_URI        $request_uri;
      fastcgi_param  DOCUMENT_URI       $document_uri;
      fastcgi_param  DOCUMENT_ROOT      $document_root;
      fastcgi_param  SERVER_PROTOCOL    $server_protocol;
      fastcgi_param  REMOTE_ADDR        $remote_addr;
      fastcgi_param  REMOTE_PORT        $remote_port;
      fastcgi_param  SERVER_ADDR        $server_addr;
      fastcgi_param  SERVER_PORT        $server_port;
      fastcgi_param  SERVER_NAME        $server_name;
      fastcgi_pass_header Cookie;
    }

    location ~ \.(gif|jpg|png)$ {
        root /data/images;
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我找到了答案,只需阅读环境变量HTTP_COOKIE

printf("HTTP_COOKIE = %s\n<br>", getenv("HTTP_COOKIE"));