PHP的setcookie默认不对加号转义,太坑 (PHP心得)
发布于 2020-03-09 00:37 阅读:1,857 评论:0 标签: cookie 加号 空格

PHP 手册对 setcookie 的使用范例里提到:

注意:在发送 Cookie 时,值的部分会被自动 urlencode 编码。收到 Cookie 时,会自动解码,并赋值到可变的 Cookie 名称上。 如果不想被编码,可以使用 setrawcookie() 代替――如果你的 PHP 版本是 5 及以上。

里面的措辞:“值的部分会被自动 urlencode 编码”令人佩服。

加号“+”不在发送时“值的部分”不被自动编码,但是在收到时,却被自动 urldecode 了:一来一回,加号“+”变成了空格。

太坑了,还是手动 rawurlencode 和 rawurldecode 比较靠谱。

展开全文  
收起全文  
函数外的引用最好用后即焚 (PHP心得)
发布于 2018-01-18 23:30 阅读:23,982 评论:0 标签: 引用

为方便,喜欢这个写法:

foreach($array as &$_arr) {
    $_arr["key"] = "value";
}

但比较坑的是,$_arr可能后续再继续使用,这是便赋予了其它值,直接导致$array最后一个变量被改变。

为避免这种,最好用后即焚:

foreach($array as &$_arr) {
    $_arr["key"] = "value";
}
unset($_arr);

今天在vim中直接手写代码,历史惯用写法不加思索,有点超出预期啊

展开全文  
收起全文  
PHP类中变量的初始化只能是定值 (PHP心得)
发布于 2010-04-14 18:19 阅读:160,866 评论:0 标签: Opcodes 初始化

    首先看代码:

以下是引用片段:
class test
{
        //private $_expire_time =  604800; // 604800 = 60 * 60 * 24 * 7
        private $_expire_time =  60 * 60 * 24 * 7;
        public function hehe()
        {
                echo $this->_expire_time;
        }
}
$a = new test;
$a->hehe();
?>
[root@sso115 append]# php test.php
PHP Parse error:  syntax error, unexpected ’*’, expecting ’,’ or ’;’ in /data1/f2r/append/test.php on line 5
Parse error: syntax error, unexpected ’*’, expecting ’,’ or ’;’ in /data1/f2r/append/test.php on line 5

    杯具了,这句话有语法错误:

以下是代码片段:
private $_expire_time =  60 * 60 * 24 * 7;

    那么,这是为什么呢?

    查看手中的手册,没发现有解释,查看官方文档:http://www.php.net/manual/en/language.oop5.properties.php,如下:

以下是引用片段:
Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

    如红色字体,翻译如下:

以下是引用片段:
    这个声明可能会包含初始化,当时这个初始化必须是一个定值,也就是说,这个定值必须在编译时就能确定,而不是依赖于php在运行时再确定值。

    呃,翻译的有点拗口。简而言之,就是说类里面变量的初始化不能是一个表达式,否则编译期间就编译不过去,产生不了Opcodes。

    手册中给出错误和正确的初始化的例子:

以下是代码片段:
class SimpleClass
{
   // invalid property declarations:
   public $var1 = ’hello ’ . ’world’;
   public $var2 = <<hello world
EOD;
   public $var3 = 1+2;
   public $var4 = self::myStaticMethod();
   public $var5 = $myVar;

   // valid property declarations:
   public $var6 = myConstant;
   public $var7 = array(true, false);

   // This is allowed only in PHP 5.3.0 and later.
   public $var8 = <<<’EOD’
hello world
EOD;
}
?>

    关于Opcodes和编译的过程,可以参考以下资料:

以下是引用片段:
深入理解PHP原理之Opcodes
http://www.laruence.com/2008/06/18/221.html

PHP编译缓存
http://www.allniu.com/2010/0108/3820.html

实现PHP的编译执行分离(separating compilation and execution)
http://www.phpchina.com/index.php?action-viewnews-itemid-34001

    说句题外话:

    据说,现在PHP官网没有提供中文文档入口,是因为中文文档没人维护的原因,杯具!:http://opensource.solidot.org/article.pl?sid=08/04/22/2359251

    不过随便进入文档任意语言页面,切换语言中选 "Other",还是可以进入中文文档的。

