Tag Archive for 'PHP'

十大流行PHP开发框架介绍

第10位 Speedphp

Speedphp是国产框架,特别推荐初学者使用和学习,中文手册让初学者阅读起来也很容易。当然可以用来做各种复杂度的项目。同时他也支持新浪应用引擎(SAE)。

第9位 DooPHP

DooPHP自称为最快的PHP框架,对于高在线需求的网站不妨考虑一下DooPHP,不过目前面临文档过少,社区支持困难的问题。如果你是一位追求速度的高级的PHP开发者,也可以试试究竟有没有作者自称的那么快。

第8位 Drupal

Drupal,你可以把他当作一个CMS来使用,功能强大到让你难以想象,可是你又可以当作框架。做二次开发的好东西,如果你想做欧美外包,不了解Drupal是绝对不行的。值得一提的是,Drupal的社区氛围非常好,你几乎可以在官方论坛上解决任何问题。 Continue reading ‘十大流行PHP开发框架介绍’

Yii CJSON

1
2
3
4
5
      $this->layout=false;
      header('Content-type: application/json');
      echo CJSON::encode(array('id'=>$project->id, 'content'=>$project->content, 'pass_way'=>$project->pass_way, 'total'=>$count, 'bs'=>$project->multiple
         ));
      Yii::app()->end();

PHP pcntl_fork

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    $pid = pcntl_fork();
    if ($pid == -1) {
       die('could not fork');
    } else if ($pid) {
       // we are the parent
      pcntl_wait($status); //Protect against Zombie children
    } else {
      // we are the child

      echo "download files ...\n";   
      $cmd = "/usr/bin/wget --user-agent=\"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;)\" \\
        --header=\"Accept:text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\" \\
        --header=\"Accept-Language: en-us,en;q=0.5\" \\
        --header=\"Accept-Encoding: gzip,deflate\" \\
        --header=\"Accept-Charset: GBK,utf-8;q=0.7,*;q=0.7\" \\
        --header=\"Keep-Alive: 300\" -O /dev/null  -i list.txt -o wget.log"
;
      exec($cmd);
      echo "done\n";
      declare(ticks = 1);

      pcntl_signal(SIGUSR1, function ($signal) {
            echo 'HANDLE SIGNAL ' . $signal . PHP_EOL;
      });

      posix_kill(posix_getpid(), SIGUSR1);
      die();
    }

Caching WordPress with Varnish

WordPress is a fantastic application that lets you get a website up and running in a matter of minutes. Not only that, but you require absolutely no knowledge of how to code to do so. As with many web applications though, it needs a little help in getting it to perform better. Lots of these dynamic apps breaks caching by using lots of cookies and setting headers like Cache-Control and Pragma. For a better in-depth tutorial on caching, read this article.

Requirements

  • Web server running on a different port than the standard one (TCP/80)
  • Varnish installed

Configuration

The standard location for Varnish’s configuration file is located at/etc/varnish/default.vcl. You will find below the configuration I use on this blog. See the comments to get a better understanding of what’s what. Also, make sure varnish will listen on port TCP/80 (use the -a flag when you start the daemon).

# Send all requests to your webserver
backend default {
 .host = "127.0.0.1";
 .port = "8080";
}

sub vcl_recv {
 # If we don't set this, in our webserver's logs, we'll get varnish's IP instead
 # of the real client's IP
 if (req.http.x-forwarded-for) {
 set req.http.X-Forwarded-For =
 req.http.X-Forwarded-For ", " client.ip;
 } else {
 set req.http.X-Forwarded-For = client.ip;
 }
 if (req.request != "GET" && req.request != "HEAD") {
 return (pass);
 }
 # Unless we're in the admin/login section, remove all cookies
 if (!(req.url ~ "wp-(login|admin)")) {
 unset req.http.cookie;
 }
 return (lookup);
}

sub vcl_pipe {
 return (pipe);
}

sub vcl_pass {
 return (pass);
}

sub vcl_hash {
 set req.hash += req.url;
 if (req.http.host) {
 set req.hash += req.http.host;
 } else {
 set req.hash += server.ip;
 }
 return (hash);
}

sub vcl_hit {
 if (!obj.cacheable) {
 return (pass);
 }
 return (deliver);
}

sub vcl_miss {
 return (fetch);
}

sub vcl_fetch {
 if (!beresp.cacheable) {
 return (pass);
 }
 if (beresp.http.Set-Cookie) {
 return (pass);
 }
 if (!(req.url ~ "wp-(login|admin)")) {
 unset req.http.cookie;
 }
 return (deliver);
}

sub vcl_deliver {
 # Remove bad headers
 remove resp.http.X-Varnish;
 remove resp.http.Via;
 remove resp.http.Age;
 remove resp.http.X-Powered-By;
 remove resp.http.Cache-Control;
 remove resp.http.Pragma;
 return (deliver);
}

Restart Varnish to load the new configuration.

Testing the recipe

Open up varnishstat in your terminal. You should see some stats. Ctrl-F5 a few times your site in your favorite browser. That way, you’re telling your browser to bypass its own cache and ask the site for fresh items. The first time you access your site, you will get lots of misses, but after that, you should get lots of cache hits.

Hitrate ratio:        9        9        9
Hitrate avg:     0.7302   0.7302   0.7302
22         0.00         0.16 Client connections accepted
132         0.00         0.95 Client requests received
100         0.00         0.72 Cache hits
5         0.00         0.04 Cache hits for pass
25         0.00         0.18 Cache misses

