2009-03-19 10:26

動態調整 PHP 最大記憶體上限(memory_limit)

在使用 php 寫 script 去做排程時
這類的 script 執行次數通常不會很多
可是卻會使用大量的 memory
雖然一個好的程式不應該如此肥胖
但為了開發上的效率和整體結構
就允許這小小的肥胖吧!

雖然可以在 php.ini 中設定 memory_limit 的值
但這並不是一個很好的方法
因為在 php.ini 中屬於全域設定
這代表每一支 script 都可以使用這麼大 memory
再開發時沒注意到的話
這只會讓胖胖的程式愈來愈多
這不是一件好事

所以呢?(廢話這麼多)
在程式中動態調整所需的 memory 會是比較好的作法
只要在程式一剛開始的地方做設定就好了
這樣也不用為了少數幾支肥胖的程式去調整 php.ini

<?php
ini_set("memory_limit","2048M");
//.......
?>
2009-03-19 01:25

[Shell] 利用 tar 作資料備份

一般在 console 使用 tar 做備份壓縮時,都會習慣使用相對路徑做壓縮,在解壓縮時也比較方便,可是再寫 Shell 做 crontab 排程時,也想要使用相對路徑做壓縮的話,必須用一些方法,下面的 script 還加上過濾檔案的機制,可以排除一些隱藏檔或特定目錄的備份。

#!/bin/bash
# Program : 備份指定目錄
# History : 2009/03/19
# Author : Jax
# E-mail : weskerjax@gmail.com
# Website : http://jax-work-archive.blogspot.com/

PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH

# 排除不需要備份的檔案類型
EXCLUDE=""
FILTER=(
"*.bak"           # 排除副檔名為 .bak 的檔案
".*"              # 排除所有隱藏檔
"/CVS/"           # 排除所有目錄為 CVS 的目錄
"Smarty/cache"    # 排除路徑為 Smarty/cache 的目錄
"Smarty/tpl_c"    # 排除路徑為 Smarty/tpl_c 的目錄
)
for i in "${FILTER[@]}" ; do
    EXCLUDE="$EXCLUDE --exclude=$i"
done

# 時間參數
DATE=$(date +%Y%m%d)

# 配份目錄
TARGET_DIR="/home/web"

# 備份檔名路徑目錄
ZIP_FILE="/home/backup/backup_$DATE.tar.bz2"

# 執行壓縮指令
tar -jcf $ZIP_FILE $EXCLUDE -C $TARGET_DIR $(ls $TARGET_DIR)

exit 0;


原始檔:server_backup.sh
2009-03-04 21:52

製作 sitemap 的心得

最近在為網站製作 sitemap 產生器,花了半小時去看 sitemaps.org - 通訊協定,這跟 Google 網站管理員工具的定義是一樣的,基本上 sitemap 的格式是很簡單的,詳細說明請察看官方文件。

網路上有很多 sitemap 產生器,但產生出來的結果不如期望,為了做出更好的 sitemap 清單,我就自己作網站的 sitemap 產生器。


需要注意的小細節:
  1. sitemap 索引與 sitemap 中 url 引入的 xsd 是不一樣的
  2. 注意檔案大小 10MB 及筆數的上限 50,000 筆 URL。
  3. 檔案如果很大最好用 gzip 壓縮,壓縮率會有 10 倍以上的落差。
  4. 一定要用驗證器確認格式是否正確,在壓縮前請先確認格式,驗證器無法解析 gzip
  5. 一份 sitemap 中的 priority 值不能全部一樣,Google 會出現 warning。
  6. 如果檔案已經封存不會變動,在 changefreq 的值最好設定為 never ,這樣會得到較好的文件索引。


Sitemap:
<?xml version='1.0' encoding='UTF-8'?>
<urlset 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9"
    url="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
>
   <url>
      <loc>http://www.example.com/</loc>
      <lastmod>2005-01-01</lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.8</priority>
   </url>
   <url>
      <loc>http://www.example.com/catalog?item=12&desc=v_hawaii</loc>
      <lastmod>2005-01-01</lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.8</priority>
   </url>
   <url>
      <loc>http://www.example.com/catalog?item=83&desc=v_usa</loc>
      <lastmod>2005-01-01</lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.8</priority>
   </url>
</urlset>


