PHP Extension Development-ini Configuration

Source: Internet
Author: User
Tags strcmp zend

php.ini file is used to save the various extension configuration of the file, each extension more or less need to have a customized configuration, INI file is a good way to save the configuration, we see how in their own extension, using the configuration function to INI

Creating configuration entries for INI
#include"php_ini.h"
INI configuration is created similar to global variables, creating a struct from a macro definition to save the INI configuration item
Parameter description:
1, Configuration name
2, configuration value
3, scope
4, modify the callback function, can be Nullphp_ini_begin () php_ini_entry ("myext.ini_string","I am a string of ini", Php_ini_all,myext_example_ini_callback) Php_ini_entry ("Myext.ini_long"," -", Php_ini_all,null) Php_ini_end ()
Add Php_mshutdown (myext) function Zend_module_entry myext_module_entry at the entrance= {#ifZend_module_api_no >= 20010901Standard_module_header,#endif "Myext",//extension nameMyext_functions,//zend_function_entry myext_functions defined function extension variablesPhp_minit (Myext),//minit_functionPhp_mshutdown (Myext),//Php_mshutdown (myext),//mshutdown_functionPhp_rinit (Myext),//rinit_functionPhp_rshutdown (Myext),//rshutdown_functionPhp_minfo (Myext),//minfo_function#ifZend_module_api_no >= 20010901Php_myext_version,#endifstandard_module_properties}; Php_minit_function (myext) {//registering the INI configurationregister_ini_entries ();returnSUCCESS;} Php_mshutdown_function (Myext) {
Destroy INI configuration unregister_ini_entries (); returnSUCCESS;}

Callback functions in the Php_ini_entry

ZEND_INI_MH (Myext_example_ini_callback) {
if (New_value_length = = 0 | | strcmp (new_value, "no_allow_string") = = 0) {
return FAILURE;
}
return SUCCESS;
}

/*

Var_dump (Ini_get (' myext.ini_string '));
Ini_set (' myext.ini_string ', ' I am the new INI string ');
Var_dump (Ini_get (' myext.ini_string '));
Var_dump (Ini_get (' Myext.ini_long '));
Ini_set (' Myext.ini_long ', ' 101 ');
Var_dump (Ini_get (' Myext.ini_long '));

String (21) "I am the strings of INI"
String (24) "I am the new ini string"
String (3) "100"
String (3) "101"

*/

Let's take a look at the problem of the third parameter scope in the Php_ini_entry function just now

Parameters Describe
Php_ini_perdir Directives can be modified in php.ini, httpd.conf, or. htaccess files
Php_ini_system Directives can be modified in php.ini and httpd.conf files
Php_ini_user Directives can be modified in user scripts
Php_ini_all Instructions can be modified anywhere

How can I access the configuration of INI in an extension?

Php_function (Myext_example_ini);//php_myext.cPhp_fe (Myext_example_ini, NULL)//one line per function, the first parameter is the same as the name of Php_function (name)php_function (myext_example_ini) {Const Char* ini_string = INI_STR ("myext.ini_string");//Get Current valueLongIni_long = Ini_int ("Myext.ini_long"); php_printf ("ini_string =%s\n", ini_string); php_printf ("Ini_long =%ld\n", Ini_long); Const Char* orig_ini_string = INI_ORIG_STR ("myext.ini_string");//Get default valuesLongOrig_ini_long = Ini_orig_int ("Myext.ini_long"); php_printf ("orig_ini_string =%s\n", orig_ini_string); php_printf ("Orig_ini_long =%ld\n", Orig_ini_long);}

/*

Myext_example_ini ();
Ini_set (' myext.ini_string ', ' I am the new INI string ');
Ini_set (' Myext.ini_long ', ' 101 ');
Myext_example_ini ();

Ini_string = I am a string of ini
Ini_long = 100
Orig_ini_string = I am a string of ini
Orig_ini_long = 100

Above is the modification before the following is modified

ini_string = I am the new INI string
Ini_long = 101
Orig_ini_string = I am a string of ini
Orig_ini_long = 100


*/

INI has four types of configuration items, all ini_str and ini_orig_str have four different types of combinations

#define Ini_int (name) zend_ini_long (name), sizeof (name), 0)#define Ini_flt (name) zend_ini_double ((name), sizeof ( Name), 0)#define ini_str (name) zend_ini_string_ex (name), sizeof (name), 0, NULL)#define Ini_bool ( Name) ((zend_bool) ini_int (name))#define ini_orig_int (name)  Zend_ini_long ((name), sizeof (name), 1)  #define Ini_orig_flt (name)  zend_ini_double (name), sizeof (name), 1)#define ini_orig_str (name)  zend_ini_string (name), sizeof (name), 1)#define Ini_orig_bool (name) ((zend_bool) ini_orig_int (name) )

In the Php_ini_entry function, the fourth parameter is a callback function, when the INI configuration item is modified, this function will be called, the function is that you can set the value of filtering, do not meet the requirements can return Failer to make this modification does not take effect.

 ZEND_INI_MH (myext_example_ini_callback) {//This function needs to be defined with  zend_ini_mh, with ZEND _function is not the same 
 if  (new_value_length = = 0  | | strcmp ( New_value, " no_allow_string  " ) = = 0   {
return FAILURE; This modification will not take effect
}
return SUCCESS;
}

#define ZEND_INI_MH (name) int name (Zend_ini_entry *entry, Char *new_value, uint new_value_length, void *mh_arg1, void *mh_arg2, void *mh_arg3, int stage tsrmls_dc)
//Macro expansion There are many Number, which we will use is to modify the value of the New_value (string content) and New_value_length (string length), the rest of the parameters, the kernel will help us to add, can not need to care about

We often need to query the current valid INI configuration item through Phpinfo or php-i, so when we develop the extension, it is best to write the current configuration information into the Php_minfo_function function, combining the knowledge of the MINFO part mentioned earlier, And just to read the knowledge of the INI configuration, we can implement this function ourselves, fortunately, Zend's kernel and help us to consider, all we can do with the following code.

php_minfo_function (myext) {    php_info_print_table_start ();    Php_info_print_table_row (2"version", php_myext_version);    Php_info_print_table_row (2"writer""zhangxiaomin ");    Php_info_print_table_end ();    Display_ini_entries (); // just add this line to it/}

/*
Php5.6-i//Command line

Myext

Version = 1.0
writer = Zhangxiaomin

Directive = Local Value = = Master value
Myext.ini_long = 100
myext.ini_string = I am the ini string = = I am the INI string


*/

Ba

PHP Extension Development-ini Configuration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.