As you can see, I’m getting about 4 times more cache hits than misses. This way, Varnish will server all the static items and will let your webserver take care of all the dynamic content. Hope this helps a bit.

 

Kohana 3 – .htaccess that Works on DreamHost

I’m currently hosting my sites on DreamHost because … well let me just put it this way: we’re both cheap. It works out. But what doesn’t work out are the default .htaccess rules that come bundled with Kohana. So to fix this, swap the line RewriteRule .* index.php/$0 [PT] found in your .htaccess with one of the following options:

1
2
3
4
5
# Option 1
RewriteRule .* index.php?/$0 [PT,L,QSA]

# Option 2
RewriteRule .* index.php?kohana_uri=$0 [PT,L,QSA]

AMQP+PHP架构

高级消息传输在未来的WEB架构必将是必备组件,对于各个应用之间的集成非常合适。

http://www.rabbitmq.com/how.html

http://cn.php.net/manual/en/book.amqp.php

https://github.com/tnc/php-amqplib

PHP二维数组排序函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  // 二维数组排序函数
  function sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")
  {
    if(!is_array($ArrayData))
    {
      return $ArrayData;
    }    
    // Get args number.
    $ArgCount = func_num_args();
    // Get keys to sort by and put them to SortRule array.
    for($I = 1;$I < $ArgCount;$I ++)
    {
      $Arg = func_get_arg($I);
      if(!eregi("SORT",$Arg))
      {
        $KeyNameList[] = $Arg;
        $SortRule[]    = '$'.$Arg;
      }
      else
      {
        $SortRule[]    = $Arg;
      }
    }
    // Get the values according to the keys and put them to array.
    foreach($ArrayData AS $Key => $Info)
    {
      foreach($KeyNameList AS $KeyName)
      {
        ${$KeyName}[$Key] = $Info[$KeyName];
      }
    }
   
    // Create the eval string and eval it.
    $EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
    eval ($EvalString);
    return $ArrayData;
  }

PHP组合排列数组函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  private function combination($ar,   $num)   {
    $control   =   range(0,   $num-1);
    $k   =   false;
    $total   =   count($ar);
    while($control[0]   <   $total-($num-1))   {
      $t   =   array();
      for($i=0;   $i <$num;   $i++)   $t[]   =   $ar[$control[$i]];
      $r[]   =   $t;

      for($i=$num-1;   $i>=0;   $i--)   {
              $control[$i]++;
              for($j=$i;   $j <$num-1;   $j++)   $control[$j+1]   =   $control[$j]+1;
              if($control[$i]   <   $total-($num-$i-1))   break;
      }
    }
    return   $r;
  } private function combination($ar,   $num)   {
    $control   =   range(0,   $num-1);
    $k   =   false;
    $total   =   count($ar);
    while($control[0]   <   $total-($num-1))   {
      $t   =   array();
      for($i=0;   $i <$num;   $i++)   $t[]   =   $ar[$control[$i]];
      $r[]   =   $t;

      for($i=$num-1;   $i>=0;   $i--)   {
              $control[$i]++;
              for($j=$i;   $j <$num-1;   $j++)   $control[$j+1]   =   $control[$j]+1;
              if($control[$i]   <   $total-($num-$i-1))   break;
      }
    }
    return   $r;
  }

精通php的十大要点

1. 在合适的时候使用PHP – Rasmus Lerdorf

没有谁比PHP的创建者Rasmus Lerdorf明白PHP用在什么地方是更合理的, 他于1995年发布了PHP这门语言,从那时起,PHP就像燎原之火,烧遍了整个开发阵营,改变了互联网的世界。 可是, Rasmus并不是因此而创建PHP的 PHP是为了解决web开发者的实际问题而诞生的。

和许多开源项目一样,PHP变得流行,流行的动机并不能用正常的哲学来进行解释,甚至流行得有些孤芳自赏。它完全可以作为一个案例,一个解决各种web问题的工具需求所引起的案例,因此当PHP刚出现的时候,这种工具需求全部聚焦到PHP的身上。

但是,你不能奢望PHP可以解决所有问题。Lerdorf是第一个承认PHP只是一种工具的人,并且PHP也有很多力所不能及的情况。

根据工作的不同来选择合适的工具。我跑了很多家公司,为了说服他们部署和使用PHP,但是这并不意味着PHP对所有问题都适用。它只是可以一个解决大部分问题的front-end脚步语言。

作为一个web开发者,尝试用PHP解决所有问题是不科学的,同时也会浪费你的时间。当PHP玩不转的时候,不要犹豫,试用一下其他的语言吧。 Continue reading ‘精通php的十大要点’

服务器端PHP多进程编程实战

最近比较PHP跟python, Erlang 的特性,发现PHP有很多人们不常用到的特性。用PHP CLI可以实现很多不错的应用。比如做爬虫, 长期运行的计算脚本, 完全可以取代其他语言来做 服务器的运维。这对于熟悉PHP的人来说如虎添翼。

为什么PHP多进程很好? 网游服务器大部分都使用多线程而不是多进程的原因也在于进程比线程更加稳定。而且多线程适合现在多核服务器的应用场景,更能发挥多核运算的能力。进程的维护可以用很多操作系统级别的工具。Message Queue解决了多大部分线程通信问题。所以PHP多进程很适合做服务器端的计算密集型的应用。

据一家越南IT公司介绍,他们成功的把PHP后台多进程用在法律文件的分发、处理银行账户的金额这样的企业级的应用上。
Continue reading ‘服务器端PHP多进程编程实战’