linux下php扩展(phpext)开发记录

18 篇文章 0 订阅

公司需要对通行证用户资料的教检函数封包(防止服务器沦陷,用户资料被解密),需要开发一个扩展函数,

  usernamecookie中的username

  hashkeycookie中的 PName

  开发php ext: 函数名为csdn_verify_auth,参数是usernamehashkey ,(算法机密文件略)

  第一次写PHP扩展,搜索资料到编写测试用了一天半完成,记录一下吧,以后如果需要做的话大部分时间只能浪费在C算法上。

  1.先down一个php下来,编译安装:(编译环境只做测试扩展用)

  wget http://cn2.php.net/get/php-5.2.13.tar.gz/from/cn.php.net/mirror

  tar zxvf php-5.2.13.tar.gz

  cd php-5.2.13

  ./configure --prefix=/csdn_verify_auth/php --with-config-file-path=/csdn_verify_auth/php/etc

  make ZEND_EXTRA_LIBS='-liconv'

  make install

  cp php.ini-dist /csdn_verify_auth/php/etc/php.ini

  2.修改一下vi /csdn_verify_auth/php/etc/php.ini中的extension_dir = “./”

  修改为extension_dir = "/csdn_verify_auth/php/lib/php/extensions/no-debug-non-zts-20060613"

  并在此行后增加以下,然后保存:

  extension = "csdn_verify_auth.so"

  csdn_verify_auth改为你开发的扩展的名字

  3.开始创建扩展项目

  进入源码目录

  cd /csdn_verify_auth/php-5.2.13/ext/

  ./ext_skel --extname=csdn_verify_auth

  创建名字为csdn_verify_auth的项目,最终会生成csdn_verify_auth.so

  4.更改配置和扩展程序开发

  vi ext/csdn_verify_auth/config.m4

  根据你自己的选择将

  dnl PHP_ARG_WITH(csdn_verify_auth, for csdn_verify_auth support,

  dnl Make sure that the comment is aligned:

  dnl [ --with-csdn_verify_auth Include csdn_verify_auth support])

  去掉dnl

  或者将

  dnl PHP_ARG_ENABLE(csdn_verify_auth, whether to enable csdn_verify_auth support,

  dnl Make sure that the comment is aligned:

  dnl [ --enable-csdn_verify_auth Enable csdn_verify_auth support])

  去掉dnl

  vi ext/csdn_verify_auth/php_csdn_verify_auth.h

  将

  PHP_FUNCTION(confirm_csdn_verify_auth_compiled); /* For testing, remove later. */

  更改为

  PHP_FUNCTION(csdn_verify_auth);

  vi ext/csdn_verify_auth/csdn_verify_auth.c

  将

  zend_function_entry php5cpp_functions[] = {

  PHP_FE(confirm_csdn_verify_auth_compiled, NULL) /* For testing, remove later. */

  {NULL, NULL, NULL} /* Must be the last line in php5cpp_functions[] */

  };

  更改为

  zend_function_entry php5cpp_functions[] = {

  PHP_FE(csdn_verify_auth, NULL)

  {NULL, NULL, NULL} /* Must be the last line in php5cpp_functions[] */

  };

  在最后添加:

  PHP_FUNCTION(csdn_verify_auth)

  {

  zend_printf("hello world/n");

  }

  5.编译生成so文件

  cd /csdn_verify_auth/php-5.2.13/ext/csdn_verify_auth/

  /csdn_verify_auth/php/bin/phpize

  ./configure --with-php-config=/csdn_verify_auth/php/bin/php-config

  make

  mv /csdn_verify_auth/php-5.2.13/ext/csdn_verify_auth/modules/csdn_verify_auth.so /csdn_verify_auth/php/lib/php/extensions/no-debug-non-zts-20060613

  cd ../../../

  6.测试扩展

  vi /csdn_verify_auth/hello.php

  csdn_verify_auth();

  ?>

  /csdn_verify_auth/php/bin/php hello.php

  hello world.

  OK

  主要用到的api就那么几个:

  ZEND_NUM_ARGS()、zend_parse_parameters(args TSRMLS_CC, “ss”, &username, &username_length, &hash, &hash_length) 参数接收系列

  RETURN_FALSE。。RETURN_STRINGL(s, l, dup)等返回系列函数

  …………………

  其他的基本就是C了,再次感叹下,学好C语言,走到哪都不怕。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值