2008年11月24日星期一

PHP 命令行 fork 多进程处理

很多人认为 PHP 没有多线程和多进程处理能力,其实 PHP 有多种运行模式。作为在作 web 应用的时候通常运行于 cgi 或者 web api 模块模式,web 服务器本身多进程或线程方式运行。而 PHP 在 cli 命令行模式下则有能力使用 fork 产生子进程(Windows 环境不可用)。本文将论述这种情况。

PHP 的进程控制支持实现了 Unix 类型的进程创建、程序执行、信号处理和进程终止。在 web 服务环境中不应该启用进程控制,如果在 web 服务环境中使用进程控制功能可能导致不可预料的结果。

int pcntl_fork(void)
fork 的意思是叉子,分岔路口。同样程序在这里分叉 ^_^。fork() 函数执行后,系统会将当前进程复制一份,子进程和父进程的差别只有 PID(进程号) 和 PPID(父进程号)不同。也就是说,这一点开始除了PID和PPID,子进程和父进程有同样的数据,也正在同样的控制流程中。所以注意一点,如果父进程在循环中fork(), 那么子进程也在循环中。
分叉点在这儿,fork()返回值:
  • 父进程从 fork() 得到的返回值是子进程的PID, 
  • 子进程从 fork() 得到的返回值是 0,
  • 如果父进程得到返回值是 -1 表示有错误发生,比如系统资源耗尽导致无法创建子进程。
因为创建的是进程,而每个进程的空间是相互独立的,所以在fork()这一刻子进程和父进程拥有相同的数据和状态,随后各自独立运行,改变的数据和状态不会相互影响。
$pid = pcntl_fork();
if ($pid == -1) {
     die('could not fork');
} else if ($pid) {
     // we are the parent
     pcntl_wait($status); //Protect against Zombie children
} else {
     // we are the child
}


[未完待续……]

2008年8月18日星期一

Type Casting in PHP

PHP 中的类型声明


允许的声明有:


  • (int), (integer) - cast to integer

  • (bool), (boolean) - cast to boolean

  • (float), (double), (real) - cast to float

  • (string) - cast to string

  • (binary) - cast to binary string (PHP 6)

  • (array) - cast to array

  • (object) - cast to object

  • (unset) - cast to NULL (PHP 5)


一些有用(有趣)的特性:
(bool),(boolean), 当然,这个通常不用去显示声明,在需要的时候自动会转换。这里要说的是值如何转换:

  • 布尔值FALSE本身

  • 整数 0 (零)

  • 浮点数 0.0 (零)

  • 空字符串 '' 和 字符串 '0' (零)

  • 空数组(没有任何元素的数组)

  • 没有任何成员的对象(仅 PHP 4)

  • 特殊类型 NULL (包括 unset 的变量)

  • 从空标记创建的 SimpleXML 对象 (也就是不会有任何节点)


除此以外的任何值都是 TRUE

(string), 用于对象时,会调用对象的 __toString() 魔术方法(PHP5.2+可以正确调用, 更早版本不会)
(object), 用于数组时,可以把数组转化为对象,键作为对象的属性

$foo = array('bar' => 'foobar');
var_dump((object)$foo);

//输出
object(stdClass)#1 (1) {
["bar"]=> string(6) "foobar"
}


(array), 作用于对象时

$foo = new stdClass();
$foo->bar = 'foobar';
var_dump((array)$foo);

//输出
array(1) {
["bar"]=> string(6) "foobar"
}

2008年7月24日星期四

Obfuscate e-mail addresses

防止e-mail地址被垃圾邮件收集的方法



原文的作者 Silvan Mühlemann 花了一年半的时间收集数据比较九种不同的方法。

以下是3中有效的方法:

1. 用 CSS 改变文字方向

<style type="text/css">
span.codedirection { unicode-bidi:bidi-override; direction: rtl; }
</style>
<p><span class="codedirection">moc.liamg@77uohcnek</span></p>


2. 用 CSS display:none

<style type="text/css">
p span.displaynone { display:none; }
</style>
<p>kenchou77@<span class="displaynone">null</span>gmail.com</p>
或者直接 inline css
<p>kenchou77@<span style="display:none">null</span>gmail.com</p>


3. ROT13 加密

ROT13 简单的位移13个字母,加密和解密方法相同。


<script type="text/javascript">
document.write("xrapubh77@tznvy.pbz".replace(/[a-zA-Z]/g, function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);}));
</script>


原文地址:http://techblog.tilllate.com/2008/07/20/ten-methods-to-obfuscate-e-mail-addresses-compared/

2008年3月18日星期二

Zend Framework 1.5 is now available!

We couldn't be happier to announce that Zend Framework 1.5 is now
available
from the Zend Framework download site!
http://framework.zend.com/download
An overview of new features in 1.5:
* New Zend_Form component with support for AJAX-enabled form elements
* New action and view helpers for automating and facilitating AJAX
requests and alternate response formats
* Infocard, OpenID, and LDAP authentication adapters
* Support for complex Lucene searches, including fuzzy, date-range, and
wildcard queries
* Support for Lucene 2.1 index file format
* Partial, Placeholder, Action, and Header view helpers for advanced
view composition and rendering
* New Zend_Layout component for automating and facilitating site layouts
* UTF-8 support for PDF documents
* New Nirvanix, Technorati, and SlideShare web services
There are a lot of people to thank, since there are a lot of people who
worked very hard to make this happen. First of all, thanks to all the
Zend Framework contributors who helped in all kinds of ways to make this
a truly great, high-quality release. I would personally like to thank
the Zend Framework team here at Zend for working the long hours this
weekend to make sure everything came together on Monday morning. There
were also some very selfless souls in marketing who stayed up late over
the last few days to make sure the site was at its best in both style
and substance for the big day. Finally we'd like to thank all the ZF
users out there who inspire us to continue improving Zend Framework and
who never fail to keep us on our toes. :) We hope this release not only
lives up to your high expectations but goes beyond them.
Now we can finally say that we wholeheartedly recommend the 1.5 release
for production use.
Enjoy ZF 1.5!

,Wil