Joe Horn 的啟示錄
Joe Horn's Blog
  • About.me
  • Facebook
  • Flickr
  • GitHub
  • Instagram
  • LinkedIn
  • Pinterest
  • Slideshare
  • Twitter
  • YouTube
  • Tumblr
RSS
  • All Posts
  • VPS Referrals
  • My Plurk
  • My Plurk Bot

11 月 30 2017

PHPExcel 的活頁簿標題 ( SheetTitle ) 處理

之前曾經使用 PHPExcel 踩到地雷,處理完之後一直忘記載這邊留個紀錄(拖稿?)。

Excel 的活頁簿標題有字元 & 長度限制,但 library ( PHPExcel ) 沒有特別判斷 & 處理,所以…

    // Excel 的活頁簿 title 不允許 * : / \ ? [ ]
    $currTitle = str_replace(
        array('*', ':', '/', '\\', '?', '[', ']'),
        "",
        $currTitle
    );

    // 字數上限 31,保守抓 30
    $maxByte = 90;
    $chars = mb_strlen($currTitle, 'UTF-8');
    while ( $chars > 30 ) {
        $currTitle = mb_strcut($currTitle, 0, $maxByte, 'UTF-8');
        $chars = mb_strlen($currTitle, 'UTF-8');
        $maxByte--;
    }

分享此文:

  • Tweet

By Joe Horn • PHP 0 • Tags: PHP, PHPExcel

2 月 22 2016

改用 Let’s Encrypt 的 certificates

之前都在用 StartSSL 的免費 certificate,雖知道有 Let’s Encrypt,但因手邊幾乎都轉用 nginx,遲遲沒下手。

約莫一週前 zeroplex 丟了這個網頁:「Why I stopped using StartSSL (Hint: it involves a Chinese company)」,三、四天前 DK 大神也撰文提及,就決定趁週末沒什麼事來動工…

用 DK 大神在一個月前撰文提過的 dehydrated 這個 GitHub 專案可以輕鬆搞定,步驟大致如下…

  1. 找個自己喜歡的目錄(不是 /tmp …),把 dehydrated clone 出來
  2. 在 clone 出來的目錄之下建立 .acme-challenges 目錄
    mkdir dehydrated/.acme-challenges
  3. 在原本的 HTTP (80 port) 設定下建立 alias,把網站的 /.well-known/acme-challenge 指向剛建立好的目錄
  4. 讓 HTTP daemon 重讀設定
  5. 執行這個指令
    dehydrated/dehydrated -c -d {網站hostname}
  6. 完成 & 成功後就會在 dehydrated/certs 發現目錄,目錄下有數個檔案
  7. 我自己讓 nginx 使用 fullchain.pem 跟 privkey.pem,設定大致如下:
    server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
    
        ssl on;
        ssl_certificate /SOMEWHERE/dehydrated/certs/{網站hostname}/fullchain.pem;
        ssl_certificate_key /SOMEWHERE/dehydrated/certs/{網站hostname}/privkey.pem;
        ssl_trusted_certificate /SOMEWHERE/dehydrated/certs/{網站hostname}/fullchain.pem;
        ....
    }
    

我記得 Let’s Encrypt 的 certificate 要在 90 天內 renew,所以 cron job 這樣放:

0 0 1 */2 * /SOMEWHERE/dehydrated/letsencrypt.sh -c -d {網站hostname} > /dev/null 2>&1

Updated : letsencrypt.sh 改為 dehydrated 。

分享此文:

  • Tweet

By Joe Horn • WWW 0 • Tags: certificate, dehydrated, HTTPS, Let's Encrypt, letsencrypt.sh, nginx, SSL, StartSSL

12 月 10 2015

網頁相關技術的抉擇

以往的網頁純粹就是 HTML,之後的動態網頁技術 ( CGI、PHP、ASP、JSP、… )、CSS、VBScript、Javascript 雖讓網頁內容/效果愈來愈多元,網頁開發/維護難度的關鍵亦僅取決於語言的熟悉度。

近幾年則是冒出了不少網頁前端 frameworks。 node.js、io.js 與 React、Angular、Backbone、Ember、…,雖說是使用相同的程式語言,但各種 frameworks 的選用與導入著實讓不少網頁前端開發人員頭疼。

