php 设置中的 auto_append_file 的一点注意

php 的auto_append_file设置允许在PHP执行退出时自动执行一个文件,我们就可以在这个文件中做一些自己想做的事了, 但是你要注意:
1. 该设置不能通过ini_set来设置,可在 php.ini, .htaccesshttpd.conf 中设置; 参考: http://php.net/ini
2.  如果主脚本中执行了exit,则auto_append_file 指定的文件将不被执行

参考资料:
http://cn.php.net/manual/zh/ini.php
http://cn.php.net/manual/zh/ini.sections.php

C语言中宏的几种定义方式

C语言中宏的几种定义方式:


#include
#include
#define ECHO(str) printf(“%s: %s”, #str,str)
#define ECHO2(str,num) printf(“%s”, str##num)
#define MULTILINE_ECHO(str,str2,str3) do {\
printf(“multiline echo:\n”); \
printf(“%s”, str); \
printf(“%s”, str2); \
printf(“%s”, str3); \
}while(0)

int main(int ac, char **av) {

char str[10] = “str1\n\0”;
char str2[10] = “str2\n\0”;
char str3[10] = “str3\n\0”;
ECHO(str);
ECHO2(str,2);
MULTILINE_ECHO(str, str2, str3);
return 0;
}

str: str1
str2
multiline echo:
str1
str2
str3

vim阅读apache的mod_php

创建tags 文件:
php-5.3.3 目录下:
ctags -R .
ctags -R -a -f tags /path/to/httpd-2.2.16
如果需要tags使用绝对路径,则可以使用参数:
–tag-relative=no

1. mod_php.c的写法,主要是定义一些函数,然后通过模块结构体变量挂到apache的钩子上:

