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)

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