考量多螢、多裝置的兼容性, COSCOP 2015 就有人提出 “每18至24個月,前端技術的難度會增加一倍以上”。
但… 真只有網頁前端開發人員的日子愈來愈難熬嗎?
SQL Injection、XSS、CORS 等議題與考量呢?

我個人一直認為程式語言只是工具,沒特別去吹捧某特定程式語言;同理,我也把 frameworks 當作是工具。
我們看到的一些新技術/framework 固然很好,真有立即導入的必要性嗎?
若看到新的技術屏除跟風心態,先稍作觀望,讓環境的變化幫我們除強汰弱,另一方面加強人員的教育訓練,新技術導入前仔細評估,審慎抉擇,是否網頁開發人員就可以不必過得如此辛苦?

分享此文:

  • Tweet

By Joe Horn • Thoughts, WWW 0 • Tags: framework, front-end developer, web, web developer

8 月 25 2015

調整 Apache HTTPD & Apache Tomcat & nginx 的 content cache & compression

前陣子調整了一些 Apache HTTPD & Tomcat & nginx 的 cache 機制與內容壓縮機制,稍微紀錄一下。

Apache HTTPD 的 cache:

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType text/css "access plus 1 month"
</IfModule>

Apache HTTPD 的壓縮:

<IfModule mod_deflate.c>
  DeflateCompressionLevel 9
  AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
  AddOutputFilter DEFLATE js css
</IfModule>

Apache Tomcat 的 cache(在 application 的 web.xml 做調整):

  <filter>
    <filter-name>ExpiresFilter</filter-name>
    <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
    <init-param>
      <param-name>ExpiresByType image</param-name>
      <param-value>access plus 1 month</param-value>
    </init-param>
    <init-param>
      <param-name>ExpiresByType text/css</param-name>
      <param-value>access plus 1 month</param-value>
    </init-param>
    <init-param>
      <param-name>ExpiresByType application/javascript</param-name>
      <param-value>access plus 1 month</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>ExpiresFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
  </filter-mapping>

Apache Tomcat 的壓縮(在 conf/server.xml 做調整):

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               compression="on" compressableMimeType="text/html,text/xml,text/plain,text/css,application/javascript"
               redirectPort="8443" />

nginx 的 cache(在 server tag 內做調整):

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires 1M;
}

nginx 的壓縮(在 http tag 內做調整):

gzip_vary on;
gzip_proxied any;
gzip_comp_level 9;
gzip_buffers 8 32k;
gzip_http_version 1.0;
gzip_types text/plain text/css application/json
           application/javascript application/x-javascript text/javascript
           text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;

大概就調這些,其他多媒體檔案就看需求了…

分享此文:

  • Tweet

By Joe Horn • WWW 0 • Tags: Apache HTTPD, Apache Tomcat, cache, compression, deflate, gzip, nginx

3 月 4 2015

勞保局的自然人憑證元件…

現在都什麼時候了… 是沒有人在用嗎? O_o

分享此文:

  • Tweet

By Joe Horn • Life, WWW 0

12 月 30 2014

Crucial M550 on Lenovo L412

從數據看來,也是受限於界面速度,但 4k 對齊與 AHCI 設定等等的 tuning 影響頗大。

調整前:

調整後:

4k-64Thrd 的寫入差距頗大…

分享此文:

  • Tweet

By Joe Horn • Computer Hardware 0 • Tags: Crucial, L412, Lenovo, M550, SSD

12 月 14 2014

rsyslog daemon 在 OpenVZ guest 瘋狂佔用 CPU resource

剛幫忙解掉的問題。
某台建立在 OpenVZ 的 Ubuntu 的 rsyslog daemon process 瘋狂佔用 CPU resource。
log 看到的狀況是這樣:

