PHP扩展开发-03-读取php.ini中的配置

@author niujiaming<[email protected]>


本节中我们读取一下php.ini文件中的配置。其实,读取php.ini配置的方式挺多,本次我们只讲一种,如果有兴趣的话,大家可以各自研究一下。

废话少说,我们更改一下之前的讲解方式,本次我们直接上代码。(其实是因为php扩展读取配置文件太简单了)

(假设我们建立了一个ini_read的扩展)

php_ini_read.h的改动如下:(改动的地方我用#####################标出来了################)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
   +----------------------------------------------------------------------+
   | PHP Version 5                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2012 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 3.01 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.php.net/license/3_01.txt                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   [email protected] so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Author:                                                              |
   +----------------------------------------------------------------------+
*/
     
/* $Id$ */
     
#ifndef PHP_INI_READ_H
#define PHP_INI_READ_H
     
extern zend_module_entry ini_read_module_entry;
#define phpext_ini_read_ptr &ini_read_module_entry
     
#ifdef PHP_WIN32
#   define PHP_INI_READ_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
#   define PHP_INI_READ_API __attribute__ ((visibility( "default" )))
# else
#   define PHP_INI_READ_API
# endif
     
#ifdef ZTS
# include  "TSRM.h"
# endif
     
PHP_MINIT_FUNCTION(ini_read);
PHP_MSHUTDOWN_FUNCTION(ini_read);
PHP_RINIT_FUNCTION(ini_read);
PHP_RSHUTDOWN_FUNCTION(ini_read);
PHP_MINFO_FUNCTION(ini_read);
     
/* 
     Declare any global variables you may need between the BEGIN
     and END macros here:     
     
ZEND_BEGIN_MODULE_GLOBALS(ini_read)
     long  global_value;
     char *global_string;
ZEND_END_MODULE_GLOBALS(ini_read)
*/
     
/* In every utility function you add that needs to use variables 
    in php_ini_read_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 INI_READ_G(variable).  You are 
    encouraged to rename these macros something shorter, see
    examples in any other php module directory.
*/
     
#ifdef ZTS
#define INI_READ_G(v) TSRMG(ini_read_globals_id, zend_ini_read_globals *, v)
# else
#define INI_READ_G(v) (ini_read_globals.v)
# endif
     
# endif   /* PHP_INI_READ_H */
     
     
/*
  * Local variables:
  * tab-width: 4
  * c-basic-offset: 4
  * End:
  * vim600: noet sw=4 ts=4 fdm=marker
  * vim<600: noet sw=4 ts=4
  */
     
PHP_FUNCTION(helloworld);#####################



ini_read.c的改动如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
   +----------------------------------------------------------------------+
   | PHP Version 5                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2012 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 3.01 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.php.net/license/3_01.txt                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   [email protected] so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Author:                                                              |
   +----------------------------------------------------------------------+
*/
     
/* $Id$ */
     
#ifdef HAVE_CONFIG_H
# include  "config.h"
# endif
     
# include  "php.h"
# include  "php_ini.h"
# include  "ext/standard/info.h"
# include  "php_ini_read.h"
     
/* If you declare any globals in php_ini_read.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(ini_read)
*/
     
/* True global resources - no need for thread safety here */
static  int le_ini_read;
     
/* {{{ ini_read_functions[]
  *
  * Every user visible function must have an entry in ini_read_functions[].
  */
const  zend_function_entry ini_read_functions[] = {
     PHP_FE(helloworld,  NULL)        /* For testing, remove later. */
     {NULL, NULL, NULL}
};
/* }}} */
     
/* {{{ ini_read_module_entry
  */
zend_module_entry ini_read_module_entry = {
# if  ZEND_MODULE_API_NO >= 20010901
     STANDARD_MODULE_HEADER,
# endif
     "ini_read" ,
     ini_read_functions,
     PHP_MINIT(ini_read),
     PHP_MSHUTDOWN(ini_read),
     NULL,#####################
     NULL,#####################
     NULL,#####################
# if  ZEND_MODULE_API_NO >= 20010901
     "0.1" /* Replace with version number for your extension */
# endif
     STANDARD_MODULE_PROPERTIES
};
/* }}} */
     
#ifdef COMPILE_DL_INI_READ
ZEND_GET_MODULE(ini_read)
# endif
     
/* {{{ PHP_INI
  */
PHP_INI_BEGIN()#####################
     PHP_INI_ENTRY( "ini_read.helloworld" "foobar" , PHP_INI_ALL, NULL)#####################
PHP_INI_END()#####################
/* }}} */
     
/* {{{ php_ini_read_init_globals
  */
/* Uncomment this function if you have INI entries
static void php_ini_read_init_globals(zend_ini_read_globals *ini_read_globals)
{
     ini_read_globals->global_value = 0;
     ini_read_globals->global_string = NULL;
}
*/
/* }}} */
     
/* {{{ PHP_MINIT_FUNCTION
  */
PHP_MINIT_FUNCTION(ini_read)
{
     REGISTER_INI_ENTRIES();#####################
     return  SUCCESS;
}
/* }}} */
     
/* {{{ PHP_MSHUTDOWN_FUNCTION
  */
PHP_MSHUTDOWN_FUNCTION(ini_read)
{
     UNREGISTER_INI_ENTRIES();#####################
     return  SUCCESS;
}
/* }}} */
     
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
  */
PHP_RINIT_FUNCTION(ini_read)
{
     return  SUCCESS;
}
/* }}} */
     
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
  */
PHP_RSHUTDOWN_FUNCTION(ini_read)
{
     return  SUCCESS;
}
/* }}} */
     
/* {{{ PHP_MINFO_FUNCTION
  */
PHP_MINFO_FUNCTION(ini_read)
{
     php_info_print_table_start();
     php_info_print_table_header(2,  "ini_read support" "enabled" );
     php_info_print_table_end();
     
     /* Remove comments if you have entries in php.ini
     DISPLAY_INI_ENTRIES();
     */
}
/* }}} */
     
/* }}} */
/* 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.
*/
     
     
/*
  * Local variables:
  * tab-width: 4
  * c-basic-offset: 4
  * End:
  * vim600: noet sw=4 ts=4 fdm=marker
  * vim<600: noet sw=4 ts=4
  */
     
PHP_FUNCTION(helloworld)
{
     RETURN_STRING(INI_STR( "ini_read.helloworld" ), 1);#####################
}

本节内容很简单,就不多说了,有问题大家可以联系我QQ 947847775


你可能感兴趣的:(PHP,PHP,PHP,php扩展开发)