编写PHP扩展之提高篇一

zlb@ubuntu:~/php-5.2.6/ext$ sudo vim crypt.def

    1.crypt.def
    string encrypt(string str)
    string decrypt(string str)

zlb@ubuntu:~/php-5.2.6/ext$ sudo ./ext_skel --extname=crypt --proto=crypt.def

zlb@ubuntu:~/php-5.2.6/ext/crypt$ sudo vim config.m4
//打开16,18行对于.configure --enalbe的支持

zlb@ubuntu:~/php-5.2.6/ext/crypt$ sudo vim crypt.c
PHP_FUNCTION(encrypt)
{
        char *str = NULL;
        int argc = ZEND_NUM_ARGS();
        int str_len;
        int i=0;
        char ch;

        if (zend_parse_parameters(argc TSRMLS_CC, "s", &str, &str_len) == FAILURE)
        {return;}
        while(ch=str[i])
        {
                str[i] +=3;
                ch = str[++i];
        }

        RETURN_STRINGL(str,i,1);
//php_error(E_WARNING, "encrypt: not yet implemented");
}
PHP_FUNCTION(decrypt)
{
        char *str = NULL;
        int argc = ZEND_NUM_ARGS();
        int str_len;
        int i=0;
        char ch;
        if (zend_parse_parameters(argc TSRMLS_CC, "s", &str, &str_len) == FAILURE)
        {return;}

        while(ch=str[i])
        {
                str[i]-=3;
                ch=str[++i];
        }
        RETURN_STRINGL(str,i,1);
        php_error(E_WARNING, "decrypt: not yet implemented");
}

zlb@ubuntu:~/php-5.2.6/ext/crypt$ sudo phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519
zlb@ubuntu:~/php-5.2.6/ext/crypt$
zlb@ubuntu:~/php-5.2.6/ext/crypt$ sudo ./configure --help|grep cry
   --enable-crypt           Enable crypt support

zlb@ubuntu:~/php-5.2.6/ext/crypt$ sudo ./configure ...
zlb@ubuntu:~/php-5.2.6/ext/crypt$ sudo make ...
zlb@ubuntu:~/php-5.2.6/ext/crypt$ sudo make install ...
zlb@ubuntu:~/php-5.2.6/ext/crypt$ php crypt.php
Functions available in the test extension:
confirm_crypt_compiled
encrypt
decrypt

Congratulations! You have successfully modified ext/crypt/config.m4. Module crypt is now compiled into PHP.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值