...
Dec 14 06:25:13 Linux rsyslogd-2177: rsyslogd[internal_messages]: 1134916 messages lost due to rate-limiting
Dec 14 06:25:13 Linux rsyslogd: imklog: error reading kernel log - shutting down: Bad file descriptor
Dec 14 06:25:13 Linux rsyslogd: message repeated 498 times: [imklog: error reading kernel log - shutting down: Bad file descriptor]
Dec 14 06:25:19 Linux rsyslogd-2177: rsyslogd[internal_messages]: 1114295 messages lost due to rate-limiting
Dec 14 06:25:19 Linux rsyslogd: imklog: error reading kernel log - shutting down: Bad file descriptor
Dec 14 06:25:19 Linux rsyslogd: message repeated 498 times: [imklog: error reading kernel log - shutting down: Bad file descriptor]
Dec 14 06:25:25 Linux rsyslogd-2177: rsyslogd[internal_messages]: 1805432 messages lost due to rate-limiting
Dec 14 06:25:25 Linux rsyslogd: imklog: error reading kernel log - shutting down: Bad file descriptor
Dec 14 06:25:25 Linux rsyslogd: message repeated 498 times: [imklog: error reading kernel log - shutting down: Bad file descriptor]
...

把 /etc/rsyslog.conf 下列這行註解掉,重新啟動 rsyslog 就搞定了… :-)

$ModLoad imklog   # provides kernel logging support

分享此文:

  • Tweet

By Joe Horn • Linux 0 • Tags: Linux, OpenVZ, rsyslog, Ubuntu

11 月 30 2014

[MySQL] shrink ibdata file

最近幫忙處理某台 MySQL 5.0 server,做了個小實驗,寫個文章把處理過程紀錄下來。

因為該 server 沒有 innodb_file_per_table 這個設定,所有的 InnoDB 資料都放在 ibdata 這個檔案裡,而我們希望能讓該 server 持續運作,盡可能在不停機的狀況下,把 InnoDB 的資料切開。

實驗後,我們透過這些步驟完成切割/轉移:

  1. 確認舊的 server 有開啟 binlog,準備跑 replication。
  2. 用 Percona XtraBackup 備份舊的 MySQL 資料庫。
  3. 準備新機器,把備份檔丟到新的 server。
  4. 在新機器的 my.cnf 加入 innodb_file_per_table 設定,把 MySQL daemon 跑起來。
  5. 在新機器用以下指令把 InnoDB table 挑出來:
    SELECT `TABLE_SCHEMA`, `TABLE_NAME` FROM `TABLES` WHERE `ENGINE`='InnoDB' ORDER BY `TABLE_SCHEMA`,`TABLE_NAME`;
  6. 在新機器用 mysqldump 匯出 table schema & data :
    mysqldump -u root -pMY_PASSWORD --max_allowed_packet=512M -Cx --opt TABLE_SCHEMA TABLE_NAME > TMP_DIR/TABLE_SCHEMA.TABLE_NAME.sql
  7. 在新機器 drop 掉已匯出的 table:
    DROP TABLE `TABLE_SCHEMA`.`TABLE_NAME`;
  8. 停掉新機器的 MySQL daemon,把 datadir 的 ib* 搬走,再啟動 MySQL daemon。
  9. 重新匯入 table:
    mysql -u root -pMY_PASSWORD TABLE_SCHEMA < TMP_DIR/TABLE_SCHEMA.TABLE_NAME.sql
  10. 用 CHANGE MASTER TO … 指令開始讓新機器的 MySQL 成為舊 server 的 SLAVE。
  11. 同步完成後,找時間讓舊的 server 退下,讓新的 server 佔用舊機器的 IP。
  12. 在新機器的 MySQL 執行 RESET SLAVE,停掉 replication。

分享此文:

  • Tweet

By Joe Horn • Database 0 • Tags: ibdata, innodb_file_per_table, MySQL, mysqldump, shrink, XtraBackup

7 月 10 2014

[PHP] 自製金錢分攤函式

近日在工作上應該會需要計算金錢分攤,簡單寫了個 PHP 函式備用。

function shareAmount( $amount , $shares ) {
    $arr = array();
    if ( is_numeric($amount) && is_int($shares) ) {
        $precision = is_int($amount) ? 0 : strlen(substr($amount, strpos($amount, '.')+1));
        for ( $i = $shares; $i > 0; $i-- ) {
            $val = round( $amount / $i , $precision );
            array_push( $arr , $val );
            $amount -= $val;
        }
    }
    return $arr;
}

執行結果大概像這樣:

