中国站长下载-为中国站长提供最好最全的建站资源! 首 页发布资源有事留言繁體中文
设为首页
加入收藏
联系我们
 
您当前的位置:中国站长下载 -> 文章中心 -> 网页编程 -> CGI 专区 -> 文章内容  虚拟主机 域名注册 退出登录 用户管理
栏目导航
· ASP编程 · .NET编程
· PHP编程 · JSP编程
· CGI 专区
热门文章
· sndvol32 - sndvol3...
· [组图] FLASH:《大话李白》...
· 个人网站到底能赚多...
· [图文] Rundll.exe是病毒吗...
· [组图] Flash:制作MV
· 价值12万元的网站SE...
· 网站创业者,你需要...
· 一个成功的网站设计...
· [图文] FLASH:韩国导航条解...
· 中国网站的赚钱模式...
相关文章
在CGI中实现session的想法和实现
作者:无从考证  来源:CSDN  发布时间:2005-9-15 11:25:25  发布人:chinazhan

 减小字体 增大字体

 session_id = cgi_val(entries,"session_id");
/**
  * open session file
  */
   strcpy(sfp,"/tmp"); 
   strcat(sfp,"/sess_");
   strcat(sfp,session_id);
   sf = fopen(sfp,"rb+");
   if(  sf == NULL )
            /** can’t open session file,maybe session has time out **/ 
   {
       print_session_error("1");
       exit(1);
   }
/**
  * read session var
  */
  bzero(buffer,256);
  fread(buffer,1,256,sf);
  for(i=0,j=0,k=0;k<5 && i<strlen(buffer);i++)
  {
     if( buffer[i] == ’\n’  )
     {
        temp[j] = ’\0’;
        strcpy(str_array[k],temp);
        j = 0;
        k ++;
     }
     else
     {
       temp[j++] = buffer[i];
     }
  }
/**
  * check active time
  */ 
  time(&now);
  if( now - atoi(str_time) > atoi(parse_config_file("session_live_time")) )
  {
     print_session_error("2"); 
     exit(1);
  } 
/**
  * compare client hash_key to session hash_key
  */
  if( HTTP_COOKIE == "" || strcmp( HTTP_COOKIE+9 , str_hash_key ) != 0 )
  {
     print_session_error("3");
     exit(1);
  } 
/**
  * compare client ip to session ip
  */
  if( strcmp( REMOTE_ADDR, str_client_ip ) != 0 )
  {
     print_session_error("4");
     exit(1);
  }
/**  
  * refresh session active time
  */ 
  time(&now);
  sprintf(str_time,"%10d\n",now);
  fseek(sf,0,SEEK_SET);
  fputs(str_time,sf);  
/**
  * get new hash_key
  */
  srand(now);
  r = rand();
  for(i=0;i<16;i++)
  {
     srand(r);
     r = rand();
     str_hash_key[i] = r % 26 + ’a’;
  }
  str_hash_key[16] = ’\n’;
  str_hash_key[17] = ’\0’;
 
/**
  * refresh session hash_key
  */
  fseek(sf,11,SEEK_SET);
  fputs(str_hash_key,sf);
  fclose(sf);
/**  
  * send cookie refresh client hash_key
  */
  printf("Set-Cookie:hash_key=%s",str_hash_key);  
}
void kill_session()
{
  char *session_id;
  char *session_path;
  char sfp[128];
  session_id   = cgi_val(entries,"session_id");
  strcpy(sfp,"/tmp"); 
  strcat(sfp,"/sess_");
  strcat(sfp,session_id);
  remove(sfp);
}
void clean_session_file()
{
  DIR *pdir;
  struct dirent *ent;
  char *path;
  char *filename;
  char filepath[64];
  int fd;
  char str_time[11];
  time_t  now;
  path = "/tmp";
  pdir = opendir(path);
  if(pdir != NULL)
  {
    while( ent =readdir(pdir) )
    {
       filename = ent->d_name; 
       if( strncmp(filename,"sess_",5)==0 )
       {
          strcpy(filepath,path);
          strcat(filepath,"/");
          strcat(filepath,filename);
          fd = open(filepath,O_RDONLY);
          read(fd,str_time,10);
          time(&now);
          if( now - atoi(str_time) > atoi(parse_config_file("session_live_time")) ) 
          {
            remove(filepath);
          } 
          close(fd);
       }
    } 
  }
  closedir(pdir);
}
void print_session_error(char *n)
{
   printf("Content-type:text/html\n\n");
   printf("<html><head>";
   print_title("请重新登陆!");
   printf("</head>\n");
   printf("<body>\n");
   printf("对不起,请重新登陆。<p>\n");
   printf("你长时间没有操作,登陆已经超时。或者是系统发生了错误。<p>\n");
   printf("如果是后者,请与管理人员联系。\n");
   printf("<!--%s-->",n);
   printf("</body>");
   printf("</html>\n");

上一页  [1] [2] 

 
[] [返回上一页] [打 印] [收 藏]
上一篇文章:获取随机的密码
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
中国站长下载
中国站长下载

本页只接受PR>=4 IT类站点连接,申请连接,谢谢您们的支持!希望我们的下载站能够真正帮到中国的站长们!
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图
Copyright © 2005-2006 ChinaZhan.Net. All Rights Reserved .