Archive for the ‘Programming/php’ Category.
Amazon EC2 / Amazon Linux AMI / AWS / 安裝 LAMP 套件
開 Amazon Linux AMI x86_64 EBS
// 裝 mysql client 及開發會用到的 libs yum install mysql mysql-libs mysql-devel // 裝 apache yum install httpd httpd-devel // 裝 php 及 php 套件 yum install php php-devel php-mysql php-gd php-dom php-pear php-json php-xml php-xmlrpc // 裝 subversion yum install svn
把 wordpress blog 立即變成 mobile friendly 的套件
阿毛介紹的: http://mobilepress.co.za/
裝好後它會偵測 user 的 user agent 若是 mobile 就會把頁面換成 ‘手機’ 版.
Question: google chart api
Open flash chart 有這樣的 數值 data 在線上
可是要怎樣 google chart 才會有呢?
google chart: http://code.google.com/intl/zh-TW/apis/chart/docs/chart_params.html
感謝 Hunter 提供解答 ^_^
hunter 說:
新版是有標示,看看是不是你要的:
http://chart.apis.google.com/chart?cht=bvg&chs=250×150&chd=t:20,66,40,55,20,90&chxt=x,y&chxs=0,000000,12,0,lt|1,000000,10,1,lt&chm=o,ff0000,0,,3|o,ff0000,0,,3,,c|o,ff0000,0,,3,,s|N,000000,0,0,10,,rs|N,000000,0,1,10,,ls|N,000000,0,2,10,,c|N,000000,0,3,10,,e|N,000000,0,4,10,,e::15|N,000000,0,5,10,,e::-12
Rimmon 2.0 說:
YES , 這樣就夠了
hunter 說:
http://code.google.com/intl/zh-TW/apis/chart/docs/chart_params.html#gcharts_data_point_labels
[php] inline string vs 兜字串的寫法 速度比較 / 測試 sample code
function timeFunc($function, $runs) {
$times = array();
for ($i = 0; $i < $runs; $i++) {
$time = microtime();
call_user_func($function);
$times[$i] = microtime() - $time;
}
return array_sum($times) / $runs;
}
function Method1() {
$foo = 'some words';
for ($i = 0; $i < 10000; $i++)
$t = "these are $foo";
}
function Method2() {
$foo = 'some words';
for ($i = 0; $i < 10000; $i++)
$t = "these are {$foo}";
}
function Method3() {
$foo = 'some words';
for ($i = 0; $i < 10000; $i++)
$t = sprintf("these are %s", $foo);
}
function Method4() {
$foo = 'some words';
for ($i = 0; $i < 10000; $i++)
$t = "these are " . $foo;
}
function Method5() {
$foo = 'some words';
for ($i = 0; $i < 10000; $i++)
$t = 'these are ' . $foo;
}
print timeFunc('Method1', 10) . "\n";
print timeFunc('Method2', 10) . "\n";
print timeFunc('Method3', 10) . "\n";
print timeFunc('Method4', 10) . "\n";
print timeFunc('Method5', 10) . "\n";
RESULT:
0.0023114
0.002422
0.0072015
0.001946
0.0017625
但是這是 loop 10萬次才有一點點的差距.
結論是 method5(單 quote + concate string) 速度較佳! method3 (sprintf) 最慘!
inline 寫法有兩種, 這兩種都差不多, 所以就 codeing 時的手順上, 以 $foo 這種是比較好寫這也是多數人的寫法 , {$foo} 這種比較好閱讀.
若是在需求上需要加上換行的話, 在一個 strings 中用 inline 寫法把 \n 放在 quote 中, 這種速度卻又比 concate string 寫法來得快.
ps: 速度比較這種題目很多人都會陷入某種迷失, 每個人的環境/需求都不盡相同, 並不是最快的就是適合自己的.
[php] array_key_exists vs isset 那一個比較快?
這些微差異要在大量的 loop 中才會有一些差別, 不過還是寫 code 時一次寫好吧 —> 給我自己的提醒!
答案是 isset 比較快!
ref. URL: http://www.alternateinterior.com/2006/11/comparing-array_key_exists-with-isset.html
相關資訊: A HOWTO on Optimizing PHP
http://phplens.com/lens/php-book/optimizing-debugging-php.php
Tuning Apache and PHP for Speed on Unix
http://phplens.com/phpeverywhere/tuning-apache-php
CodeIgniter MVC CRUD + memcache = Secret Message http://msg.monster.com.tw
這個 idea 是來自這個網站 : https://privnote.com/ , 簡單的說它是一個經由 https 保護傳送內容的網路服務 , user 用這個服務把臨時要給朋友的機密/私人資料譬如 password / URL 之類的 data 存放在這網站上, 然後此系統會給 user 一個唯一網址, 再把這網址給朋友, 開啟這個唯一網址後, 就可以看到這個訊息, 系統同時會把這訊息從系統中刪除… 哈哈! 看懂了嗎?
我的改進是用 memcache 的 expire 機制, 設定 10分鐘後, 若 user 沒讀過此訊息, 訊息會 ‘自動’ 銷毀… 我的站若再去申請 ssl 加密的話就跟那個站功能是類似的了.
codeigniter 的 MVC 架構, 讓我很快的把這個 idea implement 出來了 , 基本上就是一個簡單版的 CRUD
我的 Secret Message 服務長像非常的陽春
, 僅用簡單的 HTML , 若有空再加上 style 美化一下版面, 不過基本功能是有的了, 大家用看看, 有 idea 或意見請再告訴我.
目前 message 不提供 HTML / VBB , 但是有簡單的 skype 版表情符號 ( icon 正在慢慢搜集中 ) ….
Secret Message 網址是: http://msg.monster.com.tw/
CI 表情符號改寫例:



