时间: 2021-07-31 作者:daque
对于存户端的每一次登岸,在效劳器天生一个session,动作一个文献保存在效劳器上,比方在“/tmp”下。 文献定名为sess_发端,在加上一个随机的字符串,这个字符串称之为session_id。 在文献中保存的实质囊括: 1、用户的结果一次震动功夫。(用来查看用户能否长功夫没有操纵,视为仍旧退出登岸)。 2、一个随机的字符串。(用来考证存户端的身份,这个字符串同声动作cookie发往存户端)。 3、存户端的ip. 4、本质要保存的数据。比方用户的id,暗号等。 在用户登岸时,天生这个文献,而且,将谁人随机字符串发到存户端的cookie. 在此后的每个页面包车型的士超贯穿,或是form中的要跟入session_id. 每个页面发端,要: 1、查看能否超时。 2、比较cookie中的字符串和session文献中的,考证存户身份。 3、比较存户端ip和session文献中的ip,考证存户身份。 4、读出数据,供底下步调运用 5、革新结果震动功夫 6、天生新的随机字符串,革新session中对应局部,并将其动作cookie发往存户端。 由于我正在做的名目诉求比拟高的安定性,以是我在这上面商量的比拟多些,但我领会如许确定还 不是实足安定的。即使谁创造了什么缺点,烦恼报告我。 底下是我的局部实行代码: set_session()在登岸是挪用。 start_session()在每个页面包车型的士前方挪用。 kill_session()在退出登岸是挪用。 clean_session() 用来简略过时的session文献。 #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <dirent.h> #define remote_addr1 getenv("remote_addr") #define http_cookie getenv("http_cookie") char *sess_user_name; char *sess_user_pwd; static void print_session_error(char *); static void clean_session_file(); char *set_session(char *name,char *pwd) { char str_now[11]; char hash_key[17]; char *session_id; time_t now; file *sf; char sfp[32]; int i,temp,r; time(&now); /** * clean time out session file */ clean_session_file(); /** * get str_now */ sprintf(str_now,"%10d",now); /** * get random hash_key */ srand(now); r = rand(); for(i=0;i<16;i++) { srand(r); r = rand(); hash_key[i] = r%26 + ’a’; } hash_key[16] = ’\0’; /** * get more random session_id; */ temp = rand(); srand(temp); r = rand(); session_id = (char*) malloc(17*sizeof(char)); for(i=0;i<16; i++) { srand(r); r = rand(); session_id[i] = r%26 + ’a’; } session_id[16] = ’\0’; /** * create session file */ strcpy(sfp,"/tmp"); strcat(sfp,"/sess_"); strcat(sfp,session_id); sf = fopen(sfp,"w"); chmod(sfp,06777); if( sf == null ) { tc_error_page("can’t creat session file"); } /** * fputs session file */ fputs(str_now,sf); fputs("\n",sf); fputs(hash_key,sf); fputs("\n",sf); fputs(remote_addr1,sf); fputs("\n",sf); fputs(name,sf); //sess_user_name fputs("\n",sf); fputs(pwd,sf); // sess_user_pwd_ fputs("\n",sf); fclose(sf); /** * set cookie */ printf("set-cookie:hash_key=%s\n",hash_key); return session_id; } void start_session() { int i,j,k; char *session_id; file *sf; char sfp[32]; time_t now; int r; char buffer[256]; char temp[64]; char str_time[16]; char str_hash_key[20]; char str_client_ip[20]; char *str_array[6]; sess_user_name = (char*)malloc(32*sizeof(char)); sess_user_pwd = (char*)malloc(32*sizeof(char)); str_array[0] = str_time; str_array[1] = str_hash_key; str_array[2] = str_client_ip; str_array[3] = sess_user_name; str_array[4] = sess_user_pwd;[page_break] 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"); }