盾怪网教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

在CGI中完成session的想法与完成

时间:2024/12/3作者:未知来源:盾怪网教程人气:

[摘要]strcmp( HTTP_COOKIE+9 , str_hash_key ) != 0 ) { print_session_error("3"); exi...
 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");



关键词:在CGI中完成session的想法与完成




Copyright © 2012-2018 盾怪网教程(http://www.dunguai.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版