用 php code 產生 1×1 pixel 的 小黑點 sample code
這個可以用來做 tracking 輸出用
header("Content-Type: image/gif");
header("Content-Length: 49");
echo pack('H*',
'47494638396101000100910000000000ffffffff'
.'ffff00000021f90405140002002c000000000100'
.'01000002025401003b'
);
PHP / MySQL 一些容易被忽略的 Optimization
URL : http://www.dublish.com/articles/10.html
- Use NOT NULL as default value as much as you can, it speeds up execution and saves one bit.
- 一個 <? … ?> 比多個 <? .. ?><? .. ?><? .. ?> 還快
- 少用 . 去串接字串, 改用這樣的較快 “select addr,name from tbl_addr where id=$id”
- 或者用 ‘ “ 區分出 需要 PHP 不解譯/解譯的字串
- echo 比 print 快
- 在 loop 前就把終值先算好, 放變數裡,NG: for($i=0; $i<strlen($str); $i++) ….
[php] regular expression match
preg_match( '/c=([0-9]+)/' , $t->log['dest_query'] , $data); 這樣可以取出 c=xxxx
抓 user agent 版本
preg_match( '/MSIE ([0-9]+.[0-9])/' , $_SERVER['HTTP_USER_AGENT'] , $data_msie_ver); preg_match( '/Windows NT ([0-9]+.[0-9])/' , $_SERVER['HTTP_USER_AGENT'] , $data_nt_ver); if ( !( (float)$data_msie_ver[1]>=6 && (float)$data_msie_ver[1]<=8 ) ) $valid_useragent = 0;
debian / ubuntu 裝 oracle instant client / sqlplus / oci8 / apache2 config / steps by steps
2011.0530 整理的 install step by step:
OS : uname -a
Linux ubuntu 2.6.38-8-server #42-Ubuntu SMP Mon Apr 11 03:49:04 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
Oracle client : 11.2 R1
google "oracle instant client download"
到 oracle 網頁找 Instant Client for Linux x86-64 / Version 11.2.0.1.0 (PS: Version 11.2.0.2.0 這版有問題)
download 這兩個
oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm (47,811,007 bytes)
oracle-instantclient11.2-devel-11.2.0.1.0-1.x86_64.rpm (606,343 bytes)
用 alien 裝起來
102 alien -i *basic*
103 alien -i *devel*
新增這個檔 /etc/ld.so.conf.d/oracle.conf , 內容:
/usr/lib/oracle/11.2/client64/lib/
104 cat /etc/ld.so.conf.d/oracle.conf
105 ls -l /usr/lib/oracle/
106 ls -l /usr/lib/oracle/11.2/client64/lib/
安裝 LAMP :
108 export http_proxy="http://172.30.1.123:8080"
109 apt-get install apache2 libapache2-mod-php5 php5 php5-gd mysql-server php5-mysql
安裝 libaio 套件
138 apt-get install libaio1
安裝 pear 套件
112 apt-get install php-pear
安裝 pecl 的 oci8 套件
pear config-set http_proxy http://172.30.1.123:8080/
114 pecl update-channels
115 pecl install oci8
117 find /etc -name php.ini
這兩個檔要加入 oci8 的 extentsion
/etc/php5/cli/php.ini
/etc/php5/apache2/php.ini
install ok: channel://pecl.php.net/oci8-1.4.5
configuration option "php_ini" is not set to php.ini location
You should add "extension=oci8.so" to php.ini <------ 加這行
跑看看測試code:
$conn = oci_connect('oracle_user', 'oracle_pass', 'ip_address/instant_id');
$stid = oci_parse($conn, "select to_char(sysdate, 'YYYY-MM-DD HH24:MI:SS') from dual" );
oci_execute($stid);
$row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS);
print_r( $row );
oci_close( $conn );
// ——————————–
以下是 2011.5.25 修改
69 find / -name sqlplus -print 70 /usr/lib/oracle/11.2/client64/bin/sqlplus 71 cat > /etc/ld.so.conf.d/oracle.conf 73 ls -l /usr/lib/oracle/11.2/client64/lib/ 76 vi /etc/ld.so.conf.d/oracle.conf 77 apt-get install apache2 libapache2-mod-php5 php5 php5-gd mysql-server php5-mysql 86 apt-get install php-pear 91 pear config-set http_proxy http://172.30.1.123:8080/ 92 pecl update-channels 95 pecl install oci8
2010.0630 : 今天又在 debian 裝一次 發現, 這個版本沒辦法裝 oracle-instantclient11.2.* , 所以繼續用 instantclient11.1.*
Ubuntu 安裝 RPM 要用 alien …
sudo apt-get install alien
Ref. https://help.ubuntu.com/community/HowToBuildToraWithOracle
先去 http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/linuxsoft.html
download 這幾個 files (rpm)
Instant Client Package – Basic Lite
oracle-instantclient11.1-basiclite-11.1.0.7.0-1.i386.rpm
Instant Client Package – SQL*Plus
oracle-instantclient11.1-sqlplus-11.1.0.7.0-1.i386.rpm
Instant Client Package – SDK
oracle-instantclient11.1-devel-11.1.0.7.0-1.i386.rpm
下 alien 指令安裝 rpm
alien -i oracle-instantclient11.1-basiclite-11.1.0.7.0-1.i386.rpm alien -i oracle-instantclient11.1-sqlplus-11.1.0.7.0-1.i386.rpm alien -i oracle-instantclient11.1-devel-11.1.0.7.0-1.i386.rpm
裝好後用 sqlplus 聯看看 , 因為我得到一個 lib error
所以我要裝 apt-get install libaio1
在 /etc/ld.so.conf.d 加一個檔 cat > /etc/ld.so.conf.d/oracle.conf
內容是 oracle lib 的 path /usr/lib/oracle/11.1/client/lib
然後
apt-get install php5
apt-get install php5-dev
因為 oci8 是由 pecl 來安裝的, 所以要 apt-get install php-pear
再來裝 oci8 : pecl install oci8
在 /etc/php5/conf.d 下建一個 oci8.ini 檔 , cat > /etc/php5/conf.d/oci8.ini 內容是
extension=oci8.so
重新啟動 apache2 :
service apache2 restart
看看 phpinfo 有沒有出現 oci8 等字樣就 ok 了


加一個 virtual host :
在 /etc/apache2/sites-enabled 增加一個 xxx.conf 檔
CodeIgniter 需要用 rewrite module :
ls -sf /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled



