Home > Backend Development > PHP Tutorial > Use C language to extend PHP and write PHP extension dll_PHP tutorial

Use C language to extend PHP and write PHP extension dll_PHP tutorial

WBOY
Release: 2016-07-13 17:47:19
Original
852 people have browsed it

I have written a PHP extension DLL before. It was an extension implemented by using the COM port of the calling system, and it cannot be truly integrated with PHP. On a whim, I studied the source code of PHP, found some information on the Internet, and tried to write an extension DLL myself. The test was no problem. The specific extension methods are recorded below:

1. First download the php source code from the www.php.net website. Here we take the php-5.2.17 version as an example. After downloading, extract it to the root directory of the E: disk (the directory can be determined by yourself).

2. Download and install VC++ 6.0. Because the PHP source code is written using version 6.0, there will be no accidents when compiling with this version. Other versions have not been tested.

3. Add the absolute path of Microsoft Visual StudioCommonMSDev98Bin in the VC++ 6.0 installation directory to the system environment variable.

4. Enter the E:php-5.2.17ext directory, copy the skeleton folder, and rename it to the name of the extension you want to develop, in this case "myfun".

5. Rename skeleton.c to myfun.c and skeleton.dsp to myfun.dsp

6. Edit the three files php_skeleton.h, myfun.c, and myfun.dsp in the myfun directory, and replace all extname in the content with myfun and EXTNAME with MYFUN. (Be sure to be strictly case-sensitive)

Now let’s enter the coding stage:

7. Open the php_skeleton.h file (header file), find PHP_FUNCTION(confirm_myfun_compiled);, in PHP_FUNCTION(confirm_myfun_compiled);, write PHP_FUNCTION(mb_MessageBox); below, declare an mb_MessageBox function, the function of this function is only to output js and pop up an alert Message box, for testing.

8. Define the function entrance below, open the myfun.c file, find PHP_FE(confirm_myfun_compiled,NULL); and write PHP_FE(mb_MessageBox,NULL) below. Note here that PHP_FE is a defined macro, so there is no need to add quotes after it.

9. Write the entity part of the function at the end of myfun.c:

	
Copy after login

PHP_FUNCTION(mb_MessageBox)

{

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, "<script>alert('%s')</script>",arg); //This is the entered js code

RETURN_STRINGL(strg, len, 0);

}

Now that the code writing is complete, let’s start compiling:

10. Start->Run, enter CMD to open the command line window.

11. Enter the directory of myfun, enter msdev myfun.dsp /MAKE "myfun - Win32 Release_TS", and press Enter to compile.

12. If there are no errors, a Release_TS folder will be generated under E:php-5.2.17, in which you can find the php_myfun.dll file.

At this point, the extension dll development is completed. Let’s test it in php:

13. Copy php_myfun.dll to the ext folder in the original php directory.

14. Open the php.ini file and add the current dll extension extension=php_myfun.dll

15. Restart IIS or apache, create a new file in the website directory, and enter the following content:

echo mb_MessageBox("Test PHP extension DLL by Ma Ben");

?>

	
Copy after login

Browse to see the effect, four pictures are attached below:

Add extension:

PHP code:

Final effect:

Extended information in phpinfo:

At this point, php extension development has been completed

Author Ma Ben

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478514.htmlTechArticleI have written a PHP extension DLL before. It was an extension implemented by using the COM port of the calling system, and it cannot be used with PHP. Fusion. On a whim, I studied the source code of PHP and found some information online...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template