展开全文  
收起全文  
整型(int)数字溢出在程序和数据库设计中的考虑 (PHP心得)
发布于 2009-07-02 20:13 阅读:101,046 评论:0 标签: 溢出

    在数据库设计和程序中需要考虑数字的范围,否则可能导致一些问题。主要是考虑溢出的问题,比如如果数据库中有一个整型的数字字段,里面的数据可能随着业务的增长而膨胀,而这个数字有可能会超出列属性的范围,也就是溢出,与此同时,程序中也需要处理这个日益庞大的数字,如果其中有运算、数字类型的逻辑比较等等,也可能导致某天就出现了异常。而这种错误又是难以发现的。

    以下试以整型(int)抛砖引玉:

    一:MySQL5

    以MySQL5版本为例,大多数管理员可能把自增数字、或者其它应用数字字段的列属性设置为int类型,int占用4个字节,而int又分为无符号型和有符号性。对于无符号型的范围是0 到 4294967295;有符号型的范围是-2147483648 到 2147483647。参考资料可见mysql手册:11.2. 数值类型.

    当要在一个数值列内保存一个超出该列允许范围的值时,MySQL的操作取决于此时有效的SQL模式。如果模式未设置,MySQL将值裁剪到范围的相应端点,并保存裁减好的值。但是,如果模式设置为traditional(“严格模式”),超出范围的值将被拒绝并提示错误,并且根据SQL标准插入会失败。请参见mysql手册5.3.2节:“SQL服务器模式”。

    如果INT列是UNSIGNED,列范围的大小相同,但其端点会变为到0和4294967295。如果你试图保存-9999999999和9999999999,以非严格模式保存到列中的值是0和4294967296。

    如果在浮点或定点列中分配的值超过指定(或默认)精度和标度规定的范围,MySQL以非严格模式保存表示范围相应端点的值。

    当MySQL没有工作在严格模式时,对于ALTER TABLE、LOAD DATA INFILE、UPDATE和多行INSERT语句,由于裁剪发生的转换将报告为警告。当MySQL工作在严格模式时,这些语句将失败,并且部分或全部值不会插入或更改,取决于是否表为事务表和其它因素。详情参见mysql手册5.3.2节:“SQL服务器模式”。

    二:php5:
 
    1:整型数的字长和平台有关,PHP 不支持无符号整数。
    2:如果给定的一个数超出了 integer 的范围,将会被解释为 float。同样如果执行的运算结果超出了 integer 范围,也会返回 float。如果在程序中有对数字类型做比较,可能会产生问题。
    3:可以查看PHP_INT_SIZE、PHP_INT_MAX,以确定整数的范围。
 
    以下列子可供参考:
 
    在32位服务器下:

以下是代码片段:
[shengting@localhost ~]$ php -r "echo PHP_INT_SIZE;"
4
[shengting@localhost ~]$ php -r "echo PHP_INT_MAX;"
2147483647
[shengting@localhost ~]$ php -r "var_dump(2147483647);"
int(2147483647)
[shengting@localhost ~]$ php -r "var_dump(2147483648);"
float(2147483648)
[shengting@localhost ~]$ php -r "var_dump(-2147483647);"
int(-2147483647)
[shengting@localhost ~]$ php -r "var_dump(-2147483648);"
float(-2147483648)
[shengting@localhost ~]$ php -r "var_dump(4294967295);"
float(4294967295)
[shengting@localhost ~]$ php -r "var_dump(4294967296);"
float(4294967296)


    在64位服务器下:
 

以下是代码片段:
[root@login shengting]# php -r "echo PHP_INT_SIZE;"
8
[root@login shengting]# php -r "echo PHP_INT_MAX;"
9223372036854775807
[root@login shengting]# php -r "var_dump(2147483647);"
int(2147483647)
[root@login shengting]# php -r "var_dump(2147483648);"
int(2147483648)
[root@login shengting]# php -r "var_dump(-2147483647);"
int(-2147483647)
[root@login shengting]# php -r "var_dump(-2147483648);"
int(-2147483648)
[root@login shengting]# php -r "var_dump(4294967295);"
int(4294967295)
[root@login shengting]# php -r "var_dump(4294967296);"
int(4294967296)

    三:C/C++
 
    对C/C++也存在有符号和无符号类型的问题。
 
    对于32位系统,如果使用有符号int、long来定义变量保存唯一号就可能出现溢出,并出现上述问题。

    对于64位系统,如果使用int来定义变量保存唯一号就可能出现溢出,并出现上述问题。
 
    可参考 http://www.ibm.com/developerworks/cn/linux/l-port64.html

展开全文  
收起全文  
PHP的变量也不是随意能转化的及bash的算术运算 (PHP心得)
发布于 2008-12-30 20:47 阅读:12,094 评论:0 标签: array bash scalar

      一直以为PHP的变量是很灵活的,类型想怎么变换就怎么变换。

      但实际上并不是这样,当一个变量被赋值为数字时,是变换不了数组的,比如:

以下是代码片段:

[root@localhost shengting]# cat test.php
<?php
$a = 454;
$b = 6;
$a['gfgo'] = $b;

var_dump($a);
?>

[root@localhost shengting]# php test.php
PHP Warning:  Cannot use a scalar value as an array in /usr/home/shengting/test.php on line 4

Warning: Cannot use a scalar value as an array in /usr/home/shengting/test.php on line 4
int(454)

      从var_dump的结果看,不仅有Warning级的错误,赋值也没有成功。而,如果变量一开始是字符串的话,就没这回事情了:

以下是引用片段:

[root@localhost shengting]# cat test.php
<?php
$a = `454`;
$b = 6;
$a['gfgo'] = $b;

var_dump($a);
?>
[root@localhost shengting]# php test.php
sh: 454: command not found
array(1) {
  ["gfgo"]=>
  int(6)
}

      可以看出,赋值是成功的。

      应该,这个是和存储的类型是有联系的。同样,在Shell的Bash中,所有的变量的值都是以字符串方式存储的。尽管这个特性使得bash的数据处理起来很容易,但是也使得数字运算比较困难。原因就是数字也是以字符串的形式存储的,

      在Bash中,如果要对数字进行算术运算和逻辑操作,必须先转化为整数(可视为中间值),得到运算结果后再转化为字符串,以便正确的保存在Bash变量中。

      Bash提供了三种方式的数字数据进行算术变量:

以下是引用片段:

1:使用let命令。
2:使用shell扩展 $((expression))
3:使用expr命令

      具体用法就不说了,哈哈。偶比较喜欢使用第二种。

