设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

PHP中一些通用和易混淆技术点的最佳编程实践

2013-6-4 13:43| 发布者: 红黑魂| 查看: 3510| 评论: 0|来自: 开源中国编译

摘要: 最新改进维护本文档最后一次于2013年3月8日审核。最后一次修改是在2013年3月8日。这由我,Alex Cabal维护的。到现在我已经写了很长时间PHP代码了, 目前我运行Scribophile,为严肃作家提供的在线写作小组,Writerfolio, ...

缓存PHP操作码(字节码)

使用APC.

在PHP的标准安装环境里,每个PHP脚本在每次它被访问时都会被编译成操作码(字节码)文件并执行。花时间对完全相同的脚本一遍又一遍进行编译,对大型网站来说,势必导致性能问题。

解决方案是操作码缓存。操作码缓存是一种可以记忆每个脚本的编译结果的系统,这样服务器不必浪费时间一遍遍编译。通常它们也很聪明足以探测到一个脚本已经改变并对其重新编译所以当你更新你的PHP源文件时,不必非得手动清除缓存

有几种PHP操作码缓存系统可用,值得一提的是 eacceleratorxcache, 和 APC. APC 是由PHP项目组官方提供支持的,是最活跃也是最容易安装的。它还提供了一个可选的类似memcached的持久的键-值存储。基于这些理由,它是你应该使用的。


安装APC

通过在终端运行以下命令,可以在Ubuntu 12.04上安装APC:

sudo apt-get install php-apc

无需另外的配置。

作为一个永久的键值对存储来使用APC

APC也提供类似memcached的功能,对于你的脚本也是显而易见的。相对于使用memcached,最大的优点就是APC集成到了PHP内核中,因此你不需要再的你服务器中保存你的可动部分,PHP开发者主动地在它上面工作。另外一方面,APC不是一个分布式缓存;如果你需要这个特性,你必须使用memcached。

示例

01<?php
02// Store some values in the APC cache.  We can optionally pass a time-to-live, but in this
example the values will live forever until they're garbage-collected by APC.
03apc_store('username-1532''Frodo Baggins');
04apc_store('username-958''Aragorn');
05apc_store('username-6389''Gandalf');
06  
07// After storing these values, any PHP script can access them, no matter when it's run!
08$value = apc_fetch('username-958'$success);
09if($success === true)
10    print($value); // Aragorn
11  
12$value= apc_fetch('username-1'$success); // $success will be set to boolean false,
because this key doesn't exist.
13if($success!== true) // Note the !==, this checks for true boolean false, not "falsey"
values like 0 or empty string.
14    print('Key not found');
15  
16apc_delete('username-958'); // This key will no longer be available.
17?>

Gotchas=Got You

  • 如果你不使用PHP-FPM (例如你使用mod_php或者mod_fastcgi),每个PHP进程都将有它独一无二的APC实例,包括键值对存储。如果你不小心的话,在你的应用代码中可能导致同步问题。

进一步阅读


PHP和Memcached

如果你需要一个分布式缓存使用Memcached客户端库。或者使用APC

一个缓存系统通常能够改进你的app的性能。Memcached是一个主流的选择,并且它兼容许多语言,包括PHP。

然而当它从一个PHP脚本访问一个Memcached服务器的时候,你有两个不同的,命名愚蠢的客户端库选择:MemcacheMemcached。它们是不同的库,但是有着近乎相同的名字,并且都用于访问一个Memcached实例。

事实证明Memcached库,是实现Memcached协议最好的方法。它包括一些Memcache库所没有的,有用的特性,看起来是最被积极开发的一款。

然而如果你不需要从一系列分布式服务器中访问一个Memcached实例,则使用APC来代替。APC由PHP项目支持,有很多和Memcached相似的功能, 附加的惊喜就是,她说一个操作码缓存,这能够提升你的PHP脚本的性能。


安装Memached客户端库

在你安装Memcached服务端之后,你需要安装Memcached客户端库。没有这个库,你的PHP脚本将不能和Memcached服务端通信。

通过在终端运行如下命令,你能够安装Memcached客户端库:

sudo apt-get install php5-memcached

使用APC替代

查看the entry on opcode caches,了解更多关于使用APC作为一个Memcached替代选择。

进一步阅读


PHP和正则

使用PCRE (preg_*)族函数    

PHP有两种不同的方式使用正则表达式:PCRE (Perl-compatible, preg_*)函数和POSIX (POSIX extended, ereg_*)函数。