Sitemap 索引檔:
<?xml version='1.0' encoding='UTF-8'?>
<sitemapindex 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9"
    url="http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd"
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
>
   <sitemap>
      <loc>http://www.example.com/sitemap1.xml.gz</loc>
      <lastmod>2004-10-01T18:23:17+00:00</lastmod>
   </sitemap>
   <sitemap>
      <loc>http://www.example.com/sitemap2.xml.gz</loc>
      <lastmod>2005-01-01</lastmod>
   </sitemap>
</sitemapindex>


Sitemap 驗證器:
Google Sitemap(s) Validator


參考文章:
Peter的部落格: Google Sitemaps 教學
網站登錄與提交sitemap(Yahoo!) @ 神鵰蝦
2009-02-04 22:45

IE6 中的最大最小寬度和高度

/* 最小寬度 */
.min_width {
    min-width: 300px;
    /* sets max-width for IE */
    _width: expression(
        document.body.clientWidth < 300 ? "300px" : "auto"
    );
}

/* 最大寬度 */
.max_width{
    max-width:600px;
    /* sets max-width for IE */
    _width:expression(
        document.body.clientWidth > 600 ? "600px" : "auto"
    );
}

/* 最小高度 */
.min_height{
    min-height:200px;
    /* sets min-height for IE */
    _height:expression(
        this.scrollHeight < 200 ? "200px" : "auto"
    );
}

/* 最大高度 */
.max_height{
    max-height:400px;
    /* sets max-height for IE */
    _height:expression(
        this.scrollHeight > 400 ? "400px" : "auto"
    );
}

/* 最大最小寬度 */
.min_and_max_width{
    min-width:300px;
    max-width:600px;
    /* sets min-width & max-width for IE */
    _width: expression(
        document.body.clientWidth < 300 ? "300px" :
            ( document.body.clientWidth > 600 ? "600px" : "auto")
    );
}

/* 最大最小高度 */
.min_and_max_height{
    min-height:200px;
    max-height:400px;
    /* sets min-height & max-height for IE */
    _height: expression(
        this.scrollHeight < 200 ? "200px" :
            ( this.scrollHeight > 400 ? "400px" : "auto")
    );
}


參考來源:
IE中的最大最小宽度和高度 - barrydiu的专栏
DIV CSS网页布局:最小高度(min-height)的妙用
2008-12-21 20:20

apache 301 Redirect 永久重新導向

最近因為 Cookie 重複的問題花了不少時間
當 wacanai.com 及 www.wacanai.com 同時在一台主機上
用遊覽器瀏覽 www.wacanai.com 的網站時
會因為之前在 wacanai.com 所存的 Cookie 變數
當變數名稱重複時瀏覽器只會送出 wacanai.com 的變數
雖然這兩個 domain 在 DNS 上都是正確
但為了避免這樣的問題發生
一台主機最好不要同時擁有這兩種 domain

所以我利用 apache 中的 Rewrite 模組功能
作永久性的重新導向
在瀏覽時都導去 www.wacanai.com 這個網址

首先先確定 Rewrite 模組已經載入了
$ sudo a2enmod rewrite

Apache 還有一個重要的目錄設定就是:
<Directory /myblogroot/>
    AllowOverride FileInfo
    Options FollowSymlinks
</Directory>

如果沒有將這個設定加上
Rewrite 在啟用時會出現 http 403 的錯誤

可以利用 .htaccess 作以下轉址設定
RewriteEngine On
RewriteCond %{HTTP_HOST} ^wacanai\.com
RewriteRule (.*) http://www.wacanai.com/$1 [R=301,L]


參考網址:
301 Redirects & Canonical Redirects for Apache
Apache之AllowOverride參數詳解
2008-12-18 23:38

find 指令進階用法 [Linux]

find 指令結構
find [path] [expression]
[path]啟始路徑
[expression]查詢參數

基本參數
-name 以指定的範本搜尋檔案名稱
-iname 同上,但不區分大小寫
find . -name 'a*.jpg'
在當前目錄下搜尋開頭為 a 的圖片檔案

-path 以指定的範本搜尋檔案路徑
-ipath 同上,但不區分大小寫
find . -path '*pics/*pic_*.jpg'
在當前目錄下搜尋 ./pics/pic_01.jpg, ./01/pics/pic_a1.jpg, ./02/pics/01/pic_aa.jpg 這些檔案

-regex 以正規表示式搜尋檔案路徑
-iregex 同上,但不區分大小寫
find . -regex '.*pic_[0-9]+\.jpg'
在當前目錄下搜尋 pic_01.jpg, pic_03.jpg, pic_03.jpg 這些檔案