php-5.3.3/sapi/apache/mod_php5.c
  1.     
    //…

        

  2.     
    // 1. 这个结构的名字php5_module是有严格要求的

        

  3.     
    module MODULE_VAR_EXPORT php5_module =

        

  4.     
    {

        

  5.     
        STANDARD_MODULE_STUFF,

        

  6.     
        php_init_handler,           /* initializer */

        

  7.     
        php_create_dir,             /* per-directory config creator */

        

  8.     
        php_merge_dir,              /* dir merger */

        

  9.     
        NULL,                       /* per-server config creator */

        

  10.     
        NULL,                       /* merge server config */

        

  11.     
        php_commands,               /* command table */

        

  12.     
        php_handlers,               /* handlers */

        

  13.     
        NULL,                       /* filename translation */

        

  14.     
        NULL,                       /* check_user_id */

        

  15.     
        NULL,                       /* check auth */

        

  16.     
        NULL,                       /* check access */

        

  17.     
        NULL,                       /* type_checker */

        

  18.     
        NULL,                       /* fixups */

        

  19.     
        NULL                        /* logger */

        

  20.     
    #if MODULE_MAGIC_NUMBER >= 19970103

        

  21.     
        , NULL                      /* header parser */

        

  22.     
    #endif

        

  23.     
    #if MODULE_MAGIC_NUMBER >= 19970719

        

  24.     
        , NULL                      /* child_init */

        

  25.     
    #endif

        

  26.     
    #if MODULE_MAGIC_NUMBER >= 19970728

        

  27.     
        , php_child_exit_handler        /* child_exit */

        

  28.     
    #endif

        

  29.     
    #if MODULE_MAGIC_NUMBER >= 19970902

        

  30.     
        , NULL                      /* post read-request *

        

  31.     
    #endif

        

  32.     
    };

        

2. apache模块结构的定义:

http-2.216/include/http_config.h
  1.     
    typedef struct module_struct module;

        

  2.     
    struct module_struct {          

        

  3.     
        /** API version, *not* module version; check that module is

        

  4.     
         * compatible with this version of the server.

        

  5.     
         */

        

  6.     
        int version;                

        

  7.     
        /** API minor version. Provides API feature milestones. Not checked

        

  8.     
         *  during module init */  

        

  9.     
        int minor_version;

        

  10.     
        /** Index to this modules structures in config vectors.  default -1*/

        

  11.     
        int module_index;

        

  12.     
     

        

  13.     
        /** The name of the module’s C file */

        

  14.     
        const char *name;

        

  15.     
        /** The handle for the DSO.  Internal use only , default null*/

        

  16.     
        void *dynamic_load_handle;

        

  17.     
                                                                                                         

        

  18.     
        /** A pointer to the next module in the list                                                    

        

  19.     
         *  @defvar module_struct *next , default null*/                                                              

        

  20.     
        struct module_struct *next;                                                                      

        

  21.     
                                                                                                         

        

  22.     
        /** Magic Cookie to identify a module structure;  It’s mainly                                    

        

  23.     
         *  important for the DSO facility (see also mod_so).  */                                        

        

  24.     
        unsigned long magic;                                                                            

        

  25.     
                                                                                                         

        

  26.     
        /** Function to allow MPMs to re-write command line arguments.  This                            

        

  27.     
         *  hook is only available to MPMs.                                                              

        

  28.     
         *  @param The process that the server is running in.                                            

        

  29.     
                                                                   

        

  30.     
         */

        

  31.     
        void (*rewrite_args) (process_rec *process);

        

  32.     
        /** Function to allow all modules to create per directory configuration

        

  33.     
         *  structures.

        

  34.     
         *  @param p The pool to use for all allocations.

        

  35.     
         *  @param dir The directory currently being processed.

        

  36.     
         *  @return The per-directory structure created

        

  37.     
         */

        

  38.     
        void *(*create_dir_config) (apr_pool_t *p, char *dir);

        

  39.     
        /** Function to allow all modules to merge the per directory configuration

        

  40.     
         *  structures for two directories.

        

  41.     
         *  @param p The pool to use for all allocations.

        

  42.     
         *  @param base_conf The directory structure created for the parent directory.

        

  43.     
         *  @param new_conf The directory structure currently being processed.

        

  44.     
         *  @return The new per-directory structure created

        

  45.     
         */                                                                                              

        

  46.     
        void *(*merge_dir_config) (apr_pool_t *p, void *base_conf, void *new_conf);                      

        

  47.     
        /** Function to allow all modules to create per server configuration                            

        

  48.     
         *  structures.                                                                                  

        

  49.     
         *  @param p The pool to use for all allocations.                                                

        

  50.     
         *  @param s The server currently being processed.                                              

        

  51.     
         *  @return The per-server structure created                                                    

        

  52.     
         */                                                                                              

        

  53.     
        void *(*create_server_config) (apr_pool_t *p, server_rec *s);                                    

        

  54.     
        /** Function to allow all modules to merge the per server configuration                          

        

  55.     
         *  structures for two servers.                                                        

        

  56.     
         *  @param p The pool to use for all allocations.

        

  57.     
         *  @param base_conf The directory structure created for the parent directory.

        

  58.     
         *  @param new_conf The directory structure currently being processed.

        

  59.     
         *  @return The new per-directory structure created                                              

        

  60.     
         */                                                                                              

        

  61.     
        void *(*merge_server_config) (apr_pool_t *p, void *base_conf,                                    

        

  62.     
                                      void *new_conf);                                                  

        

  63.     
                                                                                                         

        

  64.     
        /** A command_rec table that describes all of the directives this module                        

        

  65.     
         * defines. */                                                                                  

        

  66.     
        const command_rec *cmds;                                                                        

        

  67.     
                                                                                                         

        

  68.     
        /** A hook to allow modules to hook other points in the request processing.                      

        

  69.     
         *  In this function, modules should call the ap_hook_*() functions to                          

        

  70.     
         *  register an interest in a specific step in processing the current                            

        

  71.     
         *  request.                                                                                    

        

  72.     
         *  @param p the pool to use for all allocations                                                

        

  73.     
         */                                                                                              

        

  74.     
        void (*register_hooks) (apr_pool_t *p);                                                          

        

  75.     
    };                                        

        