// var_dump( shareAmount(101,3) );
array(3) {
  [0]=>
  float(34)
  [1]=>
  float(34)
  [2]=>
  float(33)
}

// var_dump( shareAmount(0.5101,3) );
array(3) {
  [0]=>
  float(0.17)
  [1]=>
  float(0.1701)
  [2]=>
  float(0.17)
}

我在資料庫沒看到大數,所以就沒考慮 BC Math,直接用 is_numeric() 了… :p

分享此文:

  • Tweet

By Joe Horn • PHP 0 • Tags: PHP

5 月 4 2014

Javascript 的變數範圍

最近在微調部門某個 PHP 專案程式,該專案使用 ExtJS 作前端介面 framework …
整理這個專案內的 Javascript 程式讓我覺得… 應該有不少人忽略,或是不在意 Javascript 的變數定義方式。

先來看這串程式碼:

執行結果:

這張圖有趣就在第 40, 41 行程式碼執行的結果差異,以及 42, 43 行程式碼執行的結果差異。
在 Javascript 不透過 var 進行定義之變數,皆為全域變數;但透過 var 定義之變數就有變數範圍。
這兩種變數定義方式影響瀏覽器開啟網頁後的記憶體耗用量,以及 Javascript engine 進行 GC 的效率與結果,不得不慎呀…

分享此文:

  • Tweet

By Joe Horn • Javascript 0 • Tags: Garbage collection, GC, Javascript, variable scope

«‹ 2 3 4 5›»

Site Info

本站小貼紙
本站小圖
There are lots of zh_TW words encoded with UTF-8 in this Blog.
創用 CC 授權條款
本站所有內容係採用創用 CC Attribution-NonCommercial-NoDerivatives 4.0 國際 授權條款授權.

About Me


profile for Joe Horn at Stack Overflow, Q&A for professional and enthusiast programmers

My mail!

獅子座

Coffee Powered!

F1 fans

motoGP fans

Linkin Park

I am a Taiwanese!

Recent Comments

  • Avatar of johnpupu johnpupu: PHP 還有這個 phpsavant.c……
  • Avatar of Jerry Jerry: 这个不是foreach的问题。 0 ==……
  • Avatar of Joe Horn Joe Horn: 看來問題在 if ... else ..……
  • Avatar of jnlin jnlin: 因為 'b' 被轉型成 0 了…
  • Avatar of 路人 路人: 跟 foreach 沒有關係 ?…
  • Avatar of bill bill: 註冊表那裡要設定 BasicAuthLe……
  • Avatar of 虫 虫: .svn 的檔案減少可以增加在 wind……
  • Avatar of mars mars: 如果說寫程式是理性極致的話,那寫小說就是……
  • Avatar of Joe Horn Joe Horn: 已更新文章。…
  • Avatar of jackcal jackcal: joehorn.idv.tw關於轉貼 h……

Post Categories

  • About My Sites (16)
  • Computer Hardware (24)
  • Computer Software (45)
  • Database (22)
  • FreeBSD (21)
  • Funny (14)
  • Life (23)
  • Linux (5)
  • Mail (19)
  • Network (11)
  • Programing (40)
    • .NET (5)
    • JAVA (2)
    • Javascript (6)
    • PHP (29)
  • Thoughts (34)
  • Windows (13)
  • WWW (79)
    • phpBB (7)
    • WordPress (18)

Blogroll

  • Huckly 的 Blog
  • RB susu 的 Blog
  • 這裡沒有美食
  • ziway 的 Blog
  • 小恬的 Blog
  • 哈寶的 Blog
  • 日落的 Blog
  • DK Moto Club
  • ケロン軍團戰略室 (INGRESS)
  • 小麻的 Blog

Tags Cloud

AMD Apache Bloglines Coppermine DNSBL domain name eAccelerator extension Firefox Formula 1 free FreeBSD Gmail Google HDD Hsin-chu IE Intel Javascript Lenovo Longhorn Microsoft MSN MySQL Office PCHome Percona XtraBackup performance PHP phpBB pirate Postfix restaurant RSS sendmail software SpamAssassin Subversion SVN Taiwan theme translation Windows WordPress Yahoo

Ads

↑

© Joe Horn 的啟示錄 2023
Powered by WordPress • Themify WordPress Themes