海运的博客

Nginx/PHP-FPM启用Keepalive

发布时间:June 11, 2014 // 分类:Nginx,PHP // No Comments

Nginx与PHP通信使用TCP连接会造成太多TIME_WAIT及浪费新建连接开销,Nginx新版本增加了对后端启用keepalive功能,有效提高服务器性能。
配置:

http
{
    upstream fastcgi_backend {
    server 127.0.0.1:9000;
    keepalive 60;
    }
}
server
{
  location ~ .*\.php$
  {
    fastcgi_pass fastcgi_backend;
    fastcgi_keep_conn on;
  }
}

PHP取URL根域名

发布时间:June 8, 2014 // 分类:PHP // No Comments

不能提取com.cn类似的域名:

<?php
   $url = 'https://www.haiyun.me';
   $host = parse_url($url, PHP_URL_HOST);
   $host_parts = explode('.', $host);
   $domain = implode('.', array_slice($host_parts, count($host_parts)-2));
   echo $domain;
?>
<?php
   $url = 'https://www.haiyun.me';
   $host = parse_url($url, PHP_URL_HOST);
   $host_parts = explode('.', $host);
   if(count($host_parts) > 2) {
      $host_parts = array_reverse($host_parts);
      $domain = $host_parts[1].'.'.$host_parts[0];
   } else {
      $domain = $host;
   }
   echo $domain;
?>

正则,www.hai.me被认为是根域名:

<?php
   function get_domain($url)
   {
      $domain = parse_url($url, PHP_URL_HOST);
      if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
         var_dump($regs);
         return $regs['domain'];
      }
      return false;
   }

   echo get_domain("https://www.haiyun.me");
?>

基于tld列表,精确:
https://publicsuffix.org/learn/

PHP imap访问多收件箱

发布时间:May 20, 2014 // 分类:PHP // No Comments

列出所有目录:

$host = '{imap.mail.yahoo.com:993/ssl}';
$user = 'user@yahoo.com';
$pass = 'password';
$inbox = imap_open($host, $user, $pass);
$mailboxes = imap_list($inbox, $host, '*');
$mailboxes = str_replace($host, '', $mailboxes);
print_r($mailboxes);

结果:

Array
(
    [0] => Bulk Mail
    [1] => Draft
    [2] => Inbox
    [3] => Sent
    [4] => Trash
)

重新打开指定的目录:

imap_reopen($inbox, $host.'Bulk Mail');
$emails = imap_search($inbox,'ALL');
print_r($emails);

Nginx/PHP的SERVER_NAME和HTTP_HOST

发布时间:May 1, 2014 // 分类:Nginx,PHP // No Comments

SERVER_NAME对应Nginx配置文件中的server_name,通过fastcgi_param设置,如域名指向到IP而不在nginx中设置对应的server_name,PHP取SERVER_NAME为空,如果有多个server_name,取第一个。

server_name   www.haiyun.me;
fastcgi_param  SERVER_NAME        $server_name;

HTTP_HOST包含在HTTP请求信息中,即请求的域名或IP,Nginx内为host。

PHP多进程Class类

发布时间:April 26, 2014 // 分类:PHP // No Comments

<?php
   declare(ticks=1);
   //A very basic job daemon that you can extend to your needs.
   class JobDaemon{

      public $maxProcesses = 8;
      protected $jobsStarted = 0;
      protected $currentJobs = array();
      protected $signalQueue=array();  
      protected $parentPID;

      public function __construct(){
         echo "constructed \n";
         $this->parentPID = getmypid();
         pcntl_signal(SIGCHLD, array($this, "childSignalHandler"));
      }

      /**
      * Run the Daemon
      */
      public function run(){
         echo "Running \n";
         for($i=0; $i<500; $i++){
            $jobID = rand(0,10000000000000);

            while(count($this->currentJobs) >= $this->maxProcesses){
               echo "Maximum children allowed, waiting...\n";
               sleep(1);
            }

            $launched = $this->launchJob($jobID);
         }

         //Wait for child processes to finish before exiting here
         while(count($this->currentJobs)){
            echo "Waiting for current jobs to finish... \n";
            sleep(1);
         }
      }

      /**
      * Launch a job from the job queue
      */
      protected function launchJob($jobID){
         $pid = pcntl_fork();
         if($pid == -1){
            //Problem launching the job
            error_log('Could not launch new job, exiting');
            return false;
         }
         else if ($pid){
            // Parent process
            // Sometimes you can receive a signal to the childSignalHandler function before this code executes if
            // the child script executes quickly enough!
            //
            $this->currentJobs[$pid] = $jobID;

            // In the event that a signal for this pid was caught before we get here, it will be in our signalQueue array
            // So let's go ahead and process it now as if we'd just received the signal
            if(isset($this->signalQueue[$pid])){
               echo "found $pid in the signal queue, processing it now \n";
               $this->childSignalHandler(SIGCHLD, $pid, $this->signalQueue[$pid]);
               unset($this->signalQueue[$pid]);
            }
         }
         else{
            //Forked child, do your deeds....
            $exitStatus = 0; //Error code if you need to or whatever
            echo "Doing something fun in pid ".getmypid()."\n";
            exit($exitStatus);
         }
         return true;
      }

      public function childSignalHandler($signo, $pid=null, $status=null){

         //If no pid is provided, that means we're getting the signal from the system.  Let's figure out
         //which child process ended
         if(!$pid){
            $pid = pcntl_waitpid(-1, $status, WNOHANG);
         }

         //Make sure we get all of the exited children
         while($pid > 0){
            if($pid && isset($this->currentJobs[$pid])){
               $exitCode = pcntl_wexitstatus($status);
               if($exitCode != 0){
                  echo "$pid exited with status ".$exitCode."\n";
               }
               unset($this->currentJobs[$pid]);
            }
            else if($pid){
               //Oh no, our job has finished before this parent process could even note that it had been launched!
               //Let's make note of it and handle it when the parent process is ready for it
               echo "..... Adding $pid to the signal queue ..... \n";
               $this->signalQueue[$pid] = $status;
            }
            $pid = pcntl_waitpid(-1, $status, WNOHANG);
         }
         return true;
      }
   }
   $job = new JobDaemon();
   $job->run();

更多:http://blog.csdn.net/lvsmaster/article/details/6863951

分类
最新文章
最近回复
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...