3. apache动态加载模块的过程。 动态加载模块时,严格检查模块名,包括httpd.conf中的module name(如: php5_module);对应的apache模块定义所在的文件的文件名(如: mod_php5.c)也是严格检查的。

httpd-2.2.16/modules/mappers/mod_so.c
  1.     
    static const char *load_module(cmd_parms *cmd, void *dummy,

        

  2.     
                                   const char *modname, const char *filename)

        

  3.     
    {

        

  4.     
        apr_dso_handle_t *modhandle;

        

  5.     
        apr_dso_handle_sym_t modsym;

        

  6.     
        module *modp;

        

  7.     
        const char *szModuleFile = ap_server_root_relative(cmd->pool, filename);

        

  8.     
        so_server_conf *sconf;

        

  9.     
        ap_module_symbol_t *modi;

        

  10.     
    //…

        

4. Apache conf 中的配置:
LoadModule php5_module        modules/libphp5.so

5. apr 动态加载模块并获取指定符号(如:php5_module):

apr-1.4.2/dso/beos/dso.c
  1.     
     

        

  2.     
    APR_DECLARE(apr_status_t) apr_dso_sym(apr_dso_handle_sym_t *ressym, apr_dso_handle_t *handle,

        

  3.     
                   const char *symname)

        

  4.     
    {

        

  5.     
        int err;

        

  6.     
     

        

  7.     
        if (symname == NULL)

        

  8.     
            return APR_ESYMNOTFOUND;

        

  9.     
     

        

  10.     
        err = get_image_symbol(handle->handle, symname, B_SYMBOL_TYPE_ANY,

        

  11.     
                 ressym);

        

  12.     
     

        

  13.     
        if(err != B_OK)

        

  14.     
            return APR_ESYMNOTFOUND;

        

  15.     
     

        

  16.     
        return APR_SUCCESS;

        

  17.     
    }

        

6. 一切准备完毕后:
   将模块写入已加载的模块列表中: ap_add_loaded_module(modp, cmd->pool);
   注册相关钩子函数:
     apr_pool_cleanup_register(cmd->pool, modi, unload_module, apr_pool_cleanup_null);
   Finally we need to run the configuration process for the module
     ap_single_module_configure(cmd->pool, cmd->server, modp); 

雁南飞


=========================================


Firefox 中的扩展与插件的区别

插件与扩展是完全不同的东西。

插件(Plugins/Plug-ins):

它通常是第三方应用程序提供给firefox使用的二进制文件。也就是说第三方应用程序把相关功能编译成了二进制的机器指令提供给各类浏览器,方便它们调用。
Firefox需要显示某些自身并不能显示的特定文件类型的时候,就会调用与之相关的第三方应用程序提供给firefox的插件来显示它。插件的作用也在于此。

windows下的firefox插件通常是dll格式,linux下的通常是so格式。

例 如,Adobe提供给firefox的插件“Adobe reader”使其能直接在浏览器里显示网络上的pdf文档,而Adobe提供给firefox 的另一个插件“Adobe shockwave flash”则使其能显示网页中嵌入的flash。Microsoft提供的 “Windows media player firefox plugin”使firefox能播放网页中嵌入的windows媒体(wmv、wma、 asf以及对应的播放列表格式)。

扩展(extensions):

它通常是扩展开发者为了修改或者增强firefox本身的功能而提供的一种打包格式。它通常由包含功能代码的js脚本、包含界面的xul文件以及包含皮肤的css文件和各种图像文件组成。少数特定平台下的扩展可能还会附带一些二进制文件。

扩展的格式都是xpi后缀的。其实是zip格式打包的。

常见的扩展有noscript、adblock plus等。

从形式上讲:插件是已经将代码编译成了机器指令的二进制文件,而扩展是一个源码包。
从作用上讲:插件是为了让firefox能够显示特殊格式的文件而由相应的第三方应用程序(如 windows media player 和 Adobe reader)提供的;而扩展是为了修改或增强firefox本身的功能而由扩展开发者提供的。
从字面意思上讲:插件(plug-ins)的plug是插的意思,一个由外至内的动作;扩展(extensions)的extension是延伸、扩充的意思,一个由内而外的动作。
也就是说:插件是“外部”的,而扩展是“内部”的。

Mozilla文章参考:
http://www.mozilla.org/projects/plugins/
https://developer.mozilla.org/en/Plugins
https://developer.mozilla.org/en/Extensions

快速区分PHP中的函数与结构

PHP中有些类似于函数的东西其实不是函数,如 echo exit  unset  print 等。 怎么可以快速知道这些是函数还是结构呢?

测试脚本:

执行脚本: php test.php

如果被解析为 307 就是函数,否则就可能是结构(或别的什么)。
{当然您也可以直接查手册,手册中基本会提到的}
还可以用function_exists()来测试

—————————————

 

—————————————————-

PHP 一个有意思的报错

下面的一个测试脚本中,B类中在调用A的public方法m时写错了,当做静态的方法来用了,于是A->m()中的$this被解释成了B了。

$this 究竟是谁?  test.php
  1.     
    <?php

        

  2.     
    error_reporting(2047);

        

  3.     
    class A{

        

  4.     
        private $v = 8;

        

  5.     
        public function m() {

        

  6.     
            echo $this->v;

        

  7.     
        }

        

  8.     
    }

        

  9.     
    class B{

        

  10.     
        public function __construct() {

        

  11.     
            A::m();

        

  12.     
        }

        

  13.     
    }

        

  14.     
     

        

  15.     
    new B();

        

  16.     
     

        

  17.     
    exit;

        

出错信息:
PHP Notice:  Undefined property:  B::$v in test.php on line 6

无语…

PHP 中关于资源的释放

问题:
将mysql连接或memcache连接变量直接unset()或赋值为null,连接会立即关闭吗?

测试脚本:

释放资源 test.php
  1.     
    <?php

        

  2.     
    $m = new Memcache();

        

  3.     
    $m->connect("10.55.38.18", 11211);

        

  4.     
    //unset($m);

        

  5.     
    $m = null;

        

  6.     
    sleep(20);

        

执行命令:
strace php test.php
———————————————————–
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3
fcntl64(3, F_GETFL)                     = 0x2 (flags O_RDWR)
fcntl64(3, F_SETFL, O_RDWR|O_NONBLOCK)  = 0
connect(3, {sa_family=AF_INET, sin_port=htons(11211), sin_addr=inet_addr("10.55.38.18")}, 16) = -1 EINPROGRESS (Operation now in progress)
poll([{fd=3, events=POLLIN|POLLOUT|POLLERR|POLLHUP}], 1, 1000) = 1 ([{fd=3, revents=POLLOUT}])
getsockopt(3, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
fcntl64(3, F_SETFL, O_RDWR)             = 0
close(3)                                = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigaction(SIGCHLD, NULL, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
nanosleep({20, 0}, 0xbfa1e114)  
————————————————————————-

测试结果:
对于一个资源,使用unset()或 设置为null,都会立即关闭连接的。
大概关闭连接的操作是在引用变为0时触发的,具体实现还没看呢。

PHP的位操作

左移操作 ( << )

PHP 左移
  1.     
    <?php

        

  2.     
     

        

  3.     
    $i = 1;

        

  4.     
    for ($j = 0; $j < 64; $j++) {

        

  5.     
        $i = $i << 1;

        

  6.     
        echo decbin($i)."\n";

        

  7.     
    }

        

  8.     
     

        

  9.     
    exit;

        

  10.     
     

        

结果:
10
100
1000

10000000000000000000000000000000 (32位)
0
0

0

————————————————————————-
特点:

  1. 左移操作,有变补零
  2. 左移不循环