用c编写CGI,如何实现用户登录的session问题

来源:互联网 发布:南方大大数据100基金 编辑:程序博客网 时间:2024/06/04 20:03

我的web程序是  
  html+c语言cgi+boa服务器,以后要放到嵌入式板子里面  
  但是用户登录的session怎么处理?  
  javascript没有办法吧?  
  那只好通过c了,但是c怎么处理呢?  
  谢谢!

 

Session需要自己写,参考一下php的Session,它可以用文件存储,不用缓存在内存中  
   
  例如定义C的Session格式为:CGISESSION=[SESSIONID],SESSIONID自己随意设置长度,如果模拟php,就用32位定长的字母/数字随机字符,用Set-Cookie传送到客户端,然后把Session的内容存储在服务器端的特定文件夹下(例如:C:/winnt/temp/session,C:/winnt/temp这个目录的权限默认是很足的,EveryOne也有写的权限;或者写在cgi根目录下也可以),Session文件格式可以这样:  
  name=value;name=value;....  
  下次客户端再提交的时候,就解析HTTP_COOKIE变量,如果COOKIE名称是CGISESSION,那么就读取Session文件,解析Session数据……  
   
  当然如果要获得更快的速度,可以用共享内存,但不推荐,因为太多的访问者会造成内存猛涨  
  或者直接用Cookie,设置密钥,加密数据,把密钥和加密过的数据都Set-Cookie到客户端,客户端发送请求的时候,根据密钥解密数据

 

CSP/eybuild   API   参考手册:  
  http://www.eybuil.com  
   
  getCookie(   )  
  NAME  
  getCookie(   )   -   get   the   value   of   cookie  
   
  SYNOPSIS  
   
  char   *   getCookie  
          (  
          char   *   name  
          )  
   
  DESCRIPTION  
  this   routine   is   to   get   the   value   of   cookie   last   occur   in   cookie   list    
   
   
  NOTE  
   
  if   there   are   more   than   one   same   name   items,   this   routine   will   retun   the   last   one.   If   you   want   to   get   one   by   one   please   see   getCookieNext(   )   or   getCookieByIndex(   ).    
   
   
  RETURN  
  pointer   to   the   value   of   parameter,   or   ""   if   not   find    
   
   
  SEE   ALSO  
  ebrequest,   getParameter(   )    
   
  eyBuildLib   :   Routines  
   
   
   
  --------------------------------------------------------------------------------  
   
   
  setcookie(   )  
  NAME  
  setcookie(   )   -   send   cookie   with   HTTP   header  
   
  SYNOPSIS  
   
  int   setcookie  
          (  
          char   *   name,  
          char   *   value,  
                        ...                                 /*   time_t   expire,   char   *   path,   char   *   */  
                                                              /*   domain,   int   secure   */    
          )  
   
  DESCRIPTION  
  This   routine   send   cookie   with   HTTP   header.   Like   other   headers,   cookies   must   be   sent   before   any   output   flushed   from   your   script   (this   is   a   protocol   restriction,   make   sure   CSP   page   buffer   not   flushed   is   OK).    
   
  The   parameter   expire,   path,   domain,   secure   is   not   need.   You   should   make   sure   the   last   parameter   is   set   to   NULL,   eg:    
   
  setcookie("name1",   "value1",   NULL);                    
  setcookie("name2",   "value2",   time(NULL)+3600,   NULL);                    
  setcookie("name3",   "value3",   time(NULL)+7200,   "/cgi-bin/",   NULL);                    
   
  RETURNS  
  OK,   or   ERROR   while   send   cookie   error    
   
   
  SEE   ALSO  
  ebrespond,   header(   ),   print(   )    
   
   
  --------------------------------------------------------------------------------  
   
  Good   Luck!

 

使用加密用户名+时间的方式。  
  具体的操作是:  
  用户登录时通过验证后,将用户名+时间   进行一个可逆加密(比如DES),然后将加密的密文通过cookie写回客户端,客户端每次操作这个cookie,CGI做身份验证时,只需要解开这个密文,判断时间是否过期即可。

原创粉丝点击