-type 搜尋檔案類型:一般檔案 (f),裝置檔案 (b,c),目錄(d),連結檔(l),socket(s),FIFO(p)
find . -type l
在當前目錄下搜尋所有連結檔

-size 搜尋檔案大小:byte(c),Kilobytes(k),Megabytes(M),Gigabytes(G)
find . -size 3k
在當前目錄下搜尋大小為(3kb)的檔案

find . -size +3k
在當前目錄下搜尋大於(3kb)的檔案

find . -size -3k
在當前目錄下搜尋小於(3kb)的檔案


時間參數
-mtime 搜尋檔案的修改時間(天)
-mmin 同上,以分鐘為單位
-ctime 搜尋檔案的建立時間
-cmin 同上,以分鐘為單位
-atime 搜尋檔案的最後開啟時間
-amin 同上,以分鐘為單位
find . -mtime 3
在當前目錄下搜尋3天時修改的檔案

find . -mtime +3
在當前目錄下搜尋3天前修改的檔案

find . -mtime -3
在當前目錄下搜尋3天內修改的檔案


帳戶參數
-user 搜尋指定帳戶名稱的檔案
-nouser 同上,但是搜尋非帳戶名稱的檔案(-not -user)
find . -user ftp
在當前目錄下搜尋使用者名稱為(ftp)的檔案

find . -nouser ftp
在當前目錄下搜尋使用者名稱非(ftp)的檔案

-group 搜尋指定群組名稱的檔案
-nogroup 同上,但是搜尋非群組名稱的檔案(-not -group)
find . -group ftp
在當前目錄下搜尋群組名稱為(ftp)的檔案

find . -nogroup ftp
在當前目錄下搜尋群組名稱非(ftp)的檔案

-perm 搜尋指定的檔案權限
find . -perm 744
在當前目錄下搜尋檔案權限為(744)的檔案

find . -perm +744
在當前目錄下搜尋檔案權限小於(744)的檔案,(600,444)

find . -perm -744
在當前目錄下搜尋檔案權限大於(744)的檔案,(744,774)

如果上面的指令你都會了,來點特別的吧!

邏輯處理
() 將搜尋條件優先處理,在使用上記得用'\'跳脫
-not 將搜尋條件反相
! 同上,在使用上記得用'\'跳脫
-and 邏輯的(和)
-a 同上
-or 邏輯的(或)
-o 同上

find . \(-user ftp -perm 744\) -or \(-group ftp\)
在當前目錄下搜尋
使用者名稱為(ftp)且權限為(744)的檔案

群組名稱為(ftp)的檔案

其他
-delete 將搜尋出來的檔案刪除
find . -user ftp -delete
刪除使用者名稱為(ftp)的檔案

-printf 將搜尋出來的結果以自訂格式輸出
find . -printf '%p\n'

-fprintf 將搜尋出來的結果以自訂格式輸出至文件
find . -fprintf a.txt '%p\n'

參考來源:
find - Linux Command - Unix Command
2008-12-06 02:18

離開頁面前的對話匡 window.onBeforeUnload 事件

在頁面改變前會觸發 window.onBeforeUnload 事件
但要怎麼產生出離開前的確認匡呢?
根據在 MSDN(onbeforeunload Event) 找到的說明
When a string is assigned to the returnValue property of window.event, a dialog box appears that gives users the option to stay on the current page and retain the string that was assigned to it. The default statement that appears in the dialog box, "Are you sure you want to navigate away from this page? ... Press OK to continue, or Cancel to stay on the current page.", cannot be removed or altered.

我們只要對這個事件 return 一個訊息就可以達成
除了 undefined 以外的所有值都會產生確認匡

那到底要怎麼做呢?
/*註冊事件*/
window.onbeforeunload = function(){
    return "是否要離開此頁面";
};

/*解除事件*/
window.onbeforeunload = null;


就這樣簡單的幾行就可以達成
真是簡單到不行
而且大多數的瀏覽器都支援

展示頁面(Demo Page)
2008-12-05 00:52

Wacanai 改版了

花了一個多月的時間
終於將新的版面上線了
除了版面上的改變外
這次也降低 JavaScript 的使用量
根據之前的經驗
太多的 JavaScript 只會讓瀏覽器當掉
在 CSS 樣式上的規劃也改善不少
當然也為了介面與功能配置上做了一些修改
雖然整體來說還不是很完整
但希望新的版面能讓使用者感覺更親切

我們的網站:www.wacanai.com