﻿<?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-有时，退一步，能一口气进几步，只是这先退一步需要勇气和自信。-随笔分类-PHP</title><link>http://www.blogjava.net/ducklyl/category/34655.html</link><description>用心爱你，努力工作。&lt;br/&gt;
</description><language>zh-cn</language><lastBuildDate>Tue, 23 Mar 2010 20:36:46 GMT</lastBuildDate><pubDate>Tue, 23 Mar 2010 20:36:46 GMT</pubDate><ttl>60</ttl><item><title>Php Memcache函数库</title><link>http://www.blogjava.net/ducklyl/archive/2010/03/23/316262.html</link><dc:creator>王生生</dc:creator><author>王生生</author><pubDate>Tue, 23 Mar 2010 03:11:00 GMT</pubDate><guid>http://www.blogjava.net/ducklyl/archive/2010/03/23/316262.html</guid><wfw:comment>http://www.blogjava.net/ducklyl/comments/316262.html</wfw:comment><comments>http://www.blogjava.net/ducklyl/archive/2010/03/23/316262.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ducklyl/comments/commentRss/316262.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ducklyl/services/trackbacks/316262.html</trackback:ping><description><![CDATA[Memcache函数库是在PECL(PHP Extension Community Library)中，<br />
主要作用是搭建大容量的内存数据的临时存放区域，<br />
在分布式的时候作用体现的非常明显，否则不建议使用。以下為幾個例子，都很簡單。<br />
<br />
&lt;?php<br />
$memcache = new Memcache;<br />
$memcache-&gt;connect('localhost', 11211) or die ("Could <br />
not connect");<br />
$version = $memcache-&gt;getVersion();<br />
echo "Server's version: ".$version." ";<br />
?&gt;<br />
<br />
&lt;?php <br />
$memcache = new Memcache; $memcache-&gt;connect('localhost', 11211) or die ("Could not connect");<br />
print_r($memcache-&gt;getStats());<br />
/** * Array * ( * [pid] =&gt; 8052 * [uptime] =&gt; 9205 * [time] =&gt; 1205898428 * [version] =&gt; 1.2.5 * [pointer_size] =&gt; 32 * [rusage_user] =&gt; 0.008000 * [rusage_system] =&gt; 0.000000 * [curr_items] =&gt; 1 * [total_items] =&gt; 17 * [bytes] =&gt; 57 * [curr_connections] =&gt; 2 * [total_connections] =&gt; 15 * [connection_structures] =&gt; 3 * [cmd_get] =&gt; 9 * [cmd_set] =&gt; 23 * [get_hits] =&gt; 5 * [get_misses] =&gt; 4 * [evictions] =&gt; 0 * [bytes_read] =&gt; 671 * [bytes_written] =&gt; 850 * [limit_maxbytes] =&gt; 10485760 * [threads] =&gt; 1 * ) */<br />
?&gt;<br />
<br />
&lt;?php<br />
$memcache = new Memcache;<br />
$memcache-&gt;connect('localhost', 11211) or die ("Could not connect");<br />
$memcache-&gt;set( 'name', 'leo', 0, 30);<br />
if(!$memcache-&gt;add( 'name', 'susan', 0, 30)){<br />
&nbsp;echo 'susan is exist';<br />
};<br />
$memcache-&gt;replace( 'name', 'lion', 0, 300);<br />
echo $memcache-&gt;get( 'name');<br />
$memcache-&gt;delete( 'name', 5);<br />
?&gt;<br />
<br />
&lt;?php<br />
function _callback_memcache_failure($host, $port) {<br />
&nbsp;print "memcache '$host:$port' failed";<br />
}<br />
$memcache = new Memcache;<br />
$memcache-&gt;addServer('192.168.1.116', 11211);<br />
$memcache-&gt;setServerParams('192.168.1.116', 11211, 1, 15, true,'_callback_memcache_failure');<br />
echo $memcache-&gt;getServerStatus('192.168.1.116', 11211);<br />
?&gt;<br />
<br />
<br />
&lt;?php<br />
$memcache = new Memcache;<br />
$memcache-&gt;connect('localhost', 11211);<br />
$memcache-&gt;set('test_item', 8);<br />
$memcache-&gt;increment('test_item', 4);<br />
echo $memcache-&gt;decrement('test_item', 7);<br />
// 显示 5<br />
?&gt;<br />
<br />
Memcach方法說明：<br />
01.Memcache::add — 添加一个值，如果已经存在，则返回false<br />
02.Memcache::addServer — 添加一个可供使用的服务器地址<br />
03.Memcache::close — 关闭一个Memcache对象<br />
04.Memcache::connect — 创建一个Memcache对象<br />
05.memcache_debug — 控制调试功能<br />
06.Memcache::decrement — 对保存的某个key中的值进行减法操作 <br />
07.Memcache::delete — 删除一个key值<br />
08.Memcache::flush — 清除所有缓存的数据<br />
09.Memcache::get — 获取一个key值<br />
10.Memcache::getExtendedStats — 获取进程池中所有进程的运行系统统计<br />
11.Memcache::getServerStatus — 获取运行服务器的参数<br />
12.Memcache::getStats — 返回服务器的一些运行统计信息<br />
13.Memcache::getVersion — 返回运行的Memcache的版本信息<br />
14.Memcache::increment — 对保存的某个key中的值进行加法操作<br />
15.Memcache::pconnect — 创建一个Memcache的持久连接对象<br />
16.Memcache::replace — R对一个已有的key进行覆写操作<br />
17.Memcache::set — 添加一个值，如果已经存在，则覆写<br />
18.Memcache::setCompressThreshold — 对大于某一大小的数据进行压缩<br />
19.Memcache::setServerParams — 在运行时修改服务器的参数<br />
<br />
<br />
<img src ="http://www.blogjava.net/ducklyl/aggbug/316262.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ducklyl/" target="_blank">王生生</a> 2010-03-23 11:11 <a href="http://www.blogjava.net/ducklyl/archive/2010/03/23/316262.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转）PHP cookie详解</title><link>http://www.blogjava.net/ducklyl/archive/2009/12/31/307925.html</link><dc:creator>王生生</dc:creator><author>王生生</author><pubDate>Thu, 31 Dec 2009 08:22:00 GMT</pubDate><guid>http://www.blogjava.net/ducklyl/archive/2009/12/31/307925.html</guid><wfw:comment>http://www.blogjava.net/ducklyl/comments/307925.html</wfw:comment><comments>http://www.blogjava.net/ducklyl/archive/2009/12/31/307925.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ducklyl/comments/commentRss/307925.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ducklyl/services/trackbacks/307925.html</trackback:ping><description><![CDATA[<div id="blog_text" class="cnt">
<p><font color="#008080">bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )</font></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; setcookie() 定义一个和其余的 HTTP 标头一起发送的 cookie。和其它标头一样，cookie
必须在脚本的任何其它输出之前发送（这是协议限制）。这需要将本函数的调用放到任何输出之前，包括 &lt;html&gt; 和
&lt;head&gt; 标签以及任何空格。如果在调用 setcookie() 之前有任何输出，本函数将失败并返回 FALSE。如果
setcookie() 函数成功运行，将返回 TRUE。这并不说明用户是否接受了 cookie。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; 注: 自 PHP 4
起，可以用输出缓存来在调用本函数前输出内容，代价是把所有向浏览器的输出都缓存在服务器，直到下命令发送它们。可以在代码中使用
ob_start() 及 ob_end_flush() 来实现这样的功能，或者通过修改 php.ini 中的 output_buffering
配置选项来实现，也可以通过修改服务器配置文件来实现。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 除了 name 外，其它所有参数都是可选的。可以用空字符串（""）替换某参数以跳过该参数。因为参数 expire
是整型，不能用空字符串掉过，可以用零（0）来代替 。下面的说明对 setcookie() 的每一个参数都进行了解释。可以对照 Netscape
cookie 规范以了解 setcookie() 的每一个参数的细节以及通过阅读 RFC 2965 了解 HTTP cookie 的工作方式。</p>
<p><strong>1. setcookie() 参数详解<br />
</strong>参数 说明 举例<br />
<em>name Cookie 的名字</em>：&nbsp;&nbsp;  使用 $_COOKIE['cookiename'] 调用名为 cookiename 的 cookie。<br />
<em>value Cookie 的值</em>：此值保存在客户端，不要用来保存敏感数据。&nbsp;&nbsp;  假定 name 是 'cookiename'，可以通过 $_COOKIE['cookiename'] 取得其值。<br />
<em>expire Cookie 过期的时间</em>：这是个 Unix 时间戳，即从 Unix 纪元开始的秒数。换而言之，通常用
time() 函数再加上秒数来设定 cookie 的失效期。或者用 mktime()来实现。&nbsp;&nbsp; time()+60*60*24*30 将设定
cookie 30 天后失效。如果未设定，cookie 将会在会话结束后（一般是浏览器关闭）失效。<br />
<em>path Cookie 在服务器端的有效路径</em>：&nbsp;&nbsp;  如果该参数设为 '/' 的话，cookie 就在整个 domain 内有效，如果设为 '/foo/'，cookie 就只在 domain 下的 /foo/ 目录及其子目录内有效，例如 /foo/bar/。默认值为设定 cookie 的当前目录。<br />
<em>domain 该 cookie 有效的域名</em>：&nbsp;&nbsp;  要使 cookie 能在如 example.com 域名下的所有子域都有效的话，该参数应该设为 '.example.com'。虽然 . 并不必须的，但加上它会兼容更多的浏览器。如果该参数设为 <em>www.example.com</em> 的话，就只在 www 子域内有效。细节见 Cookie 规范中的 tail matching。<br />
<em>secure 指明</em>： cookie 是否仅通过安全的 HTTPS 连接传送。当设成 TRUE 时，cookie 仅在安全的连接中被设置。默认值为 FALSE。&nbsp;&nbsp;  0 或 1</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 当 cookie 被设置后，便可以在其它页面通过 $_COOKIE 或 $HTTP_COOKIE_VARS
数组取得其值。需要注意的是，autoglobals 的 $_COOKIE 形式适用于 PHP 4.1.0 或更高版本。而
$HTTP_COOKIE_VARS 则从 PHP 3 起就可以使用。Cookie 的值也会被保存到 $_REQUEST 数组中。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;  注: 如果 PHP 的选项 register_globals 被设为 on 的话，cookie 的值仍然会被斌到变量内。在下面的例子中，$TestCookie 会被注册，但是仍然推荐使用 $_COOKIE 数组。</p>
<p><strong>常见缺陷：</strong></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cookies 不会在设置它的本页生效，要测试一个 cookie
是否被成功的设定，可以在其到期之前通过另外一个页面来访问其值。过期时间是通过参数 expire 来设置的。可以简单地使用
print_r($_COOKIE); 来调试现有的 cookies。<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;  *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  Cookie 必须用和设定时的同样的参数才能删除。如果其值一个空字符串，或者是 FALSE，并且其它的参数都和前一次调用 setcookie 时相同，那么所指定名称的 cookie 将会在远程客户端被删除。<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 由于把 cookie 的值设为 FALSE 会使客户端尝试删除这个 cookie，所以要在 cookie 上保存
TRUE 或 FALSE 时不应该直接使用 boolean 值，而应该用 0 来表示 FALSE，用 1 来表示 TRUE<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp; *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 可以把 cookie 的名称设置成一个数组，但是数组 cookie
中的每个元素的值将会被单独保存在用户的系统中。考虑使用 explode() 函数用多个名称和值设定一个 cookie。不推荐将
serialize() 用于此目的，因为它可能会导致一个安全漏洞。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在 PHP 3 中，在同一个 PHP 脚本中多次使用 setcookie() 来设置
cookie，将会按照倒序的方式来分别执行，如果想要在插入另外一个 cookie 之前删除一个 cookie，要把插入放到删除之前。自 PHP
4 起，多次调用 setcookie() 则是按照顺序来执行的。</p>
<p>下面一些例子说明了如何发送 cookie：</p>
<p>例子 1. setcookie() 发送例子<br />
$value = 'something from somewhere';</p>
<p>setcookie("TestCookie", $value);<br />
setcookie("TestCookie", $value,time()+3600);&nbsp;&nbsp;  /* expire in 1 hour */<br />
setcookie("TestCookie", $value,time()+3600, "/~rasmus/", ".utoronto.ca", 1);</p>
<p>注意 cookie 中值的部分在发送的时候会被自动用 urlencode 编码并在接收到的时候被自动解码并把值赋给与自己同名的
cookie 变量。如果不想这样并且在使用 PHP 5 的话，可以用 setrawcookie()
来代替。下面这个简单的例子可以得到刚才所设定的 cookie 的值：</p>
<p>&lt;?php<br />
// 输出单独的 cookie<br />
echo $_COOKIE["TestCookie"];<br />
echo $HTTP_COOKIE_VARS["TestCookie"];</p>
<p>// 另一个调试的方法就是输出所有的 cookie<br />
print_r($_COOKIE);<br />
?&gt;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  要删除 cookie 需要确保它的失效期是在过去，才能触发浏览器的删除机制。下面的例子说明了如何删除刚才设置的 cookie：</p>
<p>例子 2. setcookie() 删除例子<br />
// 将过期时间设为一小时前<br />
setcookie("TestCookie", "", time() - 3600);<br />
setcookie("TestCookie", "", time() - 3600, "/~rasmus/", ".utoronto.ca", 1);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  也可以通过在 cookie 名称中使用数组符号来设定数组 cookie，可以设定多个 cookie 作为数组单元，在脚本提取 cookie 时所有的值都放在一个数组中：</p>
<p>例子 3. setcookie() 中使用数组的例子<br />
&lt;?php<br />
// 设定 cookie<br />
setcookie("cookie[three]", "cookiethree");<br />
setcookie("cookie[two]", "cookietwo");<br />
setcookie("cookie[one]", "cookieone");</p>
<p>// 刷新页面后，显示出来<br />
if (isset($_COOKIE['cookie'])) {<br />
&nbsp;&nbsp;&nbsp;  foreach ($_COOKIE['cookie'] as $name =&gt; $value) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  echo "$name : $value &lt;br /&gt;\n";<br />
&nbsp;&nbsp;&nbsp;  }<br />
}<br />
?&gt;</p>
<p>上例将输出：</p>
<p>three : cookiethree<br />
two : cookietwo<br />
one : cookieone</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;  注: 下面的一些 RFC 也具参考价值：RFC 2109 和 RFC 2695。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;  注意 expire 参数的接受的是 Unix 时间戳，而不是日期格式 Wdy, DD-Mon-YYYY HH:MM:SS GMT，这是因为 PHP 在内部进行了转换。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;  expire 是与客户端的时间相比较，和服务器时间可能不同。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;  注: 向 Microsoft Internet Explorer 4 Service Pack 1 不能正确处理设定了 path 的 cookie。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;  Netscape Communicator 4.05 及 Microsoft Internet Explorer 3.x 不能正确处理没有设定 path 和 time 的 cookie。</p>
<p>参见 header()，setrawcookie()</p>
</div>
<img src ="http://www.blogjava.net/ducklyl/aggbug/307925.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ducklyl/" target="_blank">王生生</a> 2009-12-31 16:22 <a href="http://www.blogjava.net/ducklyl/archive/2009/12/31/307925.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>PHP $_SERVER用法</title><link>http://www.blogjava.net/ducklyl/archive/2009/11/05/301208.html</link><dc:creator>王生生</dc:creator><author>王生生</author><pubDate>Thu, 05 Nov 2009 01:31:00 GMT</pubDate><guid>http://www.blogjava.net/ducklyl/archive/2009/11/05/301208.html</guid><wfw:comment>http://www.blogjava.net/ducklyl/comments/301208.html</wfw:comment><comments>http://www.blogjava.net/ducklyl/archive/2009/11/05/301208.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ducklyl/comments/commentRss/301208.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ducklyl/services/trackbacks/301208.html</trackback:ping><description><![CDATA[<p style="text-indent: 2em;">1.$_SERVER['HTTP_ACCEPT_LANGUAGE']//浏览器语言 </p>
<p style="text-indent: 2em;">显示:zh-cn</p>
<p style="text-indent: 2em;">2.$_SERVER['REMOTE_ADDR'] //当前用户 IP 。 </p>
<p style="text-indent: 2em;">显示:127.0.0.1</p>
<p style="text-indent: 2em;">3.$_SERVER['REMOTE_HOST'] //当前用户主机名 </p>
<p style="text-indent: 2em;">显示:</p>
<p style="text-indent: 2em;">4.$_SERVER['REQUEST_URI'] //URL</p>
<p style="text-indent: 2em;">显示:/test.php</p>
<p style="text-indent: 2em;">5.$_SERVER['REMOTE_PORT'] //端口。 </p>
<p style="text-indent: 2em;">显示:3864</p>
<p style="text-indent: 2em;">6.$_SERVER['SERVER_NAME'] //服务器主机的名称。 </p>
<p style="text-indent: 2em;">显示:127.0.0.1</p>
<p style="text-indent: 2em;">7.$_SERVER['PHP_SELF']//正在执行脚本的文件名 </p>
<p style="text-indent: 2em;">显示:/test.php</p>
<p style="text-indent: 2em;">8.$_SERVER['argv'] //传递给该脚本的参数。</p>
<p style="text-indent: 2em;">显示:ARRAY</p>
<p style="text-indent: 2em;">9.$_SERVER['argc'] //传递给程序的命令行参数的个数。 </p>
<p style="text-indent: 2em;">显示:0</p>
<p style="text-indent: 2em;">10.$_SERVER['GATEWAY_INTERFACE']//CGI 规范的版本。</p>
<p style="text-indent: 2em;">显示: CGI/1.1</p>
<p style="text-indent: 2em;">11.$_SERVER['SERVER_SOFTWARE'] //服务器标识的字串 </p>
<p style="text-indent: 2em;">显示:Apache/2.0.52 (Win32) PHP/5.2.1</p>
<p style="text-indent: 2em;">12.$_SERVER['SERVER_PROTOCOL'] //请求页面时通信协议的名称和版本 </p>
<p style="text-indent: 2em;">显示:HTTP/1.1</p>
<p style="text-indent: 2em;">13.$_SERVER['REQUEST_METHOD']//访问页面时的请求方法 </p>
<p style="text-indent: 2em;">显示:GET</p>
<p style="text-indent: 2em;">14.$_SERVER['QUERY_STRING'] //查询(query)的字符串。</p>
<p style="text-indent: 2em;">显示:</p>
<p style="text-indent: 2em;">15.$_SERVER['DOCUMENT_ROOT'] //当前运行脚本所在的文档根目录 </p>
<p style="text-indent: 2em;">显示:D:/Program Files/Apache/www</p>
<p style="text-indent: 2em;">16.$_SERVER['HTTP_ACCEPT'] //当前请求的 Accept: 头部的内容。</p>
<p style="text-indent: 2em;">显示:*/*</p>
<p style="text-indent: 2em;">17.$_SERVER['HTTP_ACCEPT_CHARSET'] //当前请求的 Accept-Charset: 头部的内容。 </p>
<p style="text-indent: 2em;">显示:</p>
<p style="text-indent: 2em;">18.$_SERVER['HTTP_ACCEPT_ENCODING'] //当前请求的 Accept-Encoding: 头部的内容 </p>
<p style="text-indent: 2em;">显示:gzip,deflate</p>
<p style="text-indent: 2em;">19.$_SERVER['HTTP_CONNECTION'] //当前请求的 Connection: 头部的内容。例如：&#8220;Keep-Alive&#8221;。</p>
<p style="text-indent: 2em;">显示:Keep-Alive</p>
<p style="text-indent: 2em;">20.$_SERVER['HTTP_HOST'] //当前请求的 Host: 头部的内容。 </p>
<p style="text-indent: 2em;">显示:127.0.0.1:8080</p>
<p style="text-indent: 2em;">21.$_SERVER['HTTP_REFERER'] //链接到当前页面的前一页面的 URL 地址。 </p>
<p style="text-indent: 2em;">显示:http://127.0.0.1:8080/</p>
<p style="text-indent: 2em;">22.$_SERVER['HTTP_USER_AGENT'] //当前请求的 User_Agent: 头部的内容。 </p>
<p style="text-indent: 2em;">显示:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 2.0.50727)</p>
<p style="text-indent: 2em;">23.$_SERVER['HTTPS']//如果通过https访问,则被设为一个非空的值(on)，否则返回off </p>
<p style="text-indent: 2em;">显示:</p>
<p style="text-indent: 2em;">24.$_SERVER['SCRIPT_FILENAME'] #当前执行脚本的绝对路径名。 </p>
<p style="text-indent: 2em;">显示:D:/Program Files/Apache/www/test.php</p>
<p style="text-indent: 2em;">25.$_SERVER['SERVER_ADMIN'] #管理员信息 </p>
<p style="text-indent: 2em;">显示:w@w.com</p>
<p style="text-indent: 2em;">26.$_SERVER['SERVER_PORT'] #服务器所使用的端口 </p>
<p style="text-indent: 2em;">显示:8080</p>
<p style="text-indent: 2em;">27.$_SERVER['SERVER_SIGNATURE'] #包含服务器版本和虚拟主机名的字符串。 </p>
<p style="text-indent: 2em;">显示:Apache/2.0.52 (Win32) PHP/5.2.1 Server at 10.145.40.150 Port 8080</p>
<p style="text-indent: 2em;">28.$_SERVER['PATH_TRANSLATED'] #当前脚本所在文件系统（不是文档根目录）的基本路径。</p>
<p style="text-indent: 2em;">显示:</p>
<p style="text-indent: 2em;">29.$_SERVER['SCRIPT_NAME'] #包含当前脚本的路径。这在页面需要指向自己时非常有用。</p>
<p style="text-indent: 2em;">显示 :/test.php</p>
<p style="text-indent: 2em;">30.$_SERVER['PHP_AUTH_USER'] #当 PHP 运行在 Apache 模块方式下，并且正在使用 HTTP 认证功能，这个变量便是用户输入的用户名。</p>
<p style="text-indent: 2em;">显示:</p>
<p style="text-indent: 2em;">31.$_SERVER['PHP_AUTH_PW'] #当 PHP 运行在 Apache 模块方式下，并且正在使用 HTTP 认证功能，这个变量便是用户输入的密码。 </p>
<p style="text-indent: 2em;">显示:</p>
<p style="text-indent: 2em;">32.$_SERVER['AUTH_TYPE'] #当 PHP 运行在 Apache 模块方式下，并且正在使用 HTTP 认证功能，这个变量便是认证的类型</p>
<img src ="http://www.blogjava.net/ducklyl/aggbug/301208.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ducklyl/" target="_blank">王生生</a> 2009-11-05 09:31 <a href="http://www.blogjava.net/ducklyl/archive/2009/11/05/301208.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(转)小谈PHPMYSQL分页原理及实现</title><link>http://www.blogjava.net/ducklyl/archive/2009/10/21/299154.html</link><dc:creator>王生生</dc:creator><author>王生生</author><pubDate>Wed, 21 Oct 2009 02:08:00 GMT</pubDate><guid>http://www.blogjava.net/ducklyl/archive/2009/10/21/299154.html</guid><wfw:comment>http://www.blogjava.net/ducklyl/comments/299154.html</wfw:comment><comments>http://www.blogjava.net/ducklyl/archive/2009/10/21/299154.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ducklyl/comments/commentRss/299154.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ducklyl/services/trackbacks/299154.html</trackback:ping><description><![CDATA[在看本文之前，请确保你已掌握了PHP的一些知识以及MYSQL的查询操作基础哦。<br />
<br />
作为一个Web程序，经常要和不计其数的数据打交道，比如会员的数据，文章数据，假如只有几十个会员那很好办，在一页显示就可以了，可是假如你的网站是几千甚至几十万会员的话，如果都在一页打开的话无论对浏览器还是观看者都是一种折磨。<br />
<br />
相信每个学习PHP的新手都会对分页这个东西感觉很头疼，不过有了默默的这一水帖，你肯定会拍拍脑袋说，嘿，原来分页竟然如此简单？的确，现在请深呼吸一口新鲜的空气，仔细的听默默给你一点一点的分解。<br />
<br />
假设我们要处理1000条数据，要在每页中显示10条，这样的话就会分100页来显示，咱们先看一看在mysql里提取10条信息是如何操作的。<br />
<br />
Select * from table limit 0,10<br />
<br />
上面是一句很简单的mysql查询语句，它的作用是从一个名叫table的表里提取10条数据，并且把所有字段的值都获得。<br />
<br />
关键的地方就在这段&#8220;limit 0,10&#8221;，它其中的0是以0为起始点，后面的10则是显示10条数据，那么我们要以10为起始点，显示到第20条数据该怎么写呢？<br />
<br />
可能很多大大会心直口快的说&#8220;limit 10,20&#8221;嘛！啊哦，这样可就错误了哦，正确的写法是&#8220;limit 10,10&#8221;它后面的参数并非是结束点而是要提取的数目，记住哦。<br />
<br />
懂得了如何提取10条数据，那么提取1000条也就是做100次这种查询呀，就是说要做如下的查询：<br />
<br />
Limit 0,10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  //第一页<br />
Limit 10,10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //第二页<br />
Limit 20,10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //第三页<br />
Limit 30,10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //第四页<br />
&#8230;&#8230;<br />
看出有什么规律了吗？没错，第一个参数每翻一页就增加10，可是第二个参数是不变的。<br />
也就是说咱们设法根据页数来改变第一个参数的值，就可以进行分页显示数据了，怎么样，原理是不是很简单？<br />
<br />
可是要怎么设法根据页数来改变第一个参数的值呢？首先，咱们要有一个页数的值，用url的GET方式获取。<br />
比如index.php?page=18<br />
相信大部分的大大对这个东西不陌生吧，这种url地址可是随处可见，其中的page参数的作用就是传入要显示的页数。<br />
<br />
咱们通过一段代码来看一看究竟是如何实现的吧：<br />
<div style="overflow: auto;"><code><span style="color: #000000;">  <br />
<span style="color: #0000bb;">&lt;?php <br />
<br />
</span><span style="color: #ff8000;">/* <br />
<br />
Author:默默 <br />
Date&nbsp;&nbsp; :2006-12-03 <br />
<br />
*/ <br />
<br />
</span><span style="color: #0000bb;">$page</span><span style="color: #007700;">=isset(</span><span style="color: #0000bb;">$_GET</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'page'</span><span style="color: #007700;">])?</span><span style="color: #0000bb;">intval</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$_GET</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'page'</span><span style="color: #007700;">]):</span><span style="color: #0000bb;">1</span><span style="color: #007700;">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//这句就是获取page=18中的page的值，假如不存在page，那么页数就是1。 <br />
</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">=</span><span style="color: #0000bb;">10</span><span style="color: #007700;">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//每页显示10条数据 <br />
<br />
</span><span style="color: #0000bb;">$db</span><span style="color: #007700;">=</span><span style="color: #0000bb;">mysql_connect</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"host"</span><span style="color: #007700;">,</span><span style="color: #dd0000;">"name"</span><span style="color: #007700;">,</span><span style="color: #dd0000;">"pass"</span><span style="color: #007700;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//创建数据库连接 <br />
</span><span style="color: #0000bb;">$select</span><span style="color: #007700;">=</span><span style="color: #0000bb;">mysql_select_db</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"db"</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$db</span><span style="color: #007700;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//选择要操作的数据库 <br />
<br />
/* <br />
首先咱们要获取数据库中到底有多少数据，才能判断具体要分多少页，具体的公式就是 <br />
总数据数除以每页显示的条数，有余进一。 <br />
也就是说10/3=3.3333=4 有余数就要进一。 <br />
*/ <br />
<br />
</span><span style="color: #0000bb;">$total</span><span style="color: #007700;">=</span><span style="color: #0000bb;">mysql_num_rows</span><span style="color: #007700;">(</span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"select id from table"</span><span style="color: #007700;">)); </span><span style="color: #ff8000;">//查询数据的总数,id是数据库中的一个自动赋值的字段 <br />
</span><span style="color: #0000bb;">$pagenum</span><span style="color: #007700;">=</span><span style="color: #0000bb;">ceil</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$total</span><span style="color: #007700;">/</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//获得总页数 <br />
<br />
//假如传入的页数参数大于总页数，则显示错误信息 <br />
</span><span style="color: #007700;">If(</span><span style="color: #0000bb;">$page</span><span style="color: #007700;">&gt;</span><span style="color: #0000bb;">$pagenum </span><span style="color: #007700;">|| </span><span style="color: #0000bb;">$page </span><span style="color: #007700;">== </span><span style="color: #0000bb;">0</span><span style="color: #007700;">){ <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Echo </span><span style="color: #dd0000;">"Error : Can Not Found The page ."</span><span style="color: #007700;">; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit; <br />
} <br />
<br />
</span><span style="color: #0000bb;">$offset</span><span style="color: #007700;">=(</span><span style="color: #0000bb;">$page</span><span style="color: #007700;">-</span><span style="color: #0000bb;">1</span><span style="color: #007700;">)*</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//获取limit的第一个参数的值，假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。 <br />
<br />
</span><span style="color: #0000bb;">$info</span><span style="color: #007700;">=</span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"select name from table limit $offset,$num"</span><span style="color: #007700;">);&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//获取相应页数所需要显示的数据,name是数据里的一个字段 <br />
</span><span style="color: #007700;">While(</span><span style="color: #0000bb;">$it</span><span style="color: #007700;">=</span><span style="color: #0000bb;">mysql_fetch_array</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$info</span><span style="color: #007700;">)){ <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Echo </span><span style="color: #0000bb;">$it</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'name'</span><span style="color: #007700;">].</span><span style="color: #dd0000;">"&lt;br /&gt;"</span><span style="color: #007700;">; <br />
}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//显示数据 <br />
&nbsp;&nbsp;&nbsp;  <br />
</span><span style="color: #007700;">For(</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">=</span><span style="color: #0000bb;">1</span><span style="color: #007700;">;</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">&lt;=</span><span style="color: #0000bb;">$pagenum</span><span style="color: #007700;">;</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">++){ <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000bb;">$show</span><span style="color: #007700;">=(</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">!=</span><span style="color: #0000bb;">$page</span><span style="color: #007700;">)?</span><span style="color: #dd0000;">"&lt;a href='index.php?page="</span><span style="color: #007700;">.</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">.</span><span style="color: #dd0000;">"'&gt;$i&lt;/a&gt;"</span><span style="color: #007700;">:</span><span style="color: #dd0000;">"&lt;b&gt;$i&lt;/b&gt;"</span><span style="color: #007700;">; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Echo </span><span style="color: #0000bb;">$show</span><span style="color: #007700;">.</span><span style="color: #dd0000;">" "</span><span style="color: #007700;">; <br />
} <br />
<br />
</span><span style="color: #ff8000;">/*显示分页信息，假如是当页则显示粗体的数字，其余的页数则为超连接，假如当前为第三页则显示如下 <br />
1 2 3 4 5 6 <br />
*/ <br />
</span><span style="color: #0000bb;">?&gt; <br />
</span> </span> </code></div>
<br />
假如你仔细的读过上面的代码，把数据库连接和查询的表替换成你的，那么就能看见它的执行效果哦。<br />
<br />
是不是很简单，只要动动脑筋，可以让它显示的更为个性化哦，给大家出一个小题，如何实现&#8220;首页 上一页 下一页 尾页&#8221;这种格式的分页呢？<br />
<br />
OK，水帖灌完，收工。^_^ <br />
辉老大『阿辉』:<br />
好帖子啊，我来顶默默的提问，代码，如下：<br />
<code><span style="color: #000000;">  <br />
<span style="color: #0000bb;">&lt;?php <br />
</span><span style="color: #ff8000;">/* <br />
Author:默默 <br />
Date&nbsp;&nbsp; :2006-12-03 <br />
*/ <br />
<br />
</span><span style="color: #0000bb;">$page</span><span style="color: #007700;">=isset(</span><span style="color: #0000bb;">$_GET</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'page'</span><span style="color: #007700;">])?</span><span style="color: #0000bb;">intval</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$_GET</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'page'</span><span style="color: #007700;">]):</span><span style="color: #0000bb;">1</span><span style="color: #007700;">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//这句就是获取page=18中的page的值，假如不存在page，那么页数就是1。 <br />
</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">=</span><span style="color: #0000bb;">10</span><span style="color: #007700;">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//每页显示10条数据 <br />
<br />
</span><span style="color: #0000bb;">$db</span><span style="color: #007700;">=</span><span style="color: #0000bb;">mysql_connect</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"localhost"</span><span style="color: #007700;">,</span><span style="color: #dd0000;">"root"</span><span style="color: #007700;">,</span><span style="color: #dd0000;">"7529639"</span><span style="color: #007700;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//创建数据库连接 <br />
</span><span style="color: #0000bb;">mysql_select_db</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"cr_download"</span><span style="color: #007700;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//选择要操作的数据库 <br />
<br />
/* <br />
首先咱们要获取数据库中到底有多少数据，才能判断具体要分多少页，具体的公式就是 <br />
总数据库除以每页显示的条数，有余进一。 <br />
也就是说10/3=3.3333=4 有余数就要进一。 <br />
*/ <br />
<br />
</span><span style="color: #0000bb;">$result</span><span style="color: #007700;">=</span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"select * from cr_userinfo"</span><span style="color: #007700;">); <br />
</span><span style="color: #0000bb;">$total</span><span style="color: #007700;">=</span><span style="color: #0000bb;">mysql_num_rows</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$result</span><span style="color: #007700;">); </span><span style="color: #ff8000;">//查询所有的数据 <br />
<br />
</span><span style="color: #0000bb;">$url</span><span style="color: #007700;">=</span><span style="color: #dd0000;">'test.php'</span><span style="color: #007700;">;</span><span style="color: #ff8000;">//获取本页URL <br />
<br />
//页码计算 <br />
</span><span style="color: #0000bb;">$pagenum</span><span style="color: #007700;">=</span><span style="color: #0000bb;">ceil</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$total</span><span style="color: #007700;">/</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//获得总页数,也是最后一页 <br />
</span><span style="color: #0000bb;">$page</span><span style="color: #007700;">=</span><span style="color: #0000bb;">min</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$pagenum</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$page</span><span style="color: #007700;">);</span><span style="color: #ff8000;">//获得首页 <br />
</span><span style="color: #0000bb;">$prepg</span><span style="color: #007700;">=</span><span style="color: #0000bb;">$page</span><span style="color: #007700;">-</span><span style="color: #0000bb;">1</span><span style="color: #007700;">;</span><span style="color: #ff8000;">//上一页 <br />
</span><span style="color: #0000bb;">$nextpg</span><span style="color: #007700;">=(</span><span style="color: #0000bb;">$page</span><span style="color: #007700;">==</span><span style="color: #0000bb;">$pagenum </span><span style="color: #007700;">? </span><span style="color: #0000bb;">0 </span><span style="color: #007700;">: </span><span style="color: #0000bb;">$page</span><span style="color: #007700;">+</span><span style="color: #0000bb;">1</span><span style="color: #007700;">);</span><span style="color: #ff8000;">//下一页 <br />
</span><span style="color: #0000bb;">$offset</span><span style="color: #007700;">=(</span><span style="color: #0000bb;">$page</span><span style="color: #007700;">-</span><span style="color: #0000bb;">1</span><span style="color: #007700;">)*</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//获取limit的第一个参数的值，假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。 <br />
<br />
//开始分页导航条代码： <br />
</span><span style="color: #0000bb;">$pagenav</span><span style="color: #007700;">=</span><span style="color: #dd0000;">"显示第 &lt;B&gt;"</span><span style="color: #007700;">.(</span><span style="color: #0000bb;">$total</span><span style="color: #007700;">?(</span><span style="color: #0000bb;">$offset</span><span style="color: #007700;">+</span><span style="color: #0000bb;">1</span><span style="color: #007700;">):</span><span style="color: #0000bb;">0</span><span style="color: #007700;">).</span><span style="color: #dd0000;">"&lt;/B&gt;-&lt;B&gt;"</span><span style="color: #007700;">.</span><span style="color: #0000bb;">min</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$offset</span><span style="color: #007700;">+</span><span style="color: #0000bb;">10</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$total</span><span style="color: #007700;">).</span><span style="color: #dd0000;">"&lt;/B&gt; 条记录，共 $total 条记录 "</span><span style="color: #007700;">; <br />
<br />
<br />
</span><span style="color: #ff8000;">//如果只有一页则跳出函数： <br />
</span><span style="color: #007700;">if(</span><span style="color: #0000bb;">$pagenum</span><span style="color: #007700;">&lt;=</span><span style="color: #0000bb;">1</span><span style="color: #007700;">) return </span><span style="color: #0000bb;">false</span><span style="color: #007700;">; <br />
<br />
</span><span style="color: #0000bb;">$pagenav</span><span style="color: #007700;">.=</span><span style="color: #dd0000;">" &lt;a href='$url?page=1'&gt;首页&lt;/a&gt; "</span><span style="color: #007700;">; <br />
if(</span><span style="color: #0000bb;">$prepg</span><span style="color: #007700;">) </span><span style="color: #0000bb;">$pagenav</span><span style="color: #007700;">.=</span><span style="color: #dd0000;">" &lt;a href='$url?page=$prepg'&gt;前页&lt;/a&gt; "</span><span style="color: #007700;">; else </span><span style="color: #0000bb;">$pagenav</span><span style="color: #007700;">.=</span><span style="color: #dd0000;">" 前页 "</span><span style="color: #007700;">; <br />
if(</span><span style="color: #0000bb;">$nextpg</span><span style="color: #007700;">) </span><span style="color: #0000bb;">$pagenav</span><span style="color: #007700;">.=</span><span style="color: #dd0000;">" &lt;a href='$url?page=$nextpg'&gt;后页&lt;/a&gt; "</span><span style="color: #007700;">; else </span><span style="color: #0000bb;">$pagenav</span><span style="color: #007700;">.=</span><span style="color: #dd0000;">" 后页 "</span><span style="color: #007700;">; <br />
</span><span style="color: #0000bb;">$pagenav</span><span style="color: #007700;">.=</span><span style="color: #dd0000;">" &lt;a href='$url?page=$pagenum'&gt;尾页&lt;/a&gt; "</span><span style="color: #007700;">; <br />
<br />
</span><span style="color: #ff8000;">//下拉跳转列表，循环列出所有页码： <br />
</span><span style="color: #0000bb;">$pagenav</span><span style="color: #007700;">.=</span><span style="color: #dd0000;">"　到第 &lt;select name='topage' size='1' onchange='window.location=""$url?page=""+this.value'&gt;"n"</span><span style="color: #007700;">; <br />
for(</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">=</span><span style="color: #0000bb;">1</span><span style="color: #007700;">;</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">&lt;=</span><span style="color: #0000bb;">$pagenum</span><span style="color: #007700;">;</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">++){ <br />
if(</span><span style="color: #0000bb;">$i</span><span style="color: #007700;">==</span><span style="color: #0000bb;">$page</span><span style="color: #007700;">) </span><span style="color: #0000bb;">$pagenav</span><span style="color: #007700;">.=</span><span style="color: #dd0000;">"&lt;option value='$i' selected&gt;$i&lt;/option&gt;"n"</span><span style="color: #007700;">; <br />
else </span><span style="color: #0000bb;">$pagenav</span><span style="color: #007700;">.=</span><span style="color: #dd0000;">"&lt;option value='$i'&gt;$i&lt;/option&gt;"n"</span><span style="color: #007700;">; <br />
} <br />
</span><span style="color: #0000bb;">$pagenav</span><span style="color: #007700;">.=</span><span style="color: #dd0000;">"&lt;/select&gt; 页，共 $pagenum 页"</span><span style="color: #007700;">; <br />
<br />
</span><span style="color: #ff8000;">//假如传入的页数参数大于总页数，则显示错误信息 <br />
</span><span style="color: #007700;">If(</span><span style="color: #0000bb;">$page</span><span style="color: #007700;">&gt;</span><span style="color: #0000bb;">$pagenum</span><span style="color: #007700;">){ <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Echo </span><span style="color: #dd0000;">"Error : Can Not Found The page "</span><span style="color: #007700;">.</span><span style="color: #0000bb;">$page</span><span style="color: #007700;">; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit; <br />
} <br />
<br />
</span><span style="color: #0000bb;">$info</span><span style="color: #007700;">=</span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"select * from cr_userinfo limit $offset,$num"</span><span style="color: #007700;">);&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//获取相应页数所需要显示的数据 <br />
</span><span style="color: #007700;">While(</span><span style="color: #0000bb;">$it</span><span style="color: #007700;">=</span><span style="color: #0000bb;">mysql_fetch_array</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$info</span><span style="color: #007700;">)){ <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Echo </span><span style="color: #0000bb;">$it</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'username'</span><span style="color: #007700;">]; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo </span><span style="color: #dd0000;">"&lt;br&gt;"</span><span style="color: #007700;">; <br />
}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #ff8000;">//显示数据 <br />
&nbsp;&nbsp;</span><span style="color: #007700;">echo</span><span style="color: #dd0000;">"&lt;br&gt;"</span><span style="color: #007700;">; <br />
&nbsp;&nbsp; echo </span><span style="color: #0000bb;">$pagenav</span><span style="color: #007700;">;</span><span style="color: #ff8000;">//输出分页导航 <br />
</span><span style="color: #0000bb;">?&gt;<br />
<br />
<br />
转自 http://www.cnblogs.com/justforfun/archive/2009/04/27/1444358.html<br />
</span></span></code>
<img src ="http://www.blogjava.net/ducklyl/aggbug/299154.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ducklyl/" target="_blank">王生生</a> 2009-10-21 10:08 <a href="http://www.blogjava.net/ducklyl/archive/2009/10/21/299154.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(转)PHP的单例模式</title><link>http://www.blogjava.net/ducklyl/archive/2008/11/15/240704.html</link><dc:creator>王生生</dc:creator><author>王生生</author><pubDate>Sat, 15 Nov 2008 09:52:00 GMT</pubDate><guid>http://www.blogjava.net/ducklyl/archive/2008/11/15/240704.html</guid><wfw:comment>http://www.blogjava.net/ducklyl/comments/240704.html</wfw:comment><comments>http://www.blogjava.net/ducklyl/archive/2008/11/15/240704.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ducklyl/comments/commentRss/240704.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ducklyl/services/trackbacks/240704.html</trackback:ping><description><![CDATA[<div id="blog_text" class="cnt">
<h4 style="margin-bottom: 0px; line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;">单例模式 有以下的特点： </strong></strong></h4>
<div style="font-size: 12px; filter: none; visibility: visible ! important; line-height: normal;">
<ol style="line-height: normal;">
    <li style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><font size="3">单例类只能有一个实例。</font></strong></strong></li>
    <li style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><font size="3">单例类必须自己创建自己的唯一的实例。</font></strong></strong></li>
    <li style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><font size="3">单例类必须给所有其他对象提供这一实例。</font></strong></strong></li>