每一族函数使用轻微不同风格的正则表达式。幸运地,从PHP5.3.0开始,POSIX函数就被弃用了。因为这个,你不应当在新代码中使用POSIX函数。总是勇士PRCE函数,即是preg_*函数。

进一步阅读


配置Web服务器提供PHP服务

使用PHP-FPM

有多种方式来配置一个web服务器以提供PHP服务。传统(并且糟糕的)的方式是使用Apache的 mod_php。Mod_php将PHP 绑定到Apache自身,但是Apache对于该模块功能的管理工作非常糟糕。一旦遇到较大的流量, 就会遭受严重的内存问题。

后来两个新的可选项很快流行起来:mod_fastcgi 和mod_fcgid。两者均保持一定数量的PHP执行进程, Apache将请求发送到这些端口来处理PHP的执行。由于这些库限制了存活的PHP进程的数量, 从而大大减少了内存使用而没有影响性能。

一些聪明的人创建一个fastcgi的实现,专门为真正与PHP工作良好而设计,他们称之为 PHP-FPM。PHP 5.3.0之前,为安装它, 你得跨越许多障碍,但幸运的是,PHP 5.3.3的核心包含了PHP-FPM,因此在Ubuntu 12.04上安装它非常方便。

如下示例是针对Apache 2.2.22的,但PHP-FPM也能用于其他web服务器如Nginx


安装 PHP-FPM 和 Apache

通过在终端中运行命令,在Ubuntu 12.04安装PHP-FPM和Apache

sudo apt-get install apache2-mpm-worker libapache2-mod-fastcgi php5-fpm sudo a2enmod actions alias fastcgi

注意到我们必须使用apache2-mpm-worker,而不是apache2-mpm-prefork或者apache2-mpm-threaded。

下一步,我们将配置我们的Apache虚拟主机,以便路由PHP请求到PHP-FPM处理中。在你的Apache配置文件中放置如下(在Ubuntu 12.04中,默认的一个路径为/etc/apache2/sites-available/default)。

1<VirtualHost *:80>
2    AddHandler php5-fcgi .php
3    Action php5-fcgi /php5-fcgi
4    Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
5    FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host 127.0.0.1:9000 -idle-timeout
120 -pass-header Authorization
6</VirtualHost>

最后,重启Apache和FPM进程:

1sudo service apache2 restart
2sudo service php5-fpm restart

进一步阅读


发送email

使用PHPMailer.

使用PHPMailer 5.1测试。

PHP提供一个mail()函数,看起来极度简单和容易。不幸的是,就像PHP中的很多事情,它的简单是容易误解的,用表面的值来使用它,容易导致严重的安全问题。

Email是一个协议的集合,具有比PHP更曲折痛苦的历史。满足它也就是说,在发送email的时候有太多的疑惑,就像PHPmail()函数应当给你的感觉一样。

PHPMailer 是一个受欢迎的,完善的开源库,提供安全发送mailis的一个简单接口。它为你处理好疑惑,以便你能够关注更重要的事情。

示例

01<?php
02// Include the PHPMailer library
03require_once('phpmailer-5.1/class.phpmailer.php');
04  
05// Passing 'true' enables exceptions.  This is optional and defaults to false.
06$mailer new PHPMailer(true);
07  
08// Send a mail from Bilbo Baggins to Gandalf the Grey
09  
10// Set up to, from, and the message body.  The body doesn't have to be HTML; check the PHPMailer
 documentation for details.
11$mailer->Sender = 'bbaggins@example.com';
12$mailer->AddReplyTo('bbaggins@example.com''Bilbo Baggins');
13$mailer->SetFrom('bbaggins@example.com''Bilbo Baggins');
14$mailer->AddAddress('gandalf@example.com');
15$mailer->Subject = 'The finest weed in the South Farthing';
16$mailer->MsgHTML('<p>You really must try it, Gandalf!</p><p>-Bilbo</p>');
17  
18// Set up our connection information.
19$mailer->IsSMTP();
20$mailer->SMTPAuth = true;
21$mailer->SMTPSecure = 'ssl';
22$mailer->Port = 465;
23$mailer->Host = 'my smpt host';
24$mailer->Username = 'my smtp username';
25$mailer->Password = 'my smtp password';
26  
27// All done!
28$mailer->Send();
29?>



酷毙
1

雷人

鲜花

鸡蛋

漂亮

刚表态过的朋友 (1 人)

  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部