PHP扩展开发01:第一个扩展【转】
我们先假设业务场景,是需要有这么一个扩展,提供一个叫ccvita_string的函数,他的主要作用是返回一段字符。(这个业务场景实在太假,大家就这么看看吧)对应的PHP代码可能是这样:
<span style="color: #0000ff;">function</span> ccvita_string(<span style="color: #800080;">$str</span><span style="color: #000000;">){ </span><span style="color: #800080;">$result</span> = '<a href="'.<span%20style=" color:>$str.'">Link</a>'<span style="color: #000000;">; </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">;}</span>
第一步,生成代码
PHP为了扩展开发的方便,提供了一个类似代码生成器的工具ext_skel,具体可以参见说明。
首先我们创建一个文件ccvita.skel,它的内容为
<span style="color: #0000ff;">string</span> ccvita_string(<span style="color: #0000ff;">string</span> str)
就是告诉ext_skel这个东西,我们要做的扩展里面有个函数叫ccvita_string。然后执行
cd MooENV/src/php-<span style="color: #800080;">5.3</span>.<span style="color: #800080;">8</span>/ext/<span style="color: #000000;">.</span>/ext_skel --extname=ccvita --proto=<span style="color: #000000;">ccvita.skelcd ccvita</span>/
这时候,ccvita这个扩展的代码框架就已经出来了。
第二步,修改配置
然后修改config.m4文件将10、11、12三行最前面的dnl删除掉,就是将
dnl PHP_ARG_WITH(ccvita, for ccvita support,<span style="color: #000000;">dnl Make sure that the comment is aligned:dnl </span><span style="color: #800000; font-weight: bold;">[</span><span style="color: #800000;"> --with-ccvita Include ccvita support</span><span style="color: #800000; font-weight: bold;">]</span>)
修改为
PHP_ARG_WITH(ccvita, for ccvita support,<span style="color: #000000;">Make sure that the comment is aligned:</span><span style="color: #800000; font-weight: bold;">[</span><span style="color: #800000;"> --with-ccvita Include ccvita support</span><span style="color: #800000; font-weight: bold;">]</span>)
第三步,实现功能
修改源码ccvita.c文件
找到将ccvita_string这个函数修改为
<span style="color: #000000;">PHP_FUNCTION(ccvita_string){ </span><span style="color: #0000ff;">char</span> *str =<span style="color: #000000;"> NULL; </span><span style="color: #0000ff;">int</span> argc =<span style="color: #000000;"> ZEND_NUM_ARGS(); </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> str_len; </span><span style="color: #0000ff;">char</span> *<span style="color: #000000;">result; </span><span style="color: #0000ff;">if</span> (zend_parse_parameters(argc TSRMLS_CC, <span style="color: #800000;">"</span><span style="color: #800000;">s</span><span style="color: #800000;">"</span>, &str, &str_len) ==<span style="color: #000000;"> FAILURE) </span><span style="color: #0000ff;">return</span><span style="color: #000000;">; str_len </span>= spprintf(&result, <span style="color: #800080;">0</span>, <span style="color: #800000;">"</span><span style="color: #800000;"><a href="%5C%22%.78s%5C%22">Link</a></span><span style="color: #800000;">"</span><span style="color: #000000;">, str); RETURN_STRINGL(result, str_len, </span><span style="color: #800080;">0</span><span style="color: #000000;">); }</span>
第四步,编译扩展
保存后,开始编译
/usr/local/php/bin/<span style="color: #000000;">phpize.</span>/configure --with-php-config=/usr/local/php/bin/php-<span style="color: #000000;">config</span><span style="color: #0000ff;">make</span>
第五步,添加扩展
这时候,一切顺利的话,该扩展已经在modules/ccvita.so这个位置了。下面就是将这个扩展加入到PHP中去,让我们PHP程序可以调用到。
<span style="color: #0000ff;">cp</span> modules/ccvita.so /usr/local/php/ext/<span style="color: #000000;">vim </span>/usr/local/php/etc/<span style="color: #000000;">php.iniextension</span>=/usr/local/php/ext/<span style="color: #000000;">ccvita.so #在php.ini文件最后增加这一行service php</span>-<span style="color: #000000;">fpm restart #重启PHP服务</span><span style="color: #0000ff;">cp</span> ccvita.php /data/www/wwwroot/default/
相关代码,可以在我的github上clone出来,地址:https://github.com/KimiChen/PHP-EXT接下来就可以访问ccvita.php这个文件,测试扩展了。下一篇,将会涉及一些更深入的内容。
转自博客(http://www.ccvita.com/496.html)

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考虑使用AttActAcks.s.s.4)

会话再生是指在用户进行敏感操作时生成新会话ID并使旧ID失效,以防会话固定攻击。实现步骤包括:1.检测敏感操作,2.生成新会话ID,3.销毁旧会话ID,4.更新用户端会话信息。

PHP会话对应用性能有显着影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

phpientifiesauser'ssessionusessessionSessionCookiesAndSessionIds.1)whiwSession_start()被称为,phpgeneratesainiquesesesessionIdStoredInacookInAcookInamedInAcienamedphpsessidontheuser'sbrowser'sbrowser.2)thisIdAllowSphptptpptpptpptpptortoreTessessionDataAfromtheserverMtheserver。

PHP会话的安全可以通过以下措施实现:1.使用session_regenerate_id()在用户登录或重要操作时重新生成会话ID。2.通过HTTPS协议加密传输会话ID。3.使用session_save_path()指定安全目录存储会话数据,并正确设置权限。

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver CS6
视觉化网页开发工具

Dreamweaver Mac版
视觉化网页开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版