<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yaron's Blog &#187; PHP高级应用</title>
	<atom:link href="http://yaron.org.cn/archives/category/web-developement/use-of-php-do-something/feed" rel="self" type="application/rss+xml" />
	<link>http://yaron.org.cn</link>
	<description>About PHP MYSQL JS WEB FreeBSD etc.</description>
	<lastBuildDate>Sat, 31 Jul 2010 13:54:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Askeet教程所遇问题备忘</title>
		<link>http://yaron.org.cn/archives/128</link>
		<comments>http://yaron.org.cn/archives/128#comments</comments>
		<pubDate>Sun, 26 Apr 2009 14:35:41 +0000</pubDate>
		<dc:creator>Yaron</dc:creator>
				<category><![CDATA[PHP高级应用]]></category>

		<guid isPermaLink="false">http://yaron.org.cn/archives/128</guid>
		<description><![CDATA[&#160;&#160;&#160;&#160;&#160; 在根据Askeet教程学习Symfony时，有遇到这样一个问题：Unable to find path for class &#34;francoisPeer&#34;，笔者经过仔细研究，终于发现问题所在。
 
错误代码：
D:\workspace\askeet\batch&#62;php load_data.php      PHP Fatal error:&#160; Uncaught exception<a href="http://yaron.org.cn/archives/128" class="searchmore">Read the Rest...</a><div class="clr"></div>]]></description>
			<content:encoded><![CDATA[<p>&#160;&#160;&#160;&#160;&#160; 在根据Askeet教程学习Symfony时，有遇到这样一个问题：Unable to find path for class &quot;francoisPeer&quot;，笔者经过仔细研究，终于发现问题所在。</p>
<p> <span id="more-128"></span>
<p>错误代码：</p>
<blockquote><p>D:\workspace\askeet\batch&gt;php load_data.php      <br />PHP Fatal error:&#160; Uncaught exception &#8217;sfException&#8217; with message &#8216;Unable to find path for class &quot;francoisPeer&quot;.&#8217; in D:\workspace\askeet\lib\vender\symfony\lib\ad on\propel\sfPropelData.class.php:206       <br />Stack trace:       <br />#0 D:\workspace\askeet\lib\vender\symfony\lib\addon\propel\sfPropelData.class.ph       <br />p(46): sfPropelData-&gt;doDeleteCurrentData(Array)       <br />#1 D:\workspace\askeet\batch\load_data.php(24): sfPropelData-&gt;loadData(&#8216;D:\works       <br />pace\as&#8230;&#8217;)       <br />#2 {main}       <br />&#160; thrown in D:\workspace\askeet\lib\vender\symfony\lib\addon\propel\sfPropelData       <br />.class.php on line 206 </p>
<p>Fatal error: Uncaught exception &#8217;sfException&#8217; with message &#8216;Unable to find path      <br />for class &quot;francoisPeer&quot;.&#8217; in D:\workspace\askeet\lib\vender\symfony\lib\addon\p       <br />ropel\sfPropelData.class.php:206       <br />Stack trace:       <br />#0 D:\workspace\askeet\lib\vender\symfony\lib\addon\propel\sfPropelData.class.ph       <br />p(46): sfPropelData-&gt;doDeleteCurrentData(Array)       <br />#1 D:\workspace\askeet\batch\load_data.php(24): sfPropelData-&gt;loadData(&#8216;D:\works       <br />pace\as&#8230;&#8217;)       <br />#2 {main}&#160;&#160; thrown in D:\workspace\askeet\lib\vender\symfony\lib\addon\propel\sfPropelData .class.php on line 206</p>
</blockquote>
<p>这个错误是test_data.yml文件格式书写错误导致的。因为直接拷贝网页上给出的yaml代码会有多余的空格，而yaml文件正是有空格控制格式的，所以要严格按照规范书写。</p>
]]></content:encoded>
			<wfw:commentRss>http://yaron.org.cn/archives/128/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP软件开发测试简介</title>
		<link>http://yaron.org.cn/archives/83</link>
		<comments>http://yaron.org.cn/archives/83#comments</comments>
		<pubDate>Mon, 09 Mar 2009 07:38:30 +0000</pubDate>
		<dc:creator>Yaron</dc:creator>
				<category><![CDATA[PHP高级应用]]></category>

		<guid isPermaLink="false">http://yaron.org.cn/archives/83</guid>
		<description><![CDATA[&#160;&#160;&#160;&#160;&#160; 笔者发现国内的PHP开发，对测试这一块儿不是特别重视，要不就是秘书兼职测试(功能测试)，要不就是程序员自己测试自己的程序，要不就是把开发效率低、水平不怎么样的程序员发配去做测试。笔者表示无奈和担忧，本文就从测试的目的、原则、方法、以及内容上，简单介绍一下PHP软件测试。
 
1. 测试目的
&#160;&#160;&#160;&#160;&#160; 从用户角度看，软件测试就是及早发现软件中隐藏的错误和缺陷。     &#160;&#160;&#160;&#160;&#160; 而开发者角度看，就是为了确认软件不存在错误，满足用户的需求。
2. 测试原则
&#160;&#160;&#160;&#160;&#160; 2.1 尽早而且不断地进行测试，开发阶段就应该有测试人员参与。     &#160;&#160;&#160;&#160;&#160; 2.2<a href="http://yaron.org.cn/archives/83" class="searchmore">Read the Rest...</a><div class="clr"></div>]]></description>
			<content:encoded><![CDATA[<p>&#160;&#160;&#160;&#160;&#160; 笔者发现国内的PHP开发，对测试这一块儿不是特别重视，要不就是秘书兼职测试(功能测试)，要不就是程序员自己测试自己的程序，要不就是把开发效率低、水平不怎么样的程序员发配去做测试。笔者表示无奈和担忧，本文就从测试的目的、原则、方法、以及内容上，简单介绍一下PHP软件测试。</p>
<p> <span id="more-83"></span><br />
<h5>1. 测试目的</h5>
<p>&#160;&#160;&#160;&#160;&#160; 从用户角度看，软件测试就是及早发现软件中隐藏的错误和缺陷。     <br />&#160;&#160;&#160;&#160;&#160; 而开发者角度看，就是为了确认软件不存在错误，满足用户的需求。</p>
<h5>2. 测试原则</h5>
<p>&#160;&#160;&#160;&#160;&#160; 2.1 尽早而且不断地进行测试，开发阶段就应该有测试人员参与。     <br />&#160;&#160;&#160;&#160;&#160; 2.2 测试用例设计应该由测试输入数据和对应的测试输出结果组成。     <br />&#160;&#160;&#160;&#160;&#160; 2.3 程序员避免测试自己的程序。     <br />&#160;&#160;&#160;&#160;&#160; 2.4 测试用例的设计中，包含合理的输入以及不合理的输入。     <br />&#160;&#160;&#160;&#160;&#160; 2.5 检查程序功能的完备性，还应该检查程序的多余性（副作用）。     <br />&#160;&#160;&#160;&#160;&#160; 2.6 业务流程和接口内容的测试十分重要。     <br />&#160;&#160;&#160;&#160;&#160; 2.7 注意测试中群集现象（即残存错误的数目与已经发现的数目成正比）。     <br />&#160;&#160;&#160;&#160;&#160; 2.8 严格执行测试计划。     <br />&#160;&#160;&#160;&#160;&#160; 2.9 对测试结果进行全面的检查。     <br />&#160;&#160;&#160;&#160;&#160; 2.10 要妥善保存测试计划、测试用例、出错统计、最终分析报告，以便查阅及重复利用。     <br />&#160;&#160;&#160;&#160;&#160; 2.11 性能测试与功能测试同等重要。</p>
<h5>3. 测试方法</h5>
<p>&#160;&#160;&#160;&#160;&#160; 3.1 动态测试。可控地运行待测程序，多角度观察程序内部运行。发现错误的效率与测试用例的设计(典型方法有黑/白盒测试)相关。     <br />&#160;&#160;&#160;&#160;&#160; 3.2 静态测试。人工评审文档和程序。     <br />&#160;&#160;&#160;&#160;&#160; 3.3 正确性测试。证明程序符合要求。常用归纳断言法，尚处于理论研究阶段。</p>
<h5>4. 测试内容</h5>
<p>&#160;&#160;&#160;&#160;&#160; 4.1 单元测试     <br />&#160;&#160;&#160;&#160;&#160; 单元测试是程序模块正确性的检验。具体包括模块接口内容测试、局部数据结构测试、路径测试、错误处理测试、边界值测试。     <br />&#160;&#160;&#160;&#160;&#160; 4.2 集成测试     <br />&#160;&#160;&#160;&#160;&#160; 集成测试是对各模块集成运行的正确性检验。方法有自顶向下、自底向上，应用中一般将两种方法混合使用。     <br />&#160;&#160;&#160;&#160;&#160; 4.3 确认测试     <br />&#160;&#160;&#160;&#160;&#160; 确认测试检查软件是否按照合同要求进行工作，是否完成软件需求说明书中的确认目标。包括确认标准、配置复审、α/β测试。     <br />&#160;&#160;&#160;&#160;&#160; 4.4 系统测试     <br />&#160;&#160;&#160;&#160;&#160; 充分运行系统的各个部分，对其可恢复性、安全性、承受强度、性能等方法进行测试。</p>
<h5>5. 总结</h5>
<p> 
<p>&#160;&#160;&#160;&#160;&#160; 以上是笔者根据自己的经验以及软件测试相关知识整理而来。在实际工作中不是都要应用，WEB系统开发周期短，以及系统规模、开发进度、项目投资等要素的不同，相应取舍调整、灵活应用。    <br />&#160;&#160;&#160;&#160;&#160; 笔者推荐一篇相关的经典文章：<a href="http://www.zhiweinet.com/jiaocheng/2009-02/2524.htm">php教程:软件测试之Web测试经典总结</a> 注重实际的应用，值得收藏。</p>
]]></content:encoded>
			<wfw:commentRss>http://yaron.org.cn/archives/83/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>抓取并处理WEB页面数据（CURL/DOM）</title>
		<link>http://yaron.org.cn/archives/77</link>
		<comments>http://yaron.org.cn/archives/77#comments</comments>
		<pubDate>Fri, 06 Mar 2009 03:12:18 +0000</pubDate>
		<dc:creator>Yaron</dc:creator>
				<category><![CDATA[PHP高级应用]]></category>

		<guid isPermaLink="false">http://yaron.org.cn/archives/77</guid>
		<description><![CDATA[&#160;&#160;&#160;&#160;&#160; 昨天面试，让写一个小脚本，抓取http://www.newegg.com.cn相关产品的数据。以下是笔者的实现思路。
 
笔者要从以下两方面入手分析：
1. 如何提交数据   &#160;&#160;&#160;&#160;&#160; 首先是接受数据表单所在的页面，一般都是首页。其次，分析表单是通过POST还是GET提交数据，新蛋使用的是GET提交数据。最后，如果使用的是GET方法，那么需要找到参数所附的URL是什么，新蛋的是http://www.newegg.com.cn/Product/ProductSearchAdvanced.aspx。
2. 返回数据的结构   &#160;&#160;&#160;&#160;&#160; 根据查看返回的源代码，笔者认为，新蛋返回的数据结构比较清晰。比如，产品列表块儿就有id标识，而产品标题使用h3标识，这些都方便了DOM解析。
&#160;&#160;&#160;&#160;&#160; 经过笔者简单分析，用不到POST传递参数，那么FOPEN()/cURL/SOCKET均可实现数据抓取(三者区别笔者会在后续的博文中详述)，这里采用cURL技术；而数据的处理，可以使用正则表达式和DOM类来实现，由于数据结构清晰，使用DOM类足够，正则表达式同样可以做到，但是效率不高(关于正则表达式的使用，笔者会在后续的博文中详述)，这里不采用。
编码实现
1. 功能实现用到的函数   Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
&#160;
/**
 * 抓取web数据，并以UTF-8格式返回结果
<a href="http://yaron.org.cn/archives/77" class="searchmore">Read the Rest...</a><div class="clr"></div>]]></description>
			<content:encoded><![CDATA[<p>&#160;&#160;&#160;&#160;&#160; 昨天面试，让写一个小脚本，抓取<a href="http://www.newegg.com.cn">http://www.newegg.com.cn</a>相关产品的数据。以下是笔者的实现思路。</p>
<p> <span id="more-77"></span>
<p>笔者要从以下两方面入手分析：</p>
<p>1. 如何提交数据   <br />&#160;&#160;&#160;&#160;&#160; 首先是接受数据表单所在的页面，一般都是首页。其次，分析表单是通过POST还是GET提交数据，新蛋使用的是GET提交数据。最后，如果使用的是GET方法，那么需要找到参数所附的URL是什么，新蛋的是<a title="http://www.newegg.com.cn/Product/ProductSearchAdvanced.aspx" href="http://www.newegg.com.cn/Product/ProductSearchAdvanced.aspx">http://www.newegg.com.cn/Product/ProductSearchAdvanced.aspx</a>。</p>
<p>2. 返回数据的结构   <br />&#160;&#160;&#160;&#160;&#160; 根据查看返回的源代码，笔者认为，新蛋返回的数据结构比较清晰。比如，产品列表块儿就有id标识，而产品标题使用h3标识，这些都方便了DOM解析。</p>
<p>&#160;&#160;&#160;&#160;&#160; 经过笔者简单分析，用不到POST传递参数，那么FOPEN()/cURL/SOCKET均可实现数据抓取(三者区别笔者会在后续的博文中详述)，这里采用cURL技术；而数据的处理，可以使用正则表达式和DOM类来实现，由于数据结构清晰，使用DOM类足够，正则表达式同样可以做到，但是效率不高(关于正则表达式的使用，笔者会在后续的博文中详述)，这里不采用。</p>
<p>编码实现</p>
<p>1. 功能实现用到的函数   <br />Code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * 抓取web数据，并以UTF-8格式返回结果
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> catchWebPage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$curl</span>	<span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #339933;">,</span> CURLOPT_FAILONERROR<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #339933;">,</span> CURLOPT_TIMEOUT<span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #339933;">,</span> CURLOPT_BINARYTRANSFER<span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$r</span>		<span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$curl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$r</span>	<span style="color: #339933;">=</span> <span style="color: #990000;">iconv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'gb2312'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'utf-8'</span><span style="color: #339933;">,</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//$r	= mb_convert_encoding($r, 'utf-8','gb2312');</span>
<span style="color: #000088;">$r</span>	<span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=gb2312&quot;&gt;'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$r</span>	<span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;head&gt;'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&lt;/head&gt;&lt;head&gt;&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;&gt;'</span><span style="color: #339933;">,</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$r</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * 统计出页面数量
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> catchProductPages<span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$dom</span>	<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">@</span><span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadHTML</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pages</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'em'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pages</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$pageNo</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$pageNo</span>	<span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pageNo</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$pageNo</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">saveHTML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * 使用DOM树方式处理并展示抓取结果
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> displayProduct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$dom</span>	<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">@</span><span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadHTML</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pages</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'em'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pages</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$pageNo</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$pageNo</span>	<span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pageNo</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$products</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'products'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$productlist</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$products</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$productlist</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$product</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$productName</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'h3'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$productInfos</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ul'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'li'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$productImage</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'img'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'src'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$productPrice</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$product</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ul'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'li'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;p style='clear:both;border-bottom:1px #ccc solid;height:100px;'&gt;&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&lt;span style=&quot;float:right;margin-left:20px;&quot;&gt;'</span><span style="color: #339933;">,</span><span style="color: #000088;">$productPrice</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&lt;/span&gt;&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&lt;img src=&quot;</span><span style="color: #000088;">$productImage</span><span style="color: #0000ff;">&quot; style=&quot;</span>float<span style="color: #339933;">:</span>left<span style="color: #339933;">;</span><span style="color: #0000ff;">&quot;/&gt;&quot;</span><span style="color: #339933;">;</span>	
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$productName</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&lt;br /&gt;&lt;br /&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$productInfos</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$productInfo</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;span style=&quot;margin:auto 10px;&quot;&gt;'</span><span style="color: #339933;">,</span><span style="color: #000088;">$productInfo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'&lt;/span&gt;'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/p&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">saveHTML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>meta<span style="color: #339933;">&gt;&lt;/</span>head<span style="color: #339933;">&gt;&lt;/</span>meta<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>2. 数据提交表单<br />
  <br />Code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>form method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&amp;lt;?=strip_tags(<span style="color: #006699; font-weight: bold;">$_SERVER</span>['PHP_SELF']) ?&amp;gt;&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>select name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;brand&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;sony&quot;</span><span style="color: #339933;">&gt;</span>Sony<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;canon&quot;</span><span style="color: #339933;">&gt;</span>Canon<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>select<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>select name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;product&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;数码相机&quot;</span><span style="color: #339933;">&gt;</span>数码相机<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>option value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;笔记本&quot;</span><span style="color: #339933;">&gt;</span>笔记本<span style="color: #339933;">&lt;/</span>option<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>select<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>3. 函数调用<br />
  <br />Code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$brand</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'brand'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$proName</span><span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'product'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$keyWord</span>	<span style="color: #339933;">=</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">iconv</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'utf-8'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'gb2312'</span><span style="color: #339933;">,</span><span style="color: #000088;">$proName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$keyWord</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$brand</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'+'</span><span style="color: #339933;">.</span><span style="color: #000088;">$keyWord</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$url</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.newegg.com.cn/Product/ProductSearchAdvanced.aspx?keyWord=<span style="color: #006699; font-weight: bold;">{$keyWord}</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$r</span>	<span style="color: #339933;">=</span> catchWebPage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
displayProduct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pageAll</span>	<span style="color: #339933;">=</span> catchProductPages<span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span> <span style="color: #339933;">=</span><span style="color: #000088;">$pageAll</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$urlTemp</span>	<span style="color: #339933;">=</span> <span style="color: #000088;">$url</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;pageIndex='</span><span style="color: #339933;">.</span><span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$r</span>		<span style="color: #339933;">=</span> catchWebPage<span style="color: #009900;">&#40;</span><span style="color: #000088;">$urlTemp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	displayProduct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>&#160;&#160;&#160;&#160;&#160; 在使用DOM类时需要注意编码问题。新蛋网页是GB2312，cURL抓取后同样是GB2312，而DOM类默认只处理UTF-8格式编码。所以，使用cURL抓取后要转码。这部分可以参考<a href="http://www.fwolf.com/blog/post/314" target="_blank">Fwolf’s Blog</a>《DOMDocument-&gt;loadHTML()处理中文的一点问题》，有很好解决方案。</p>
<p>&#160;&#160;&#160;&#160;&#160; 另外一个问题就是，curl默认只抓取当前页返回的数据，要想抓取所有数据，就需要获得页数，循环抓取每个分页的数据。具体可以参考第三部分代码。</p>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://yaron.org.cn/archives/77/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ＰＨＰ漏洞ＸＳＳ的介绍和预防策略</title>
		<link>http://yaron.org.cn/archives/62</link>
		<comments>http://yaron.org.cn/archives/62#comments</comments>
		<pubDate>Wed, 04 Mar 2009 14:00:41 +0000</pubDate>
		<dc:creator>Yaron</dc:creator>
				<category><![CDATA[PHP高级应用]]></category>

		<guid isPermaLink="false">http://yaron.org.cn/archives/62</guid>
		<description><![CDATA[&#160;&#160;&#160;&#160;&#160; XSS，第一次听说是在博彦面试的时候，后来查询才知道这是PHP编程的一个漏洞。用户可以利用此漏洞执行自己的JS代码，获得网站重要数据。

&#160;&#160;&#160;&#160;&#160; XSS英文全称：Cross Site Scripting。它是和$_SERVER['PHP_SELF'](不多讲查手册)相关的一个漏洞。主要出现在表单提交数据到本身脚本这一应用上。
&#160;&#160;&#160;&#160;&#160; 先看一下应用代码：

1
2
3
4
// 将尖括号更换正常状态
&#60;form method=&#34;post&#34; action=&#34;&#38;lt;?=$_SERVER['PHP_SELF']; ?&#38;gt;&#34;&#62;
&#60;input type=&#34;submit&#34; value=&#34;submit&#34;/&#62;
&#60;/form&#62;

&#160;&#160;&#160;&#160;&#160; 我们看到代码是想通过$_SERVER['PHP_SELF']的值，确定表单数据提交的位置，也就是脚本本身。比如该脚本名字index.php，当然在地址栏中输入index.php是没有任何问题的，但是如果在地址栏中给index.php脚本加上参数，会是怎样的结果？

1
2
3
4
5
6
7
8
9
10
11
&#60;!--将尖括号更换正常状态--&#62;
&#60;!--地址栏输入
http://localhost/eg/stu/XSS/index.php/&#38;lt;script&#38;gt;alert('xss')&#38;lt;/script&#38;gt;--&#62; 
&#60;!--回车打开页面，此时会有窗口弹出，确定后查看页面源码如下：--&#62;
&#160;
&#60;form method=&#34;post&#34; action=&#34;
/eg/stu/XSS/index.php/&#38;lt;script&#38;gt;alert('xss')&#38;lt;/script&#38;gt;&#34;&#62;
&#60;input type=&#34;submit&#34; value=&#34;submit&#34; /&#62;
&#60;/form&#62;
&#160;
&#60;!-- 此时单击Submit按钮，同样有JS代码执行<a href="http://yaron.org.cn/archives/62" class="searchmore">Read the Rest...</a><div class="clr"></div>]]></description>
			<content:encoded><![CDATA[<p>&#160;&#160;&#160;&#160;&#160; XSS，第一次听说是在博彦面试的时候，后来查询才知道这是PHP编程的一个漏洞。用户可以利用此漏洞执行自己的JS代码，获得网站重要数据。</p>
<p><span id="more-62"></span>
<p>&#160;&#160;&#160;&#160;&#160; XSS英文全称：Cross Site Scripting。它是和$_SERVER['PHP_SELF'](不多讲查手册)相关的一个漏洞。主要出现在表单提交数据到本身脚本这一应用上。</p>
<p>&#160;&#160;&#160;&#160;&#160; 先看一下应用代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// 将尖括号更换正常状态</span>
<span style="color: #339933;">&lt;</span>form method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&amp;lt;?=<span style="color: #006699; font-weight: bold;">$_SERVER</span>['PHP_SELF']; ?&amp;gt;&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;submit&quot;</span><span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>&#160;&#160;&#160;&#160;&#160; 我们看到代码是想通过$_SERVER['PHP_SELF']的值，确定表单数据提交的位置，也就是脚本本身。比如该脚本名字index.php，当然在地址栏中输入index.php是没有任何问题的，但是如果在地址栏中给index.php脚本加上参数，会是怎样的结果？</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;!--将尖括号更换正常状态--&gt;
&lt;!--地址栏输入
http://localhost/eg/stu/XSS/index.php/&amp;lt;script&amp;gt;alert('xss')&amp;lt;/script&amp;gt;--&gt; 
&lt;!--回车打开页面，此时会有窗口弹出，确定后查看页面源码如下：--&gt;
&nbsp;
&lt;form method=&quot;post&quot; action=&quot;
/eg/stu/XSS/index.php/&amp;lt;script&amp;gt;alert('xss')&amp;lt;/script&amp;gt;&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;submit&quot; /&gt;
&lt;/form&gt;
&nbsp;
&lt;!-- 此时单击Submit按钮，同样有JS代码执行 --&gt;</pre></td></tr></table></div>

<p>&#160;&#160;&#160;&#160;&#160; 如果参数是一段js脚本又会怎样？这有更多的测试代码http://ha.ckers.org/xss.html</p>
<p>&#160;&#160;&#160;&#160;&#160; 是不是js代码执行了？这个到底有多恐怖，大家可以参考<a href="http://www.fwolf.com/blog/post/300" target="_blank">fwolf的博文</a>，那里更为详细的阐述了XSS的危害。</p>
<p>&#160;&#160;&#160;&#160;&#160; <strong>这里笔者给出解决XSS攻击的思路：</strong></p>
<p>&#160;&#160;&#160;&#160;&#160; 对$_SERVER['PHP_SELF']进行转义。过滤其携带的可执行代码。涉及到的函数有htmlentities、strip_tags，以及正则表达式等。</p>
<p>当然，表单的action默认为空就安全了吗？同样不安全！问题如上。</p>
]]></content:encoded>
			<wfw:commentRss>http://yaron.org.cn/archives/62/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