展开全文  
收起全文  
PHP显示出错提示的三种方法 (PHP心得)
发布于 2008-05-16 20:04 1 阅读:25,532 评论:1 标签: php 出错提示 方法

  PHP在页面上显示出错消息并让用户看见是开发人员怎么样也不愿意面对的,但是谁都不想在一台没有配置成显示出错消息的服务器上用PHP开发代码。开发时显示PHP的错误消息对开发人员来说是个明智的选择,所谓工具是使人方便的,就是如此。

  下面来说说显示PHP错误提示消息的三个方法。

  一:php.ini配置

  php.ini配置中与此相关的有两个配置变量。下面是这两个变量及其默认值:

以下是引用片段:
display_errors = Off
error_reporting = E_ALL & ~E_NOTICE

  display_errors 变量的目的很明显 ―― 它告诉PHP是否显示错误。默认值是 Off。现在我们的目的是显示错误提示,那么:

以下是引用片段:
display_errors = On

  E_ALL,这个设置会显示从不良编码实践到无害提示到出错的所有信息。E_ALL 对于开发过程来说有点太细,因为它连变量未初始化也显示提示,而这一点正是PHP“高级”的一个特征。幸好,error_reporting的默认值是“E_ALL & ~E_NOTICE”,这样就只看到错误和不良编码了,对程序无不利的提示则不会显示。

  修改php.ini后需要重新启动Apache,这样才可以在apache中生效,当然你如果只在命令行下测试程序,是不需要这一步的。

  二:Apache的httpd.conf和.htaccess配置

  如果要在.htaccess 文件中的指令来修改 PHP 的配置设定。需要有“AllowOverride Options”或“AllowOverride All”权限才可以。 请参见:AllowOverride 指令http://lamp.linux.gov.cn/Apache/ApacheMenu/mod/core.html#allowoverride

  AllowOverride指令确定允许存在于.htaccess文件中的指令类型,它仅在不包含正则表达式的配置段中才是有效的。

  如果此指令被设置为None ,那么.htaccess文件将被完全忽略。事实上,服务器根本不会读取.htaccess文件。

  当此指令设置为 All时,所有具有".htaccess"作用域的指令都允许出现在.htaccess文件中。

  例如以下指令只允许在.htaccess中使用AuthConfig和Indexes组的指令:

以下是引用片段:
AllowOverride AuthConfig Indexes

  .htaccess里的设置和httpd.conf是一样的。

  要在做在httpd.conf实现这一功能,需要把下列各行添加到 httpd.conf,以覆盖php.ini文件做出的配置:

以下是引用片段:
php_flag  display_errors        on
php_value error_reporting       2039

  这会覆盖在 php.ini 文件中为 display_errors 已经设置的标志,以及 error_reporting 的值。值 2039 代表 E_ALL & ~E_NOTICE。如果愿意采用 E_ALL,请把值设为 2047。同样,还是要重启 Apache。

  httpd.conf对php宏定义设置的具体用法可以参见:http://www.php.net/manual/zh/configuration.changes.php

  你可以注意到,上面我们没有使用“E_ALL & ~E_NOTICE”这样的宏定义。这是因为这些宏定义都是php的变量,apache是不认识的,所以我们使用与其等同的值来设置。相关的信息和说明请见:http://www.php.net/manual/zh/ref.errorfunc.php#errorfunc.constants

  下面给出一个简单的对应值:

以下是引用片段:
值 宏定义
1 E_ERROR 
2 E_WARNING 
4 E_PARSE 
8 E_NOTICE 
16 E_CORE_ERROR 
32 E_CORE_WARNING 
64 E_COMPILE_ERROR 
128 E_COMPILE_WARNING 
256 E_USER_ERROR 
512 E_USER_WARNING 
1024 E_USER_NOTICE 
6143 E_ALL 
2048 E_STRICT 
4096 E_RECOVERABLE_ERROR

  三:程序中改变

  以上所介绍的都是配置文件里面做设置,如果你没有这个权限,或者只想测试一个程序,那么你可以使用两个函数来做到这一点。

  它们分别是:

以下是引用片段:
string ini_set ( string $varname , string $newvalue )

int error_reporting ([ int $level ] )

  两个函数大同小异,error_reporting()看上去是ini_set()的error_reporting功能版。事实上我认为也的确如此。

  在ini_set()中你可以设置display_errors和error_reporting的值,而在error_reporting()中你只能设置error_reporting的值,也就是说,如果服务器默认不显示错误提示,你在error_reporting()中怎么变换都是不管用的。两个函数的参数都可以参见上面提到的数字--宏定义对应值。

  需要注意,以上两个函数的适用用时间为该函数起,一直到程序结束。它们不会影响到其他的程序。

  最后:关于PHP的宏定义

  最后,仍需要说明一点:无论httpd.conf还是ini_set()所设置的宏定义并不是无限的,更多的宏定义值请参看手册中的“php.ini 配置选项列表”:http://www.php.net/manual/zh/ini.php

  在里面列表中有一列为“可修改范围”,里面的值为PHP_INI_* ,所以在使用时需要看清宏定义的可修改范围。在列表的末尾,手册中有提示:

以下是引用片段:
PHP_INI_* 常量的定义
常量 值 含义
PHP_INI_USER 1 配置选项可在用户的 PHP 脚本或 Windows 注册表中设置
PHP_INI_PERDIR 2 配置选项可在 php.ini, .htaccess 或 httpd.conf 中设置
PHP_INI_SYSTEM 4 配置选项可在 php.ini or httpd.conf 中设置
PHP_INI_ALL 7 配置选项可在各处设置

  本文参考资料:

以下是引用片段:

PHP 程序员的调试技术
http://www.ibm.com/developerworks/cn/opensource/os-debug/

AllowOverride 指令
http://lamp.linux.gov.cn/Apache/ApacheMenu/mod/core.html#allowoverride

