ææ ¢å¼·ã人ã®ããã®peclã§session_handlerãä½ãã
session_handlerä½ã£ã¦ã¾ããï¼
ã»ãã·ã§ã³ããã¡ã¤ã«ä»¥å¤ã«æ ¼ç´ããã¨ã使ãã¾ããã
ä¾ãã°ãã»ãã·ã§ã³ããã¼ã¿ãã¼ã¹ã¨ããKVSçã®ã»ãã·ã§ã³ãæ ¼ç´ããã¨ãã¨ãã
ã»ã¨ãã©ã®å ´å㯠php ã§ session_handlerãã·ã³ã·ã³ä½ã£ã¦ããã¨æãã¾ãã
ããphpã§ã»ãã·ã§ã³ãã³ãã©ã¼ãä½ãã¨ãããã©ã¼ãã³ã¹çã«ããã ã£ãããæ¯åä½ãã®ãããã©ãã£ããããã£ã¨é«åº¦ãªãã¨ãããããªã£ãããããã£ãªãã§ããã Yes/Y
ããã ã£ããã pecl 㪠PHPæ¡å¼µã¨ãã¦ãCè¨èªã§æ¸ããããªãã§ãããã
pecl ãªã»ãã·ã§ã³ãã³ãã©ã«å¯¾å¿ãã¦ãã pecl æ¡å¼µã®å ´åããã session.save_handler ãå®ç¾©ãã¦ããããã¨ã§ãç°¡åã«å°å ¥ã§ãã¾ãã
//tokyo tyrant ã«ã»ãã·ã§ã³ã¹ãã¢ããããæããã ini_set('session.save_handler', 'tokyo_tyrant'); ini_set('session.save_path', 'tcp://127.0.0.1:1978');
ãã®ãµã³ãã«ãä½ãã®ã«ã¯ã pecl tokyotyrant 㨠ext/session/mod_files.c ãåèã«ãã¦ãã¾ãã
åä½ãããµã³ãã«ã®ãã¦ã³ãã¼ã
ããã°ã©ã
http://rtilabs.net/files/2011_02_13/mytest_session.tar.gz
webã®ç»é¢
http://rtilabs.net/files/2011_02_13/webtest.tphp
0.å°ã£ãã¨ãã«æ³£ãã¤ãå
ã»ã»ãã·ã§ã³å¨ãã®å®ç¾©ãããã¦ãã¾ãã
php/ext/session/php_session.h
ã»ãã£ãã©ã«ãã®ã»ãã·ã§ã³ãã³ãã©ã¼ files ã®å®è£
ã§ãã
php/ext/session/mod_files.c
ã»åãã¦ããå®è£
ãåèã«www
pecl tokyotyrantã®ã½ã¼ã¹ã¨ãw
ã»æçµå
µå¨ãã¬ã¼ã«ããprintfãããã°ww
printfãããã°ã®ãä¾ã«
http://d.hatena.ne.jp/rti7743/20100604/1275665317
1.ç°å¢ãä½ãã
ããã§ã¯ãååã®ã魔法少女好きだったらわかるphp拡張の作り方。ãã§ä½æããã¢ã¸ã¥ã¼ã«ãããã«æ¡å¼µããã¦ãã»ãã·ã§ã³ãã³ãã©ã使ãã¾ãã
ã¨ããããã mytest.so ãåºæ¥ãã¨ããã¾ã§å§ãã¦ãã ããã
2. ã»ãã·ã§ã³ãã³ãã©ã¼ç¨ã®ããã°ã©ã ãæ¸ã
ä»åã¯ã/tmp/ ã®ä¸ã«ãã¡ã¤ã«ãä½ãã¨ãã php ã®ãã£ãã©ã«ãã®ã»ãã·ã§ã³ã¹ãã¢ããªã¹ãã¯ããããã®ãã¬ãã§å®è£ ãããã¨æãã¾ãã
mytest_session.c 㨠mytest_session.h ã®2ã¤ã®ãã¡ã¤ã«ã使ãã¾ãã
ãã¡ã¤ã«ã®æåã³ã¼ã㯠UTF-8 ã§ä½ã£ã¦ãã ããã
ã¾ã mytest_session.h ãä½ããã
#ifndef MYTEST_SESSION_H #define MYTEST_SESSION_H #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_mytest.h" #include <fcntl.h> /* ã»ãã·ã§ã³ç®¡çç¨ã®ãã¼ã¿æ§é */ typedef struct { char * filename; char * save_path; } php_mytest_session; #endif //MYTEST_SESSION_H
次ã«ãmytest_session.c ãä½ããã
ããã¤ã¯ã§ããããå±éºãå±ãªããã
vi mytest_session.c -------------------------------------------------------------------- #include "mytest_session.h" /* ã»ãã·ã§ã³ãã¼ã§ã¤ã¿ã ã©ããããªãããã«ãã§ãã¯ãã. php æ¬ä½ã¨åãè¦åã§ãã§ãã¯ãã. /a-zA-Z0-9,\-/ ã OK é·ãããããã¡ãçªç ´ããªãããã«. */ static int checkSessionKey(const char * key) { const char * p; for(p = key ; *p ; p ++) { if (! ( (*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || *p == ',' || *p == '-' )) { /* ç¡å¹ãªæåå */ return 0; } } if ( (int)(p - key) > MAXPATHLEN - 20 || key[0] == '\0') { /* -20㯠/tmp/mytest... ãªã©ã®åã®ç®å®w ããã£ãç§ã®ã»ãã·ã§ã³é·ãã! */ return 0; } return 1; } /** ã»ãã·ã§ã³ç¨ã®ã¦ãã¼ã¯ãªã©ã³ãã ãªæåå sid ãè¿ã. ç®ç 1.ã»ãã·ã§ã³ç¨ã®sid (ã©ã³ãã ãªã¦ãã¼ã¯ãªæåå)ã emalloc ãã¦è¿ã. 注æ ä¸è¦åç´ããã ããsession_regenerate_idã® ç½ ãããã ãããå¼ã°ãæãã»ãã·ã§ã³ãæå¹ãªå ´åã¨ç¡å¹ãªå ´åãããã ã¾ãä¸è¦æå¹ã ããå®ã¯ç¡å¹ãªã©ããè¤éãªå ´åãããã ã¤ã¾ãã åä½1.ã¾ã£ãããµã©å°ã®ç¶æ ã§ã»ãã·ã§ã³ãä½ãå ´å (大é¨åã®å ´å) åä½2.ã»ãã·ã§ã³ã¯æå¹ã§å©ç¨ã§ããããæ°ãã«ä½ãç´ãå ´å åä½3.ã»ãã·ã§ã³ã¯æå¹ã¨ããããå®ã¯ç¡å¹ãªãã¼ã¿ NULL ãå ¥ã£ã¦ããå ´å session_regenerate_id(TRUE) ãµã¼ãã«å¯¾ãã connection poolçããã¦ããå ´åã¯è¦æ³¨æã§ããã 詳ããã¯ã pecl tokyotyrant ã® session.c ã¨ããè¦ãã°ããã. å®ç¾© char *ps_create_sid_##x(void **mod_data, int *newlen TSRMLS_DC) */ PS_CREATE_SID_FUNC(mytest) { /* phpæ¬ä½ã®php_session_create_idãå¼ã³åºãã¦ã¦ãã¼ã¯ãª SID ãã¤ãã£ã¦ãããã èªåã§ä½ã£ã¦ããããã ãããã©ããã */ char *sid = php_session_create_id(PS(mod_data), NULL TSRMLS_CC); if (!sid) { php_error_docref(NULL TSRMLS_CC, E_ERROR, "PS_CREATE_SID_FUNC(mytest) SIDãã¤ãããªãã!!"); return NULL; } return sid; } /** ã»ãã·ã§ã³ç®¡çãéå§ãããã¨ãå¼ã°ããã ç®ç 1.ã»ãã·ã§ã³ç¨ã®ã¡ã¢ãªã®ç¢ºä¿ session = emalloc( sizeof( php_mytest_session ) ); PS_SET_MOD_DATA(session) ä»¥å¾ PS_GET_MOD_DATA() ã§åãåºãã. 2.æ ¼ç´ããå ´æã®åæåãè¡ãã PS(save_path) 注æ PS_OPEN_FUNCã¨ååããããã ããã§ã¯ãsid ãã¾ã ããããªãã®ã§ãã»ãã·ã§ã³ã open ã§ããªãã ãªããconnection poolçãããã§éå§ããå®è£ ãããã å®ç¾© int ps_open_##x(void **mod_data, const char *save_path, const char *session_name TSRMLS_DC) */ PS_OPEN_FUNC(mytest) { /* ã»ãã·ã§ã³ç¨ã®ã¡ã¢ãªãç¢ºä¿ */ php_mytest_session * session = (php_mytest_session*)emalloc( sizeof( php_mytest_session ) ); if (!session) { PS_SET_MOD_DATA(NULL); php_error_docref(NULL TSRMLS_CC, E_WARNING, "PS_OPEN_FUNC(mytest) ã»ãã·ã§ã³ãåããªãã"); return FAILURE; } /* ã»ãã·ã§ã³ãã»ã¼ããããã¹ */ session->save_path = estrndup( save_path, strlen( save_path )); /* ãµã¼ãã¿ã¤ãã®å ´åããã§æ¥ç¶ãããã¨ãã§ãã. */ /* ã»ãã·ã§ã³ãã¼ã¿ã®è¨é² */ PS_SET_MOD_DATA(session); return SUCCESS; } /** ã»ãã·ã§ã³ç®¡çãéæ¾ããæã«å¼ã°ãã. ç®ç 1.ã»ãã·ã§ã³ç¨ã®ã¡ã¢ãªã®éæ¾ PS_OPEN_FUNC ã§ç¢ºä¿ããé åã®éæ¾ãè¡ãã æ³¨æ ãã¨ã§ã§ãã PS_DESTORY_FUNC ã¨ç´ãããããã Close ã®ç®çã¯ã Openã®éã§ããã å®ç¾© int ps_close_##x(void **mod_data TSRMLS_DC) */ PS_CLOSE_FUNC(mytest) { /* ã»ãã·ã§ã³ç®¡çãã¼ã¿ã®èªã¿åºã. */ php_mytest_session * session = PS_GET_MOD_DATA(); if (!session) { /* closeãªã®ã§çãã«. */ return SUCCESS; } /* PS_OPEN_FUNCã§ç¢ºä¿ãããã¼ã¿ãè§£æ¾. */ efree(session->save_path); efree(session); /* ãã¼ã¿ã«ã¯ãä½ãããã¾ãããã */ session = NULL; PS_SET_MOD_DATA(NULL); return SUCCESS; } /** ã»ãã·ã§ã³ã«å¯¾ããèªã¿è¾¼ã¿ ç®ç 1.ã»ãã·ã§ã³ãã¼ã¿ key ãæããã¼ã¿ããPS_OPEN_FUNC ã§æºåãã PS_GET_MOD_DATA() ã使ã£ã¦èªã¿è¾¼ã 2.èªã¿è¾¼ãã ãã¼ã¿ *val ã«æ¸ãããã 3.èªã¿è¾¼ãã ãã¼ã¿é·ã㯠*vallen ã«æ¸ã. å®ç¾© int ps_read_##x(void **mod_data, const char *key, char **val, int *vallen TSRMLS_DC) */ PS_READ_FUNC(mytest) { /* ã»ãã·ã§ã³ç®¡çãã¼ã¿ã®èªã¿åºã. */ php_mytest_session * session = PS_GET_MOD_DATA(); if (!session) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "PS_READ_FUNC(mytest) ã»ãã·ã§ã³ãåããªãã"); return FAILURE; } /* 弿° key ã§ã¤ã¿ã ã©ãããªãããã«. */ if ( ! checkSessionKey(key) ) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "PS_READ_FUNC(mytest) key %s ã¯ç¡å¹ãªæååã§ã" , key); return FAILURE; } /* 弿° key ãæã示ããã¼ã¿ãéã. */ char filename[MAXPATHLEN]; snprintf(filename,MAXPATHLEN,"%s/mytest.session.%s.txt",session->save_path , key ); filename[MAXPATHLEN - 1] = '\0'; FILE * fp = fopen(filename,"ab+"); if (!fp) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "PS_READ_FUNC(mytest) ãã¡ã¤ã« %s ãéããªãã! %s %d" , filename,strerror(errno), errno); return FAILURE; } flock(fp, LOCK_EX); //å é ã«æ»ã. rewind(fp); struct stat sbuf; if (fstat(fileno(fp), &sbuf)) { fclose(fp); php_error_docref(NULL TSRMLS_CC, E_WARNING, "PS_READ_FUNC(mytest) ãã¡ã¤ã« %s ã® fstat ãåããªãã" ,filename,strerror(errno), errno); return FAILURE; } /* ãã¼ã¿ã®é·ãã 0 ã®å ´åã¯ç¹æ®ãªå¦çããã. */ if (sbuf.st_size == 0) { flock(fp, LOCK_UN); fclose(fp); *val = STR_EMPTY_ALLOC(); *vallen = 0; return SUCCESS; } /* ãã¼ã¿é·åã®é åã確ä¿.*/ char* readbuffer = (char*)emalloc(sbuf.st_size); int readsize = fread(readbuffer,1,sbuf.st_size,fp); /* ãããã¼ã¿ã¯ä¸è¦ãªã®ã§ãã¡ã¤ã«ãéãã. */ flock(fp, LOCK_UN); fclose(fp); /* èªããããªï¼ */ if (readsize != sbuf.st_size) { /* èªãããã£ããã */ efree(readbuffer); php_error_docref(NULL TSRMLS_CC, E_WARNING, "PS_READ_FUNC(mytest) ãã¡ã¤ã« %s ãããã¼ã¿ãèªã¿åããªãã" , filename); return FAILURE; } /* èªã¿è¾¼ãã ãã¼ã¿ãè¿ã. (readbufferãéæ¾ããã®ã¯ãphpããã®è²¬ä»»!!) */ *val = readbuffer; *vallen = readsize; /* ãã£ãã¼!! */ return SUCCESS; } /** ã»ãã·ã§ã³ã«å¯¾ããæ¸ã込㿠ç®ç 1.ã»ãã·ã§ã³ãã¼ã¿ key ã®ãã¼ã¿ val ããPS_OPEN_FUNC ã§æºåãã PS_GET_MOD_DATA() ã使ã£ã¦æ¸ããã 2.ãã¼ã¿ val ã®ãã¼ã¿ã®é·ã vallen ã«ãªã. å®ç¾© int ps_write_##x(void **mod_data, const char *key, const char *val, const int vallen TSRMLS_DC) */ PS_WRITE_FUNC(mytest) { /* ã»ãã·ã§ã³ç®¡çãã¼ã¿ã®èªã¿åºã. */ php_mytest_session * session = PS_GET_MOD_DATA(); if (!session) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "PS_WRITE_FUNC(mytest) ã»ãã·ã§ã³ãåããªãã"); return FAILURE; } /* 弿° key ã§ã¤ã¿ã ã©ãããªãããã«. */ if ( ! checkSessionKey(key) ) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "PS_WRITE_FUNC(mytest) key %s ã¯ç¡å¹ãªæååã§ã" , key); return FAILURE; } /* 弿° key ãæã示ããã¼ã¿ãéã. */ char filename[MAXPATHLEN]; snprintf(filename,MAXPATHLEN,"%s/mytest.session.%s.txt",session->save_path , key ); filename[MAXPATHLEN - 1] = '\0'; FILE * fp = fopen(filename,"wb+"); if (!fp) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "PS_WRITE_FUNC(mytest) ãã¡ã¤ã« %s ãéããªãã! %s %d" , filename,strerror(errno), errno); return FAILURE; } flock(fp, LOCK_EX); /* ãã¼ã¿é·åã®é åã確ä¿. */ int writesize = fwrite(val,1,vallen,fp); /* ãããã¼ã¿ã¯ä¸è¦ãªã®ã§ãã¡ã¤ã«ãéãã. */ flock(fp, LOCK_UN); fclose(fp); /* ãããããªï¼ */ if (writesize != vallen) { /* ããããã£ãããã */ php_error_docref(NULL TSRMLS_CC, E_WARNING, "PS_WRITE_FUNC(mytest) ãã¡ã¤ã« %s ã«ããããã£ãã!" , filename); return FAILURE; } return SUCCESS; } /** ã»ãã·ã§ã³ãã¼ã¿ãåé¤ãã ç®ç 1.ã»ãã·ã§ã³ãã¼ã¿ key ã®ãã¼ã¿ãåé¤ãã æ³¨æ PS_CLOSE_FUNCã¨ã¯éããPS_SET_MOD_DATA(NULL)ãããããã§ã¯ãªããã ããã¾ã§ãã key ã®ãã¼ã¿ãæ¶å»ããã ãã§ããã å®ç¾© int ps_delete_##x(void **mod_data, const char *key TSRMLS_DC) */ PS_DESTROY_FUNC(mytest) { /* ã»ãã·ã§ã³ç®¡çãã¼ã¿ã®èªã¿åºã. */ php_mytest_session * session = PS_GET_MOD_DATA(); if (!session) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "PS_DESTROY_FUNC(mytest) ã»ãã·ã§ã³ãåããªãã"); return FAILURE; } /* 弿° key ã§ã¤ã¿ã ã©ãããªãããã«. */ if ( ! checkSessionKey(key) ) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "PS_DESTROY_FUNC(mytest) key %s ã¯ç¡å¹ãªæååã§ã" , key); return FAILURE; } /* 弿° key ãæã示ããã¼ã¿ãéã. */ char filename[MAXPATHLEN]; snprintf(filename,MAXPATHLEN,"%s/mytest.session.%s.txt",session->save_path , key ); filename[MAXPATHLEN - 1] = '\0'; /* ãã¼ã¿ key ã®ãã¼ã¿ãå ¥ã£ã¦ãããã¡ã¤ã«ãåé¤ãã. */ unlink(filename); return SUCCESS; } /* ã¬ãã¼ã¸ã³ã¬ã¯ã·ã§ã³ãè¡ãã ç®ç 1.å¤ããã¼ã¿ãã¡ã¤ã«ãåé¤ãã 2.å¤ããã¼ã¿ã®å®ç¾©ã¯ã弿° maxlifetime (ç§)ãéãããã¼ã¿ã§ããã æ³¨æ PHPã®ã»ãã·ã§ã³ã¯ãç¹å®ã®ã¦ã¼ã¶ã»ãã·ã§ã³ãå©ç¨ãã¦ãã»ãã·ã§ã³ã® GC ãèµ·åããã éãGCãåããã¨ã¦ã¼ã¶ä½é¨ã®é»å®³ã«ãªãå¯è½æ§ãããã GC ãçºåãã確çã¯ã PS(gc_probability) / PS(gc_divisor) ã§ããã å®ç¾© int ps_gc_##x(void **mod_data, int maxlifetime, int *nrdels TSRMLS_DC) */ PS_GC_FUNC(mytest) { /* ã»ãã·ã§ã³ç®¡çãã¼ã¿ã®èªã¿åºã. save_path ãåãåºãããã ãã«å©ç¨ãã... */ php_mytest_session * session = PS_GET_MOD_DATA(); if (!session) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "PS_GC_FUNC(mytest) ã»ãã·ã§ã³ãåããªãã"); return FAILURE; } /* GCããããã«ãã£ã¬ã¯ããªãéãã ããã«ããå¤ããã¡ã¤ã«ãåé¤ãã¦ãã. */ DIR *dir = opendir(session->save_path); if (!dir) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "PS_GC_FUNC(mytest) ãã£ã¬ã¯ã㪠%s ãéããªãã! %s %d" , session->save_path,strerror(errno), errno); return FAILURE; } /* ç¾å¨æå»ã ãã®æå»ããã¼ã¹ã«ã 弿° maxlifetime (ç§) éãã¦ãããã¼ã¿ãåé¤ãã¦ãã. */ time_t now = time(NULL); char filename[MAXPATHLEN]; struct dirent *dire; while ((dire = readdir(dir)) != NULL ) { /* ã»ãã·ã§ã³ãã¼ã¿ä»¥å¤ã¯ç¡è¦ãã. */ if ( strcmp(dire->d_name , "mytest.session.") != 0) { continue; } /* ãã¡ã¤ã«åã®çæ */ snprintf(filename,MAXPATHLEN,"%s/mytest.session.%s.txt",session->save_path , dire->d_name ); filename[MAXPATHLEN - 1] = '\0'; struct stat sbuf; if (stat(filename , &sbuf)) { /* 使 ãæ¥ä»ã¨ããèªããªã... */ continue; } /* å¤ããã¼ã¿ï¼ */ if ((now - sbuf.st_mtime) <= maxlifetime) { /* å¤ããªã. */ continue; } /* ãã¼ã¿ãæ¶å»ãã. */ unlink(filename); } return SUCCESS; } /* session_handler ãå®ç¾ããããã®ãã¾ããªã. ããã¯ããã¯ãå±éããã¦ã PS_CREATE_SID_FUNC ãªã©ã§å®ç¾©ãã颿°ã¸ã®ã«ãã¯ã¢ãããã¼ãã«ã«ãªããã å®ç¾© typedef struct ps_module_struct { const char *s_name; int (*s_open)(PS_OPEN_ARGS); int (*s_close)(PS_CLOSE_ARGS); int (*s_read)(PS_READ_ARGS); int (*s_write)(PS_WRITE_ARGS); int (*s_destroy)(PS_DESTROY_ARGS); int (*s_gc)(PS_GC_ARGS); char *(*s_create_sid)(PS_CREATE_SID_ARGS); } ps_module; */ ps_module ps_mod_mytest = { PS_MOD(mytest) }; -------------------------------------------------------------------------
é·ããã
ã¨ããããåãããã¨ãåæã«ããããã®ã§è§£èª¬ã¯ãã¨ã§ãããã
3 php_mytest.h ã®å¤æ´
php_mytest.h ã®ãã¨è¦ãã¦ãï¼
mytest.so ã®ã³ã¢ã«ãªã£ããããã¼ãã¡ã¤ã«ã ãã
æå¾ã®æ¹ã®1ç®æå¤æ´ãããã
vi php_mytest.h ------------------------------------------------------------------------- #ifndef PHP_MYTEST_H #define PHP_MYTEST_H extern zend_module_entry mytest_module_entry; #define phpext_mytest_ptr &mytest_module_entry #ifdef PHP_WIN32 # define PHP_MYTEST_API __declspec(dllexport) #elif defined(__GNUC__) && __GNUC__ >= 4 # define PHP_MYTEST_API __attribute__ ((visibility("default"))) #else # define PHP_MYTEST_API #endif #ifdef ZTS #include "TSRM.h" #endif PHP_MINIT_FUNCTION(mytest); PHP_MSHUTDOWN_FUNCTION(mytest); PHP_RINIT_FUNCTION(mytest); PHP_RSHUTDOWN_FUNCTION(mytest); PHP_MINFO_FUNCTION(mytest); PHP_FUNCTION(confirm_mytest_compiled); /* For testing, remove later. */ PHP_FUNCTION(mytest_mahou_syoujyo); /* 追å 追å 追å 追å 追å 追å 追å */ /* Declare any global variables you may need between the BEGIN and END macros here: ZEND_BEGIN_MODULE_GLOBALS(mytest) long global_value; char *global_string; ZEND_END_MODULE_GLOBALS(mytest) */ /* In every utility function you add that needs to use variables in php_mytest_globals, call TSRMLS_FETCH(); after declaring other variables used by that function, or better yet, pass in TSRMLS_CC after the last function argument and declare your utility function with TSRMLS_DC after the last declared argument. Always refer to the globals in your function as MYTEST_G(variable). You are encouraged to rename these macros something shorter, see examples in any other php module directory. */ #ifdef ZTS #define MYTEST_G(v) TSRMG(mytest_globals_id, zend_mytest_globals *, v) #else #define MYTEST_G(v) (mytest_globals.v) #endif /* 追å 追å 追å 追å 追å 追å 追å 追å 追å 追å ã»ãã·ã§ã³é¢ä¿ã®ãã¼ã */ #include "ext/session/php_session.h" extern ps_module ps_mod_mytest; /* ããã¾ã§å¤æ´ */ #endif /* PHP_MYTEST_H */ -----------------------------------------------------------------
4 mytest.c ã®å¤æ´
session handler ã®å¤æ´ããããã£ã¦éç¥ãã¦ãããæã追å ãããã
ãããæå¾ã®æ¹ã«1ç®æå¤æ´ããããã
vi mytest.c ----------------------------------------------------------------- #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_mytest.h" /* If you declare any globals in php_mytest.h uncomment this: ZEND_DECLARE_MODULE_GLOBALS(mytest) */ /* True global resources - no need for thread safety here */ static int le_mytest; /* {{{ mytest_functions[] * * Every user visible function must have an entry in mytest_functions[]. */ const zend_function_entry mytest_functions[] = { PHP_FE(confirm_mytest_compiled, NULL) /* For testing, remove later. */ PHP_FE(mytest_mahou_syoujyo, NULL) /* For moemoe chun. â追å 追å 追å 追å */ {NULL, NULL, NULL} /* Must be the last line in mytest_functions[] */ }; /* }}} */ /* {{{ mytest_module_entry */ zend_module_entry mytest_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "mytest", mytest_functions, PHP_MINIT(mytest), PHP_MSHUTDOWN(mytest), PHP_RINIT(mytest), /* Replace with NULL if there's nothing to do at request start */ PHP_RSHUTDOWN(mytest), /* Replace with NULL if there's nothing to do at request end */ PHP_MINFO(mytest), #if ZEND_MODULE_API_NO >= 20010901 "0.1", /* Replace with version number for your extension */ #endif STANDARD_MODULE_PROPERTIES }; /* }}} */ #ifdef COMPILE_DL_MYTEST ZEND_GET_MODULE(mytest) #endif /* {{{ PHP_INI */ /* Remove comments and fill if you need to have entries in php.ini PHP_INI_BEGIN() STD_PHP_INI_ENTRY("mytest.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_mytest_globals, mytest_globals) STD_PHP_INI_ENTRY("mytest.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_mytest_globals, mytest_globals) PHP_INI_END() */ /* }}} */ /* {{{ php_mytest_init_globals */ /* Uncomment this function if you have INI entries static void php_mytest_init_globals(zend_mytest_globals *mytest_globals) { mytest_globals->global_value = 0; mytest_globals->global_string = NULL; } */ /* }}} */ /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(mytest) { /* If you have INI entries, uncomment these lines REGISTER_INI_ENTRIES(); */ /* 追å 追å 追å 追å 追å 追å 追å 追å 追å 追å 追å 追å 追å 追å 追å ã»ãã·ã§ã³ç¨ã¢ã¸ã¥ã¼ã«ããã¼ãããã§ãã. */ php_session_register_module(&ps_mod_mytest); return SUCCESS; } /* }}} */ /* {{{ PHP_MSHUTDOWN_FUNCTION */ PHP_MSHUTDOWN_FUNCTION(mytest) { /* uncomment this line if you have INI entries UNREGISTER_INI_ENTRIES(); */ return SUCCESS; } /* }}} */ /* Remove if there's nothing to do at request start */ /* {{{ PHP_RINIT_FUNCTION */ PHP_RINIT_FUNCTION(mytest) { return SUCCESS; } /* }}} */ /* Remove if there's nothing to do at request end */ /* {{{ PHP_RSHUTDOWN_FUNCTION */ PHP_RSHUTDOWN_FUNCTION(mytest) { return SUCCESS; } /* }}} */ /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(mytest) { php_info_print_table_start(); php_info_print_table_header(2, "mytest support", "enabled"); php_info_print_table_end(); /* Remove comments if you have entries in php.ini DISPLAY_INI_ENTRIES(); */ } /* }}} */ /* Remove the following function when you have succesfully modified config.m4 so that your module can be compiled into PHP, it exists only for testing purposes. */ /* Every user-visible function in PHP should document itself in the source */ /* {{{ proto string confirm_mytest_compiled(string arg) Return a string to confirm that the module is compiled in */ PHP_FUNCTION(confirm_mytest_compiled) { char *arg = NULL; int arg_len, len; char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } len = spprintf(&strg, 0, "ãããã¡ããã¨çµå©ããã! Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "mytest", arg); RETURN_STRINGL(strg, len, 0); } /* }}} */ /* 以ä¸å ¨é¨è¿½å ! */ /* {{{ proto string mytest_mahou_syoujyo(string arg) */ PHP_FUNCTION(mytest_mahou_syoujyo) { char *arg = NULL; int arg_len, len; char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } if ( strcmp(arg,"ããã") == 0) { len = spprintf(&strg, 0,"ã¯ã«ãã¼ã"); } else { len = spprintf(&strg, 0,"ããã %s ã¢ããªãããã£",arg); } RETURN_STRINGL(strg, len, 0); } /* }}} */ /* The previous line is meant for vim and emacs, so it can correctly fold and unfold functions in source code. See the corresponding marks just before function definition, where the functions purpose is also documented. Please follow this convention for the convenience of others editing your code. */ -----------------------------------------------------------------
5. ã©ã¹ã! config.m4 ã®ä¿®æ£
æ«å°¾ã®ä¸è¡ãä¿®æ£ãã¾ãã
ããããªãã¨ãã»ãã·ã§ã³ã管çãããªãã£ãããã mytest_session.c ãã³ã³ãã¤ã«å¯¾è±¡ã«ãªãã¾ããã
vi config.m4 ------------------------------------------------------------------------- PHP_NEW_EXTENSION(mytest, mytest.c, $ext_shared) âââââââââ夿´âââââââââââââââ PHP_NEW_EXTENSION(mytest, mytest.c mytest_session.c, $ext_shared) -------------------------------------------------------------------------
6. ã§ããã¼
åããã¦ã¿ã¾ããã!!
make clean phpize ./configure make
ããã¾ã§ã§ã¨ã©ã¼ãªãã§ããï¼
ã¨ã©ã¼ããªããªãã ã¤ã³ã¹ãã¼ã«ãã¦ã¿ã¾ãããã
sessionå¨ããªã®ã§ããã©ã¦ã¶è¶ãã«ãããã¹ãã§ããªãã§ãã
7.ã¤ã³ã¹ãã¼ã«
make install
ããã§ã /usr/lib/php/modules/mytest.so ã«ã³ãã¼ããã¾ãã
8. æ¡å¼µã phpãèªã¿è¾¼ãããã«ããã
æ¡å¼µã php ããã¼ãã§ããããã«ã extension ã«æ¸ãã¦ããã¾ãã
vi /etc/php.d/mytest.ini ---------------------------------------- extension=mytest.so ----------------------------------------
9. httpd ãªã¹ã¿ã¼ã!!
/etc/init.d/httpd restart
10.å®è¡ãã php ãè¨ç½®ãã.
以ä¸ã®ãã㪠php ã webroot ã®ä¸ã«è¨ç½®ãã¾ãã
å
容ã¯ãã»ãã·ã§ã³ãå©ç¨ãã¦ããªãã¼ããããã³ã«æ°åã coutnupããããã®ã§ãã
--------------------------------------------------------- <?php //mytest ãå©ç¨ãã ini_set('session.save_handler', 'mytest'); ini_set('session.save_path', '/tmp'); session_start(); if ( isset ( $_GET['renew'] ) ) { $old_session_id = session_id(); $result_session_regenerate_id = session_regenerate_id(TRUE); $new_session_id = session_id(); echo "REGENERATE!!<br>\r\n"; echo "old_session_id:{$old_session_id}<br>\r\n"; echo "new_session_id:{$new_session_id}<br>\r\n"; echo "session_regenerate_id return :{$result_session_regenerate_id}<br>\r\n"; echo "<br>\r\n"; } else { $new_session_id = session_id(); } $a = (int)@$_SESSION['a']; @$_SESSION['a'] = $a + 1; echo "Count : {$a}<br>\r\n"; echo "Session : {$new_session_id}<br>\r\n"; ?> <br> <br> <br> <a href="?">countup : reload or click</a><br> <a href="?renew">session_regenerate_id</a><br> <br> ---------------------------------------------------------
11.ãã©ã¦ã¶ãããã£ãã® php ãé²è¦§ãã!!
ã¨ã©ã¼ã表示ããããã«ãæ°åãã«ã¦ã³ãã¢ãããããã°æåã§ã!!!!!!
ãç²ããã¾ã§ããã
ã¨ã©ã¼ã表示ããã¦ãã¾ã£ãã¨ããããªããããã£ããä½ãããããããã§ããã¼ã
ã©ããã¾ããã¨ããããã£ãã¨æãã¾ãã
ããããããããã°ããããã¾ãããæ²ç¤ºæ¿ã§æãã¦ããã ããã¨å¹¸ãã§ãã
12.ã»ãã·ã§ã³ã®ç¢ºèª
ãã¾ãåä½ããã¨ã /tmp/ã®ä¸ã«ä»¥ä¸ã®ãããªã»ãã·ã§ã³ãä½ããã¾ãã
phpã®ãã£ãã©ã«ãã®ã»ãã·ã§ã³ã¯ sess_* ã¨ããååãªã®ã§ã ä»åä½ã£ã mytest ã»ãã·ã§ã³ãã³ãã©ã¼ãæ£ããåä½ãã¦ãããã¨ããããã¾ãã
ll /tmp/ -------------------------------------------------- mytest.session.5b6173nhv0t9c3vg5g46sh1ve6.txt --------------------------------------------------
ãã£ããâ
ãã¬ã³ã¿ã¤ã³ãã§ã³æ¬²ããï¼
![ã«ã¼ããã£ãã¿ã¼ããã âã¯ãã¦ã«ã¼ãç·¨â BOX (æééå®çç£) [Blu-ray] ã«ã¼ããã£ãã¿ã¼ããã âã¯ãã¦ã«ã¼ãç·¨â BOX (æééå®çç£) [Blu-ray]](https://images-fe.ssl-images-amazon.com/images/I/51w8ZQaaODL._SL160_.jpg)
ã«ã¼ããã£ãã¿ã¼ããã âã¯ãã¦ã«ã¼ãç·¨â BOX (æééå®çç£) [Blu-ray]
- åºç社/ã¡ã¼ã«ã¼: ã¸ã§ããªã³ ã¨ã³ã¿ãã¤ã³ã¡ã³ã
- çºå£²æ¥: 2009/03/27
- ã¡ãã£ã¢: Blu-ray
- ã¯ãªãã¯: 264å
- ãã®ååãå«ãããã° (43ä»¶) ãè¦ã