</ol>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><font size="3">代码：</font> </strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><font size="3">Singleton.<strong style="color: black; line-height: normal; background-color: #ffff66;">php :<br style="line-height: normal;" />
<br style="line-height: normal;" />
&lt;?<strong style="color: black; line-height: normal; background-color: #ffff66;">php <br style="line-height: normal;" />
class Singleton<br style="line-height: normal;" />
{<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;  private static $instance;</strong></strong></font></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">&nbsp;&nbsp;&nbsp;  private function __construct()<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;  {<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;  }</font></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">&nbsp;&nbsp;&nbsp;  public static function getInstance()<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;  {<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  if(self::$instance == null)<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  {<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  self::$instance = new Singleton();<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  }</font></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  return self::$instance;<br style="line-height: normal;" />
&nbsp;&nbsp;&nbsp;  }<br style="line-height: normal;" />
}<br style="line-height: normal;" />
?&gt;</font></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">在使用的时候，因为构造方法是private（私有）的，所以是不能直接实例化的，必须使用类似下面的方法：</font></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><font size="3">例子：</font> </strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><font size="3">&lt;?<strong style="color: black; line-height: normal; background-color: #ffff66;">php <br style="line-height: normal;" />
require_once('Singleton.<strong style="color: black; line-height: normal; background-color: #ffff66;">php ');<br style="line-height: normal;" />
<br style="line-height: normal;" />
<u style="line-height: normal;">$instance = Singleton::getInstance();</u><br style="line-height: normal;" />
?&gt;</strong></strong></font></strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">============================================</font></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">其它关于静态的说明</font></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">静态成员. <br style="line-height: normal;" />
他在类被声明时就产生了，也就是在程序编译阶段产生的，它只有一个所有该类实例共享的原本，任何该类实例更改静态变量的值后，其它该类实例再去访问该静态变量，其值已经变成更改后的值，因为其在内存，就存一个原本。 <br style="line-height: normal;" />
非静态成员是在类实例化时产生的，你new一个该类实例，系统就会为该类实例的所有非静态成员新开辟一个空间，每个实例都只能自己的非静态成员。（有多少个类的实例，就要开辟多少个非静态成员的空间）</font></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">单例类全局只有一个实例，你可以把它看成全局对象。</font></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">从底层来说，编译完成后主要是两个部分，一是CPU指令，这一部分包含各种方法；二是数据，也就是程序定义的各种类型的变量。运行程序实例化一个对象时，按如下方式分配内存：<br style="line-height: normal;" />
1。在程序启动时将静态数据和静态方法放入堆内存中。<br style="line-height: normal;" />
2。第一次实例化对象时将其它方法放入代码段中。<br style="line-height: normal;" />
3。每次实例化对象时将其它数据放入栈内存中。<br style="line-height: normal;" />
实例化同一个对象时，上述1、2项不动，只是再加一个第３项即可。销毁一个实例的时候，如果还有其它的实例存在，就只释放该实例的栈数据段(上述第３项)。</font></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">根据以上原理回答您的问题<br style="line-height: normal;" />
静态方法和数据一般都是Public型的，用于类的极普遍的事物处理，可以在程序的任意地方调用，在整过程序运行过程中始终占用计算机内存，一搬少用。如果一个程序中的各个对象都用一个数据库的话，可以使用静态方法连接。</font></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">单件模式的实例销毁时可以释放所有的非静态方法和数据，，如果把这个类的所有方法都设计为静态的，这些方法将一直占用内存，浪费资源，不可取。</font></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">在new一个对象时，只是加一个数据段，并不重新加载方法和静态变量。</font></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">最后说一个变量传送的问题，网上很多朋友在回答类之间参数传递时都说用静态变量，我认为这种方法不好，本来加类的目的就是为了封装，如果用一个类似于全局变量的静态变量作为参数就大大削弱了封装的意义。</font></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">实际上，系统和应用程序对内存的管理相当复杂．<br style="line-height: normal;" />
=======================下面摘自思归的文字</font></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">从表面上看，静态(static)成员可以通过 类名.成员名 来直接调用，而实例(instance)成员需要生成一个对象后才能调用。同一个操作，性能当然静态成员好</font></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">但其实区别是跟对象设计有关的，一般来说实例成员跟实例的状态有关，某个方法的调用可能需要访问当前对象的状态，并改变其状态，从而影响其他方法的结果</font></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">而静态成员是跟类本身有关，与单独的实例状态无关。但静态成员不要太多了，否则就沦为以前的procedural programming风格了，也许对象需要重新设计或重构。</font></strong></strong></strong></strong></strong></strong></strong></strong></p>
<p style="line-height: normal;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #a0ffff;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="line-height: normal;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><strong style="color: black; line-height: normal; background-color: #ffff66;"><font size="3">静态成员往往会在多线程下操作，需要做同步化控制</font></strong></strong></strong></strong></strong></strong></strong></strong></p>
</div>
</div>
<img src ="http://www.blogjava.net/ducklyl/aggbug/240704.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ducklyl/" target="_blank">王生生</a> 2008-11-15 17:52 <a href="http://www.blogjava.net/ducklyl/archive/2008/11/15/240704.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>(转)优化PHP代码的40条建议</title><link>http://www.blogjava.net/ducklyl/archive/2008/11/15/240703.html</link><dc:creator>王生生</dc:creator><author>王生生</author><pubDate>Sat, 15 Nov 2008 09:51:00 GMT</pubDate><guid>http://www.blogjava.net/ducklyl/archive/2008/11/15/240703.html</guid><wfw:comment>http://www.blogjava.net/ducklyl/comments/240703.html</wfw:comment><comments>http://www.blogjava.net/ducklyl/archive/2008/11/15/240703.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ducklyl/comments/commentRss/240703.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ducklyl/services/trackbacks/240703.html</trackback:ping><description><![CDATA[<p>优化PHP代码的40条建议<br />
40 Tips for optimizing your php Code<br />
原文地址：<a href="http://reinholdweber.com/?p=3">http://reinholdweber.com/?p=3</a><br />
英文版权归Reinhold Weber所有，中译文作者yangyang（aka davidkoree）。双语版可用于非商业传播，但须注明英文版作者、版权信息，以及中译文作者。翻译水平有限，请广大PHPer指正。</p>
<p>1. If a method can be static, declare it static. Speed improvement is by a factor of 4. 如果一个方法可静态化，就对它做静态声明。速率可提升至4倍。</p>
<p>2. echo is faster than print. echo 比 print 快。</p>
<p>3. Use echo&#8217;s multiple parameters instead of string concatenation. 使用echo的多重参数（译注：指用逗号而不是句点）代替字符串连接。</p>
<p>4. Set the maxvalue for your for-loops before and not in the loop. 在执行for循环之前确定最大循环数，不要每循环一次都计算最大值。</p>
<p>5. Unset your variables to free memory, especially large arrays. 注销那些不用的变量尤其是大数组，以便释放内存。</p>
<p>6. Avoid magic like __get, __set, __autoload 尽量避免使用__get，__set，__autoload。</p>
<p>7. require_once() is expensive require_once()代价昂贵。</p>
<p>8. Use full paths in includes and requires, less time spent on resolving the OS paths. 在包含文件时使用完整路径，解析操作系统路径所需的时间会更少。</p>
<p>9. If you need to find out the time when the script started
executing, $_SERVER[&#8217;REQUEST_TIME&#8217;] is preferred to time()
如果你想知道脚本开始执行（译注：即服务器端收到客户端请求）的时刻，使用$_SERVER[&#8216;REQUEST_TIME&#8217;]要好于time()。</p>
<p>10. See if you can use strncasecmp, strpbrk and stripos instead of regex. 检查是否能用strncasecmp，strpbrk，stripos函数代替正则表达式完成相同功能。</p>
<p>11. str_replace is faster than preg_replace, but strtr is faster
than str_replace by a factor of 4.
str_replace函数比preg_replace函数快，但strtr函数的效率是str_replace函数的四倍。</p>
<p>12. If the function, such as string replacement function, accepts
both arrays and single characters as arguments, and if your argument
list is not too long, consider writing a few redundant replacement
statements, passing one character at a time, instead of one line of
code that accepts arrays as search and replace arguments.
如果一个字符串替换函数，可接受数组或字符作为参数，并且参数长度不太长，那么可以考虑额外写一段替换代码，使得每次传递参数是一个字符，而不是只写一行
代码接受数组作为查询和替换的参数。</p>
<p>13. It&#8217;s better to use select statements than multi if, else if, statements. 使用选择分支语句（译注：即switch case）好于使用多个if，else if语句。</p>
<p>14. Error suppression with @ is very slow. 用@屏蔽错误消息的做法非常低效。</p>
<p>15. Turn on apache&#8217;s mod_deflate 打开apache的mod_deflate模块。</p>
<p>16. Close your database connections when you&#8217;re done with them. 数据库连接当使用完毕时应关掉。</p>
<p>17. $row[&#8217;id&#8217;] is 7 times faster than $row[id]. $row[&#8216;id&#8217;]的效率是$row[id]的7倍。</p>
<p>18. Error messages are expensive. 错误消息代价昂贵。</p>
<p>19. Do not use functions inside of for loop, such as for ($x=0; $x
&lt; count($array); $x) The count() function gets called each time.
尽量不要在for循环中使用函数，比如for ($x=0; $x &lt; count($array);
$x)每循环一次都会调用count()函数。</p>
<p>20. Incrementing a local variable in a method is the fastest. Nearly
the same as calling a local variable in a function.
在方法中递增局部变量，速度是最快的。几乎与在函数中调用局部变量的速度相当。</p>
<p>21. Incrementing a global variable is 2 times slow than a local var. 递增一个全局变量要比递增一个局部变量慢2倍。</p>
<p>22. Incrementing an object property (eg. $this-&gt;prop++) is 3
times slower than a local variable.
递增一个对象属性（如：$this-&gt;prop++）要比递增一个局部变量慢3倍。</p>
<p>23. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one. 递增一个未预定义的局部变量要比递增一个预定义的局部变量慢9至10倍。</p>
<p>24. Just declaring a global variable without using it in a function
also slows things down (by about the same amount as incrementing a
local var). PHP probably does a check to see if the global exists.
仅定义一个局部变量而没在函数中调用它，同样会减慢速度（其程度相当于递增一个局部变量）。PHP大概会检查看是否存在全局变量。</p>
<p>25. Method invocation appears to be independent of the number of
methods defined in the class because I added 10 more methods to the
test class (before and after the test method) with no change in
performance. 方法调用看来与类中定义的方法的数量无关，因为我（在测试方法之前和之后都）添加了10个方法，但性能上没有变化。</p>
<p>26. Methods in derived classes run faster than ones defined in the base class. 派生类中的方法运行起来要快于在基类中定义的同样的方法。</p>
<p>27. A function call with one parameter and an empty function body
takes about the same time as doing 7-8 $localvar++ operations. A
similar method call is of course about 15 $localvar++ operations.
调用带有一个参数的空函数，其花费的时间相当于执行7至8次的局部变量递增操作。类似的方法调用所花费的时间接近于15次的局部变量递增操作。</p>
<p>28. Surrounding your string by &#8216; instead of " will make things
interpret a little faster since php looks for variables inside "&#8230;" but
not inside &#8216;&#8230;&#8217;. Of course you can only do this when you don&#8217;t need to
have variables in the string.
用单引号代替双引号来包含字符串，这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量，单引号则不会。当然，只有当你不需要在字符串中包含变
量时才可以这么做。</p>
<p>29. When echoing strings it&#8217;s faster to separate them by comma
instead of dot. Note: This only works with echo, which is a function
that can take several strings as arguments.
输出多个字符串时，用逗号代替句点来分隔字符串，速度更快。注意：只有echo能这么做，它是一种可以把多个字符串当作参数的&#8220;函数&#8221;（译注：PHP手册
中说echo是语言结构，不是真正的函数，故把函数加上了双引号）。</p>
<p>30. A PHP script will be served at least 2-10 times slower than a
static HTML page by Apache. Try to use more static HTML pages and fewer
scripts. Apache解析一个PHP脚本的时间要比解析一个静态HTML页面慢2至10倍。尽量多用静态HTML页面，少用脚本。</p>
<p>31. Your PHP scripts are recompiled every time unless the scripts
are cached. Install a PHP caching product to typically increase
performance by 25-100% by removing compile times.
除非脚本可以缓存，否则每次调用时都会重新编译一次。引入一套PHP缓存机制通常可以提升25%至100%的性能，以免除编译开销。</p>
<p>32. Cache as much as possible. Use memcached - memcached is a
high-performance memory object caching system intended to speed up
dynamic web applications by alleviating database load. OP code caches
are useful so that your script does not have to be compiled on every
request.
尽量做缓存，可使用memcached。memcached是一款高性能的内存对象缓存系统，可用来加速动态Web应用程序，减轻数据库负载。对运算码
(OP code)的缓存很有用，使得脚本不必为每个请求做重新编译。</p>
<p>33. When working with strings and you need to check that the string
is either of a certain length you&#8217;d understandably would want to use
the strlen() function. This function is pretty quick since it&#8217;s
operation does not perform any calculation but merely return the
already known length of a string available in the zval structure
(internal C struct used to store variables in PHP). However because
strlen() is a function it is still somewhat slow because the function
call requires several operations such as lowercase &amp; hashtable
lookup followed by the execution of said function. In some instance you
can improve the speed of your code by using an isset() trick.
当操作字符串并需要检验其长度是否满足某种要求时，你想当然地会使用strlen()函数。此函数执行起来相当快，因为它不做任何计算，只返回在zval
结构（C的内置数据结构，用于存储PHP变量）中存储的已知字符串长度。但是，由于strlen()是函数，多多少少会有些慢，因为函数调用会经过诸多步
骤，如字母小写化（译注：指函数名小写化，PHP不区分函数名大小写）、哈希查找，会跟随被调用的函数一起执行。在某些情况下，你可以使用isset()
技巧加速执行你的代码。</p>
<p>Ex.（举例如下）<br />
if (strlen($foo) &lt; 5) { echo "Foo is too short"; }<br />
vs.（与下面的技巧做比较）<br />
if (!isset($foo{5})) { echo "Foo is too short"; }</p>
<p>Calling isset() happens to be faster then strlen() because unlike
strlen(), isset() is a language construct and not a function meaning
that it&#8217;s execution does not require function lookups and lowercase.
This means you have virtually no overhead on top of the actual code
that determines the string&#8217;s length.
调用isset()恰巧比strlen()快，因为与后者不同的是，isset()作为一种语言结构，意味着它的执行不需要函数查找和字母小写化。也就是
说，实际上在检验字符串长度的顶层代码中你没有花太多开销。</p>
<p>34. When incrementing or decrementing the value of the variable $i++
happens to be a tad slower then ++$i. This is something PHP specific
and does not apply to other languages, so don&#8217;t go modifying your C or
Java code thinking it&#8217;ll suddenly become faster, it won&#8217;t. ++$i happens
to be faster in PHP because instead of 4 opcodes used for $i++ you only
need 3. Post incrementation actually causes in the creation of a
temporary var that is then incremented. While pre-incrementation
increases the original value directly. This is one of the optimization
that opcode optimized like Zend&#8217;s PHP optimizer. It is still a good
idea to keep in mind since not all opcode optimizers perform this
optimization and there are plenty of ISPs and servers running without
an opcode optimizer.
当执行变量$i的递增或递减时，$i++会比++$i慢一些。这种差异是PHP特有的，并不适用于其他语言，所以请不要修改你的C或Java代码并指望它
们能立即变快，没用的。++$i更快是因为它只需要3条指令(opcodes)，$i++则需要4条指令。后置递增实际上会产生一个临时变量，这个临时变
量随后被递增。而前置递增直接在原值上递增。这是最优化处理的一种，正如Zend的PHP优化器所作的那样。牢记这个优化处理不失为一个好主意，因为并不
是所有的指令优化器都会做同样的优化处理，并且存在大量没有装配指令优化器的互联网服务提供商（ISPs）和服务器。</p>
<p>35. Not everything has to be OOP, often it is too much overhead,
each method and object call consumes a lot of memory.
并不是事必面向对象(OOP)，面向对象往往开销很大，每个方法和对象调用都会消耗很多内存。</p>
<p>36. Do not implement every data structure as a class, arrays are useful, too. 并非要用类实现所有的数据结构，数组也很有用。</p>
<p>37. Don&#8217;t split methods too much, think, which code you will really re-use. 不要把方法细分得过多，仔细想想你真正打算重用的是哪些代码？</p>
<p>38. You can always split the code of a method later, when needed. 当你需要时，你总能把代码分解成方法。</p>
<p>39. Make use of the countless predefined functions. 尽量采用大量的PHP内置函数。</p>
<p>40. If you have very time consuming functions in your code, consider
writing them as C extensions. 如果在代码中存在大量耗时的函数，你可以考虑用C扩展的方式实现它们。</p>
<p>41. Profile your code. A profiler shows you, which parts of your
code consumes how many time. The Xdebug debugger already contains a
profiler. Profiling shows you the bottlenecks in overview.
评估检验(profile)你的代码。检验器会告诉你，代码的哪些部分消耗了多少时间。Xdebug调试器包含了检验程序，评估检验总体上可以显示出代码
的瓶颈。</p>
<p>42. mod_gzip which is available as an Apache module compresses your
data on the fly and can reduce the data to transfer up to 80%.
mod_zip可作为Apache模块，用来即时压缩你的数据，并可让数据传输量降低80%。</p>
<p>43. Excellent Article （<a href="http://phplens.com/lens/php-book/optimizing-debugging-php.php">http://phplens.com/lens/php-book/optimizing-debugging-php.php</a>）about optimizing php by John Lim 另一篇优化PHP的精彩文章，由John Lim撰写。</p>
<img src ="http://www.blogjava.net/ducklyl/aggbug/240703.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ducklyl/" target="_blank">王生生</a> 2008-11-15 17:51 <a href="http://www.blogjava.net/ducklyl/archive/2008/11/15/240703.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>（转）PHP cookie和session的分析</title><link>http://www.blogjava.net/ducklyl/archive/2008/09/25/231193.html</link><dc:creator>王生生</dc:creator><author>王生生</author><pubDate>Thu, 25 Sep 2008 13:04:00 GMT</pubDate><guid>http://www.blogjava.net/ducklyl/archive/2008/09/25/231193.html</guid><wfw:comment>http://www.blogjava.net/ducklyl/comments/231193.html</wfw:comment><comments>http://www.blogjava.net/ducklyl/archive/2008/09/25/231193.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/ducklyl/comments/commentRss/231193.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/ducklyl/services/trackbacks/231193.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 1.&nbsp;PHP的COOKIEcookie&nbsp;是一种在远程浏览器端储存数据并以此来跟踪和识别用户的机制。PHP在http协议的头信息里发送cookie,&nbsp;因此&nbsp;setcookie()&nbsp;函数必须在其它信息被输出到浏览器前调用，这和对&nbsp;header()&nbsp;函数的限制类似。1.1&nbsp;设置cookie:&nbsp...&nbsp;&nbsp;<a href='http://www.blogjava.net/ducklyl/archive/2008/09/25/231193.html'>阅读全文</a><img src ="http://www.blogjava.net/ducklyl/aggbug/231193.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/ducklyl/" target="_blank">王生生</a> 2008-09-25 21:04 <a href="http://www.blogjava.net/ducklyl/archive/2008/09/25/231193.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>