怎样修改配置设定
http://www.php.net/manual/zh/configuration.changes.php

php.ini 配置选项
http://www.php.net/manual/zh/ini.php

运行时配置
http://www.php.net/manual/zh/ref.errorfunc.php#ini.error-reporting

ini_set()
http://www.php.net/manual/zh/function.ini-set.php

error_reporting()
http://www.php.net/manual/zh/function.error-reporting.php

display_errors
http://www.php.net/manual/zh/ref.errorfunc.php#ini.display-errors

预定义常量
http://www.php.net/manual/zh/ref.errorfunc.php#errorfunc.constants

展开全文  
收起全文  
循环中的逻辑陷阱 (PHP心得)
发布于 2008-04-10 18:42 1 阅读:8,130 评论:1 标签: 陷阱 循环

  循环是个好东西,用循环加几条简单的语言可以实现处理多条数据的功能,充分展示了计算机是为人民服务的。但是如果利用不好,和谐社会便被打破了。现举两个陷阱。

  一:双重循环中建立的临时变量。

  首先临时变量就是临时的,不能和全局变量起重名,除非这个全局变量是个全局的临时变量(随用随赋值)。这一点在需要临时变量与某个变量做比较操作时尤为重要。现在举一个双重循环的例子:

以下是代码片段:
foreach($arr0 as $k0 => $arr1)
{
     foreach($arr1 as $k1 => $v1)
     {
          $total +=$v1;
     }
 
     if($total > 1000)
     {
          echo "No !\n";
     }
}

  如果你只是想单独分析数据$arr1,那很遗憾这个程序的结果是失败的。因为$total的值会被下一次循环继续累加,然后在判断大小的时候会一路报错。如果你在第二次循环前把$total置0就OK了。

  二:有序值查漏的陷阱。

  我想,这是思维严谨的问题。

  但一个有序排列的数组,我们可以简化为一个数字为键的数组$arr。原始数组是否有序是无所谓了,因为有ksort()函数,可以帮助我们排序。这个数字有序但不连续,例如键为:1、2、6、7,中间漏了3、4、5,现在弄个程序找出来。

  假定数组的键起点为0,终点以$arr为准。我一拍脑袋,写下如下程序:

以下是代码片段:
$i = 0;
foreach($arr as $k => $v)
{
     if($i++ != $v)
     {
          echo "$v | ";
     }
}

  此程序的恶果为,一旦遇到不连续的键,以后的键值都被认作不连续了,原因在于$i的自增方式有问题。你可以自己实践一下加强体验。

  有效的程序如下,用while我们可以避免风险。

以下是代码片段:
$i = -1;
foreach ($arr as $k => $v)
{
     while(++$i != $k)
     {
          echo "$i | ";
     }
}

  当然,实际程序中还得考虑$i溢出的问题。

展开全文  
收起全文  
PHP字符串类型转化的例子 (PHP心得)
发布于 2008-03-28 13:01 1 阅读:12,141 评论:1 标签: PHP 类型

以下是代码片段:
if('dgfdg' == 0)
   echo 'Yes';
else
   echo 'No';
?>

  上面的程序会输出什么?这个例子是从其他blog上看见的,时间长了记不清是那个blog了。这个blog上说,如果答不出来说明基础知识还不够。偶很惭愧,觉得自己的回答没有底气。于是实践了一下。

  发现这是个PHP类型转换的例子,字符串与数字同时操作的时候,字符串会转化为整型,看下面的例子

以下是引用片段:
[root@login yayu]# php -r "if('gfd' == 0) echo 'Yes'; else echo 'No'; echo \"\n\";"
Yes
[root@login yayu]# php -r "if('gfd544' == 0) echo 'Yes'; else echo 'No'; echo \"\n\";"
Yes
[root@login yayu]# php -r "if('56gfd544' == 0) echo 'Yes'; else echo 'No'; echo \"\n\";"
No
[root@login yayu]# php -r "if('5gfd544' == 5) echo 'Yes'; else echo 'No'; echo \"\n\";"
Yes

  再看一下,字符串是如何转化为整型的:

以下是引用片段:
[root@login shengting]# php -r "echo (int) 10; echo \"\n\";"
10
[root@login shengting]# php -r "echo (int) '10gdfgfd'; echo \"\n\";"
10
[root@login shengting]# php -r "echo (int) 'gdfgfd'; echo \"\n\";"
0
[root@login shengting]# php -r "echo (int) 'gdfgfd10'; echo \"\n\";"
0

  从上面可以看出,转换的时候和第一个字符有很大关系。纯字母会直接转换为0,字母开头的字符串也会转换为0,只有以数字开头才会转换为该字符串前几个数字,此时有字母在后则完全截断了。

  下面是手册上的例子:

以下是代码片段:
$foo "10.5";                // $foo is float (11.5)
$foo "-1.3e3";              
// $foo is float (-1299)
$foo "bob-1.3e3";           
// $foo is integer (1)
$foo "bob3";                
// $foo is integer (1)
$foo "10 Small Pigs";       
// $foo is integer (11)
$foo "10.2 Little Piggies"
// $foo is float (14.2)
$foo "10.0 pigs " 1;          
// $foo is float (11)
$foo "10.0 pigs " 1.0;        
// $foo is float (11)
?>

展开全文  
收起全文  
PHP加速器eAccelerator文档翻译 (PHP心得)
发布于 2007-11-06 11:02 1 阅读:62,097 评论:1 标签: eAccelerator Linux PHP 翻译 文档

  前端时间用到这个,看了一下英文文档觉得还是看最原始的资料看到的东西最全。比如说eAccelerator自带了共享内存管理的程序,这个在我找到的中文资料里面就没有看到过。

  本文翻译的是:Release-0.9.5.2 ,官网是:http://www.eaccelerator.net/。原文附后,英文水平不高,如果您发现有不对的地方,还请告诉我!

--------------------------

PHP的eAccelerator
=====================

什么是eAccelerator?
----------------------
eAccelerator是一个的免费、开源的PHP模块,它能够为提供PHP加速、优化、加码、和动态内容缓存功能。它通过存储PH脚本编译后的状态而加快执行PHP脚本的速度,而不需要频繁的编译这个PHP脚本。而且它能优化PHP脚本,以提高执行PHP的速度。eAccelerator特色是减少了服务器负载、使PHP脚本加速1-10倍。

eAccelerator是TurckMMCache的一个分支
(请详见Dmitry Stogov维护的:http://sourceforge.net/project/turckmm-cache/

eAccelerator把编译好的PHP程序存储在共享内存里面,并直接在这里面执行程序。但在共享内存里面寻找编译好的PHP程序时,会在很短的时间内产生一些锁定,所以一个程序可以被多个进程同时执行。不适合放入共享内存的文件将被缓存到硬盘上。

eAccelerator包含了一个PHP加码器和解释器(loader????)。你可以使用encoder.php对你的程序加码,这样在分发程序的时候就可以不给出源码了。加码后的文件可以在任何机子上运行,只要它装有eAccelerator的PHP。加码后的代码不能被恢复,因为它是以编译完了的形式而存储的,而且存储时不包含源码。当然,程序中一些内部的东西可以使用反编译工具(different  reverse  engineering  tools)还原(例如:disassemblers, debuggers等等),但这并不是重要的。

eAccelerator可以和Zend Optimizer的加码器共存。但是在php.ini中Zend Optimizer必须在eAccelerator配置参数的后面。如果你不使用Zend Optimizer的加码器,我们不推荐同时安装Zend Optimizer

eAccelerator不能在CGI模式下运行,但是它能在一些web服务器下的Fast-CGI模式运行,比如:lighttpd。

下面是一些能提供相同作用的产品:
 - Zend Performance Suite (http://www.zend.com/)
 - Alternative PHP Cache (http://pecl.php.net/package/APC)

下载
--------
最新的eAccelerator版本能在下面的页面下载:
http://sourceforge.net/projects/eaccelerator/
版本控制的快照在:
http://snapshots.eaccelerator.net

要求:
--------
apache >= 1.3, mod_php >= 4.1, linux下工具:autoconf, automake, libtool, m4

兼容性:
--------
eAccelerator能与PHP4、PHP5很好的工作,对PHP5.1版本还没很好的兼容。它能使用在Linux, FreeBSD, MacOS X,
Solaris 和 Windows 下的 apache 1.3 and 2, lighttpd 和 IIS.

快速安装:
-------------

你可以在下面的网站上找到更多的安装信息:
http://eaccelerator.sourceforge.net/

注意:在Microsoft Windows下安装,请参考README.win32文件

第一步:编译eaccelerator

  export PHP_PREFIX="/usr"
 
  $PHP_PREFIX/bin/phpize
 
  ./configure \
  --enable-eaccelerator=shared \
  --with-php-config=$PHP_PREFIX/bin/php-config
 
  make

  你必须使用“export”命令指明PHP的安装路径的前缀,它可能是"/usr" "/usr/local",或其它

第二布:安装 eAccelerator

  make install

第三步:配置 eAccelerator

  eAccelerator能做为Zend或PHP的扩展。

对于大于0.9.1版本的eaccelerator,如果你有/etc/php.d目录,你应该复制eaccelerator.ini到里面去,当然你可以根据你的需要修改它。

如果没有,你需要修改你的php.ini文件(通常是/etc/php.ini)

作为Zend的扩展:

  zend_extension="/usr/lib/php4/eaccelerator.so"
  eaccelerator.shm_size="16"
  eaccelerator.cache_dir="/tmp/eaccelerator"
  eaccelerator.enable="1"
  eaccelerator.optimizer="1"
  eaccelerator.check_mtime="1"
  eaccelerator.debug="0"
  eaccelerator.filter=""
  eaccelerator.shm_max="0"
  eaccelerator.shm_ttl="0"
  eaccelerator.shm_prune_period="0"
  eaccelerator.shm_only="0"
  eaccelerator.compress="1"
  eaccelerator.compress_level="9"

  如果你使用thread safe模式安装的PHP,你应该使用“zend_extension_ts”代替“zend_extension”

作为PHP的扩展:

  extension="eaccelerator.so"
  eaccelerator.shm_size="16"
  eaccelerator.cache_dir="/tmp/eaccelerator"
  eaccelerator.enable="1"
  eaccelerator.optimizer="1"
  eaccelerator.check_mtime="1"
  eaccelerator.debug="0"
  eaccelerator.filter=""
  eaccelerator.shm_max="0"
  eaccelerator.shm_ttl="0"
  eaccelerator.shm_prune_period="0"
  eaccelerator.shm_only="0"
  eaccelerator.compress="1"
  eaccelerator.compress_level="9"
 
第四步:建立缓存目录

  mkdir /tmp/eaccelerator
  chmod 0777 /tmp/eaccelerator

 

配置选项
--------

eaccelerator.shm_size
 eAccelerator使用共享内存的总数。单位是MB.
 设置为“0”,则为操作系统默认值。默认为“0”

eaccelerator.cache_dir
 硬盘缓存的目录。eAccelerator存储预编译代码,session数据,内容数据(content)和使用的入口(entres??)。这些数据也能够存储在共享内存里(为了得到更快的通道)。默认为“/tmp/eaccelerator”。

 yayu:建立这个空目录后,在有人访问php页面后会自动建立二级二维目录(0,1.....e,f)(16进制,晕)

eaccelerator.enable
 决定eAccelerator是否有效。“1”为有效,“0”为无效。默认为“1”

eaccelerator.optimizer
 是否使用内置的优化工具加速代码的执行。“1”为是,“0”为否。默认为“1”

eaccelerator.debug
 是否记录eAccelerator debug log。“1”为是,“0”为否。默认为“0”

eaccelerator.check_mtime
 是否检查php程序更新时间。“1”为是,“0”为否。如果你想改变php程序后重编译程序到共享内存,那就应该设置为“1”。默认为“1”

eaccelerator.filter
 决定哪些php文件被缓存。你可能需要指定哪些文件(如:"*.php *.phtml")需要被缓存。如果在文件前加上“!”,那么符合条件的文件将被忽略。默认为"",这以为着所有php文件都会被缓存。

eaccelerator.shm_max
 设置诸如“eaccelerator_put()”之类的函数能往共享内存里面加载数据的大小。单位为MB。“0”为不限制,默认为“0”。

eaccelerator.shm_ttl
 当共享内存空间已满,将删除在“shm_ttl”秒前没有使用的程序。默认为0,为不删除任何文件。

eaccelerator.shm_prune_period
 共享内存已满。前一次操作是在shm_prune_period秒之前,那么这一次将删除所有的旧程序。默认为“0”,意为不删除任何程序。

eaccelerator.shm_only
 是否把编译后程序缓存到硬盘上。这个选项对session数据和内容(content)缓存无效。默认为“0”,意为同时使用共享内存和硬盘做缓存。

eaccelerator.compress
 是否对缓存内容做压缩。默认为“1”,为不压缩。

eaccelerator.compress_level
 压缩的级别,默认和最高都为“9”。

eaccelerator.name_space
 一个对所有键值假拟的字符串。通过在.htaccess文件中设置的这个值,允许两个应用使用相同的键值运行在同一个主机上。

 yayu:命名空间?

eaccelerator.keys
eaccelerator.sessions
eaccelerator.content
 决定那些键,session数据和内容将被缓存,这些可能的值是:

 "shm_and_disk" - 缓存数据在共享内存和硬盘上(默认值)
 "shm"          - 缓存数据在共享内存,如果共享内存已满或者提交的数据大小超过eaccelerator.shm_max,则存储在硬盘上。
 "shm_only"     - 只缓存数据在共享内存
 "disk_only"    - 只缓存数据在硬盘
 "none"         - 不缓存数据

eaccelerator.allowed_admin_path
 允许得到管理信息和管理操作的脚本路径。


控制面板及反汇编
---------------

如果你要使用控制面板,你需要在编译eAccelerator时加上 --with-eaccelerator-info选项,这个是默认值。

复制control.php文件到你的跟目录下,并且设置这个路径到php.ini 或者 eaccelerator.ini的eaccelerator.allowed_admin_path选项。如果你不这样做,你将看不到更多的相关信息,也不能控制eAccelerator。

你可以在control.php文件中设置用户名和密码以进入控制面板

当你使用--with-eaccelerator-disassembler编译时,你需要把dasm.php和PHP_Highlight.php也放到control.php目录里面。反汇编需要在编译PHP时加上对tokenizer的支持(--enable-tokenizer)
你可以在dasm.php文件的上面得到执行反汇编时的用户名和密码


eAccelerator 函数
-----------------

函数文档请看: http://bart.eaccelerator.net/doc/phpdoc/

联系我们
--------
当你有疑问、需要补丁,提交bug,请发email给 Bart Vanbrabant

--------------------------

以下是英文全文:

eAccelerator for PHP
=====================

What is eAccelerator?
----------------------
eAccelerator is a free open source PHP  accelerator,  optimizer,  encoder  and
dynamic content cache for PHP. It increases  performance  of  PHP  scripts  by
caching them in compiled state, so that the overhead of  compiling  is  almost
completely eliminated. It also optimises  the  script  to speed  up  execution
of PHP scripts. eAccelerator typically reduces server load and  increases  the
speed of your PHP code by 1-10 times.

eAccelerator is a fork of TurckMMCache
( http://sourceforge.net/project/turckmm-cache/  by Dmitry Stogov )

eAccelerator stores compiled PHP scripts in shared memory  and  executes  code
directly from it. It creates  locks  only  for  short  time,  while  searching
compiled PHP script in the cache, so one script can be executed simultaneously
by several engines. Files that can't fit in shared memory are cached  on  disk
only.

eAccelerator contains a PHP encoder and loader. You  can  encode  PHP  scripts
using encoder.php in order to distribute them without sources.  Encoded  files
can be run on any site which  runs  PHP  with  eAccelerator.  The  sources  of
encoded scripts can't be restored because they are stored in a  compiled  form
and the encoded version doesn't contain the source. Of course, some  internals
of the scripts can  be  restored  with  different  reverse  engineering  tools
(disassemblers, debuggers, etc), but it is not trivial.

eAccelerator is compatible with Zend Optimizer's loader. Zend  Optimizer  must
be installed after eAccelerator in php.ini. If you don't use  scripts  encoded
with  Zend  Encoder  we  do  not  recommend  to  install  Zend  Optimizer with
eAccelerator.

eAccelerator does not work in CGI mode but it does work in Fast-CGI mode with
webservers like lighttpd.


Here are some other products that provide the same functionality:

 - Zend Performance Suite (http://www.zend.com/)
 - Alternative PHP Cache (http://pecl.php.net/package/APC)

Download
--------
Latest eAccelerator versions can be downloaded at the sourceforge page:
http://sourceforge.net/projects/eaccelerator/
Development snapshots from cvs can be downloaded at
http://snapshots.eaccelerator.net


Requirements
------------
apache >= 1.3, mod_php >= 4.1, autoconf, automake, libtool, m4


Compatibility
-------------
eAccelerator has been reported working with php4 and php5, php 5.1 support
hasn't been integrated yet. It is being used on Linux, FreeBSD, MacOS X,
Solaris and Windows with apache 1.3 and 2, lighttpd and IIS.


Quick install
-------------

You can find more information about installation on eAccelerator website.
http://eaccelerator.sourceforge.net/

Note(1): for Microsoft Windows installation, please refer to README.win32 file.

Step 1. Compiling eAccelerator

  export PHP_PREFIX="/usr"
 
  $PHP_PREFIX/bin/phpize
 
  ./configure \
  --enable-eaccelerator=shared \
  --with-php-config=$PHP_PREFIX/bin/php-config
 
  make

  You must specify the real prefix where PHP is installed in the "export"
  command. It may be "/usr" "/usr/local", or something else.

Step 2. Installing eAccelerator

  make install

Step 3. Configuring eAccelerator

eAccelerator can be installed both as Zend or PHP extension.

For eaccelerator > 0.9.1, if you have /etc/php.d directory, you should copy eaccelerator.ini inside and modify default value if you need.

If not, you need to edit your php.ini file (usually /etc/php.ini).

To install as Zend extension:

  zend_extension="/usr/lib/php4/eaccelerator.so"
  eaccelerator.shm_size="16"
  eaccelerator.cache_dir="/tmp/eaccelerator"
  eaccelerator.enable="1"
  eaccelerator.optimizer="1"
  eaccelerator.check_mtime="1"
  eaccelerator.debug="0"
  eaccelerator.filter=""
  eaccelerator.shm_max="0"
  eaccelerator.shm_ttl="0"
  eaccelerator.shm_prune_period="0"
  eaccelerator.shm_only="0"
  eaccelerator.compress="1"
  eaccelerator.compress_level="9"

  If you use thread safe build of PHP you must use "zend_extension_ts" instead
  of "zend_extension".

To install as PHP extension:

  extension="eaccelerator.so"
  eaccelerator.shm_size="16"
  eaccelerator.cache_dir="/tmp/eaccelerator"
  eaccelerator.enable="1"
  eaccelerator.optimizer="1"
  eaccelerator.check_mtime="1"
  eaccelerator.debug="0"
  eaccelerator.filter=""
  eaccelerator.shm_max="0"
  eaccelerator.shm_ttl="0"
  eaccelerator.shm_prune_period="0"
  eaccelerator.shm_only="0"
  eaccelerator.compress="1"
  eaccelerator.compress_level="9" 

Step 4. Creating cache directory

  mkdir /tmp/eaccelerator
  chmod 0777 /tmp/eaccelerator


Configuration Options
---------------------

eaccelerator.shm_size
    The amount of shared memory (in megabytes) that eAccelerator will use.
    "0" means OS default. Default value is "0".

eaccelerator.cache_dir
    The directory that is used for disk cache. eAccelerator stores precompiled
    code, session data, content and user entries  here. The same data  can  be
    stored in shared memory also (for more quick access). Default value is
    "/tmp/eaccelerator".

eaccelerator.enable
    Enables or disables eAccelerator. Should be "1" for enabling  or  "0"  for
    disabling. Default value is "1".

eaccelerator.optimizer
    Enables or disables internal peephole optimizer which may  speed  up  code
    execution. Should be "1" for enabling or "0" for disabling. Default  value
    is "1".

eaccelerator.debug
    Enables or disables debug logging. Should be "1" for enabling or  "0"  for
    disabling. Default value is "0".

eaccelerator.check_mtime
    Enables or disables PHP file modification checking .  Should  be  "1"  for
    enabling or "0" for disabling. You should set it to "1"  if  you  want  to
    recompile PHP files after modification. Default value is "1".

eaccelerator.filter
    Determine which PHP files must be cached. You may specify  the  number  of
    patterns (for example "*.php *.phtml") which specifies to cache or not  to
    cache. If pattern starts with the character "!", it means to ignore  files
    which are matched by the following pattern. Default value is "" that means
    all PHP scripts will be cached.

eaccelerator.shm_max
    Disables putting large values into shared memory by " eaccelerator_put() "
    function. It indicates the largest allowed size in bytes (10240, 10K, 1M).
    The "0" disables the limit. Default value is "0".

eaccelerator.shm_ttl
    When eaccelerator fails to get shared memory for new script it removes all
    scripts which were not accessed  at  last "shm_ttl"  seconds  from  shared
    memory. Default value is "0" that means -  don't  remove  any  files  from
    shared memory.

eaccelerator.shm_prune_period
    When eaccelerator fails to get shared memory for new script  it  tryes  to
    remove  old  script   if   the   previous   try   was   made   more   then
    "shm_prune_period" seconds ago. Default value is "0" that  means  -  don't
    try to remove any files from shared memory.

eaccelerator.shm_only
    Enables or disables caching of compiled scripts on disk. It has  no  effect
    on session data and content caching. Default value is "0" that means -  use
    disk and shared memory for caching.

eaccelerator.compress
    Enables or disables cached content compression. Default value is  "1"  that
    means enable compression.

eaccelerator.compress_level
    Compression level used for content caching.  Default value is "9" which  is
    the maximum value

eaccelerator.name_space
    A string that's prepended to all keys. This allows two applications that
    use the same key names to run on the same host by setting this in .htaccess
    or in the main configuration file for the whole webserver.
   
eaccelerator.keys
eaccelerator.sessions
eaccelerator.content
    Determine where keys, session data and content will be cached. The possible
    values are:
    "shm_and_disk" - cache data in shared memory and on disk (default value)
    "shm"          - cache data in shared memory or on disk if shared memory
                     is full or data size greater then "eaccelerator.shm_max"
    "shm_only"     - cache data in shared memory
    "disk_only"    - cache data on disk
    "none"         - don't cache data

eaccelerator.allowed_admin_path
    The script paths that are allowed to get admin information and do admin
    controls

Control panel and disassembler
------------------------------

If you want to use the control-panel you need to compile eAccelerator with
    --with-eaccelerator-info which is the default value.
You need to copy the control.php file to your webroot and set the path to it
in the php.ini or eaccelerator.ini in the eaccelerator.allowed_admin_path
directive. If you don't do this you wont be able to see much information and
can't control eAccelerator.
You can set the username and password needed to access the control-panel in
the control.php file.

When you compile eAccelerator with --with-eaccelerator-disassembler you need
to place the dasm.php and PHP_Highlight.php file also in the same directory
as the control.php file. The disassembler requires PHP to be compiled with
tokenizer support (--enable-tokenizer).
You can set the username and password needed to access the disassembler at the
top of dasm.php.

eAccelerator API
----------------

API documentation can be found on this website: http://bart.eaccelerator.net/doc/phpdoc/

Contact us
----------
You can contact us with questions, patches or bugs, by sending an email to
Bart Vanbrabant

展开全文  
收起全文  
php的异常处理基础学习 (PHP心得)
发布于 2007-09-09 18:33 2 阅读:10,024 评论:2 标签: php 学习 异常

  一直以来对PHP5没怎么好好学习,属于那种用着的时候,查手册Google资料的那种人。前天看见同事的程序满眼的try,throw,catch,立马汗颜。不禁为自己在这方面的知识深深自责一下

  今天挤出点时间学习了这个东西。参考了一下文档:
  1:PHP 15:异常
  2:用实例分析PHP5异常处理,一看就懂
  3:PHP 5.0异常处理机制深度探索
  4:PHP5的异常处理机制

  PHP5内建的异常类需要有以下成员方法:

__construct() 构造函数,需要一个出错信息和一个可选的整型错误标记作参数
getMessage() 取得出错信息
getCode()
出错的代码
getFile() 异常发生的文件
getLine() 异常发生的行数
getTrace() 跟踪异常每一步传递的路线,存入数组,返回该数组
getTraceAsString()

和getTrace()功能一样,但可以将数组中的元素转成字符串并按一定格式输出

 __toString()  允许简单的显示Exception对象,并且给出所有以上方法给出的信息。

  Haohappy有几句话说的很好,PHP的异常机制可以满足我们如下的4点需求:

以下是引用片段:

1.允许一个方法给出一个出错标记给客户代码
2.提供程序错误的详细信息
3.让你同时判断多个出错条件,将你的错误报告和程序处理流程分开。
4.返回值必须是独立的类型,不会与正常返回的类型相混淆

  写了点程序练习一下:

以下是代码片段:

$a = 20;

// 使用常规catch的例子
try
{
  if($a == 1)
  {
    throw new Exception("I am 1", 1);
  }
  elseif ($a == 2)
  {
    throw new Exception("I am 2", 3);
  }
  elseif ($a == 3)
  {
    throw new Exception("I am 3", 3);
  }
  elseif ($a == 4)
  {
    throw new Exception("I am 4", 4);
  }
  else
  {
    throw new Exception("Who am I ?", 0);
  }
}
catch (Exception $e)
{
  if($e->getCode() == 1)
  {
    echo "1 : ".$e;
  }
  elseif ($e->getCode() == 2)
  {
    echo "2 : ".$e;
  }
  elseif ($e->getCode() == 3)
  {
    echo "3 : ".$e;
  }
  elseif ($e->getCode() == 4)
  {
    echo "4 : ".$e;
  }
  else
  {
    echo "0 : ".$e;
  }
}

echo "


";

// 使用不同的扩展异常类的例子
class A extends Exception{};
class B extends Exception{};

try
{
 if($a == 1)
  {
    throw new A("I am A", 1);
  }
  elseif ($a == 2)
  {
    throw new B("I am B", 2);
  }
  else
  {
    throw new Exception("Who am I ?", 0);
  }
}
catch (A $e)
{
  echo "1 : ".$e;
}
catch (B $e)
{
  echo "2 : ".$e;
}
catch (Exception $e)
{
  echo "0 : ".$e;
}

?>

  输出如下:

以下是引用片段:

0 : exception 'Exception' with message 'Who am I ?' in D:\WebPHP\WWW\new\test.php:24 Stack trace: #0 {main}


0 : exception 'Exception' with message 'Who am I ?' in D:\WebPHP\WWW\new\test.php:69 Stack trace: #0 {main}

展开全文  
收起全文