2009年8月23日 星期日

[PHP] Is there a way to get a variable name as a string?

On rare occasions, you may need to retrieve a variable name as a string.

This handy little function retreives the name of the variable:


$my_var = 1;
echo var_name ($my_var);
?>

will give 'my_var'.


I started trying to find a solution to this problem, because I needed such a function and because questions about it appeared on several mailing lists and forums:
[PHP] Is there a way to get a variable name as a string?

most people said it was not possible...
Re: [PHP] Is there a way to get a variable name as a string?
From: Rasmus Lerdorf

In PHP it is possible to use a variable if you have its name as a string :



$iVarName = 'MaxSize';
$$iVarName = 10;
echo $MaxSize;

?>

But PHP does not natively include a way to get the name of a given variable.

This might be useful in situations where you design a function with a parameter passed by reference and you need to know which variable was sent as a parameter for this function.



The solution :

The following function retreives the variable name from a given variable:



function var_name (&$iVar, &$aDefinedVars)
{
foreach ($aDefinedVars as $k=>$v)
$aDefinedVars_0[$k] = $v;

$iVarSave = $iVar;
$iVar =!$iVar;

$aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));
$iVar = $iVarSave;

return $aDiffKeys[0];
}

?>

This function has to be called with a second parameter always set to the result of the function "get_defined_vars()":



var_name($iVar, get_defined_vars());

?>



How does it work ?

var_name compares the result of the function "get_defined_vars()" before and after modification of the variable whose name we want to find.

The initial set of defined variables passed as a parameter to var_name is first stored in "$aDefinedVars_0" and compared later.

Before modifying the value of the variable, its value is saved in $iVarSave.

$iVar is then changed to something different :
$iVar=!$iVar;

$aDefinedVars keeps track of this modification and can be compare to $aDefinedVars_0 (its initial value). The difference is the variable we modified and we can get its name as a string in the key value of the array record.

The value of the variable is restored to its initial value kept in $iVarSave and the name of the variable is returned.



Example :

The following PHP code shows the function var_name in action:



$v_1 = 1; echo 'var $',var_name ($v_1, get_defined_vars()),' = ',var_dump($v_1),'
';
$v_2 = 4; echo 'var $',var_name ($v_2, get_defined_vars()),' = ',var_dump($v_2),'
';
$v_3 = 'qwerty'; echo 'var $',var_name ($v_3, get_defined_vars()),' = ',var_dump($v_3),'
';
$v_4 = array('aa'=>'11','bb'=>'22',3); echo 'var $',var_name ($v_4, get_defined_vars()),' = ',var_dump($v_4),'
';
$v_5 = &$v_2; echo 'var $',var_name ($v_5, get_defined_vars()),' = ',var_dump($v_5),'
';
echo 'var $',implode (' / $',var_name ($v_5, get_defined_vars(),true)),' = ',var_dump($v_5),'
';

function test()
{
$v_1 = 'qwerty'; echo 'var $',var_name ($v_1, get_defined_vars()),' = ',var_dump($v_1),'


';
global $v_5; echo 'var $',var_name ($v_5, get_defined_vars()),' = ',var_dump($v_5),'


';
}

test();

?>

var_name can retreive the name of the variable in every case.



Application :

The most direct application of this function is, of course, a "dump" function.
Just call this "dump" function with the variable you want to monitor - do not forget to add the "get_defined_vars()" parameter...



function dump(&$v, &$aDefinedVars)
{
echo '
$',implode (' / $',var_name ($v, $aDefinedVars, true)),' = ',print_r($v, true),'


';
}

dump($v_4, get_defined_vars());

?>



Going further :

The function var_name can be enhanced by adding the parameter $bShowAllRef to show, if set to true, all the references relating to the variable passed as a parameter:



function var_name (&$iVar, &$aDefinedVars, $bShowAllRef=false )
{
foreach ($aDefinedVars as $k=>$v)
$aDefinedVars_0[$k] = $v;

$iVarSave = $iVar;
$iVar =!$iVar;

$aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));
$iVar = $iVarSave;

return ($bShowAllRef? $aDiffKeys: $aDiffKeys[0]);
}

?>

That's it ! You can download the function here.

This code is free to use and published under the GPL license.

function var_name (&$iVar, &$aDefinedVars)
{
foreach ($aDefinedVars as $k=>$v)
$aDefinedVars_0[$k] = $v;

$iVarSave = $iVar;
$iVar =!$iVar;

$aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));
$iVar = $iVarSave;

return $aDiffKeys[0];
}

摘自:http://mach13.com/how-to-get-a-variable-name-as-a-string-in-php

2009年8月19日 星期三

RewriteRule 記事

想用RewriteRule把網址轉成 .html

www.xxx/test.php
www.xxx/1st/test2.php
www.xxx/1st/2st/test3.php

轉成
www.xxx/test.html
www.xxx/1st/test2.html
www.xxx/1st/2st/test3.html

#啟動 Rewrite
RewriteEngine On
#設定重寫的目錄
RewriteBase /
#設定重寫規則
RewriteRule ^(.+)\.htm$ $1.php

URL Rewriting Engine

http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

apache RewriteRule 的問題

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html


RewriteRule IIS映射

安裝說明:

1、在IIS的Isapi上新增這個篩選器,篩選器名稱 re,可執行檔案選擇 Rewrite.dll ;
2、設定httpd.ini檔案

RewriteRule /news/(\d+)\.html /jsp/news\.jsp\?id=$1 [N,I]
表示將news.jsp?id=95 轉換成news/95.html

以此類推,如果你有更多的參數,則增加相應的(\d+),而後面的id=$1,則增加id=$2,等等。

目前較為普遍的動態網頁包括asp,php,jsp,shtml,jhtml,cgi......甚至還有一些自己定義的,比如:aspx,do,index,hello等等。表現形式為:news.asp?id=95。建議讀者通過re_write將其轉換成news/95.html,以便於google對改網頁的識別。

re_write是iis裡的一個模塊,你可以從http://www.GoogleDmoz.com/dirtool/re_write.zip

當需要將news.jsp?id=95的映射成news/95.html時,只需設定httpd.ini檔案:
RewriteRule /news/(\d+)\.html /news\.jsp\?id=$1 [N,I]
這樣就把 /news/95.html 這樣的請求映射成了 /news.jsp?id=95

然後你在連接處這樣寫:95新聞。
如果你的新聞是通過資料庫循環讀取出來的,那麼寫法是:
while(rs.next())
{
String id = (String)rs.getString('id');
out.print('');
out.print('95新聞');
out.print('
');
}

如果你在處理資料翻頁,那麼寫法是:

More_<%=Page%>_<%=type%>.html (註:page是翻頁頁數,type是資料類型)
表現形式:More_1_95.html

如果翻下一頁,則為:More_2_95.html,繼續下一頁的循環,則是:
More_3_95.html,以此類推。

不過你需要在httpd.ini檔案中增加以下代碼:
RewriteRule /More_(\d+)_(\d+)\.html /jsp/more\.jsp\?page=$1&type=$2 [N,I]

如果你的動態程式有多個參數需要傳遞,那麼就增加多個(\d+)即可,如下:

RewriteRule /More_(\d+)_(\d+)_(\d+)\.html /jsp/more\.jsp\?page=$1&type=$2&type2=$3 [N,I]

翻頁處理表現形式是:More_1_95.html

Apache的Mod_rewrite

Syntax: RewriteRule Pattern Substitution [flags]

  一條RewriteRule指令,定義一條重寫規則,規則間的順序非常重要。對Apache1.2及以後的版本,模板(pattern)是一個POSIX正則式,用以匹配當前的URL。當前的URL不一定是用記最初提交的URL,因為可能用一些規則在此規則前已經對URL進行了處理。


  對mod_rewrite來說,!是個合法的模板前綴,表示「非」的意思,這對描述「不滿足某種匹配條件」的情況非常方便,或用作最後一條默認規則。當使用!時,不能在模板中有分組的通配符,也不能做後向引用。


  當匹配成功後,Substitution會被用來替換相應的匹配,它除了可以是普通的字符串以外,還可以包括:

1. $N,引用RewriteRule模板中匹配的相關字串,N表示序號,N=0..9
2. %N,引用最後一個RewriteCond模板中匹配的資料,N表示序號
3. %{VARNAME},服務器變數
4. ${mapname:key|default},映射函數呼叫


這些特殊內容的擴展,按上述順序進行。
  一個URL的全部相關部分都會被Substitution替換,而且這個替換過程會一直持續到所有的規則都被執行完,除非明確地用L標誌中斷處理過程。
  當susbstitution有」-」前綴時,表示不進行替換,只做匹配檢查。
  利用RewriteRule,可定義含有請求串(Query String)的URL,此時只需在Sustitution中加入一個?,表示此後的內容放入QUERY_STRING變數中。如果要清空一個QUERY_STRING變數,只需要以?結束Substitution串即可。
  如果給一個Substitution增加一個


1. 'redirect|R [=code]' (強制重定向)
  給當前的URI增加前綴


2. 'forbidden|F' (強制禁止訪問URL所指的資源)
  立即傳回狀態值403 (FORBIDDEN)的應答包。將這個標誌與合適的RewriteConds 聯合使用,可以阻斷訪問某些URL。


3. 'gone|G' (強制傳回URL所指資源為不存在(gone))
  立即傳回狀態值410 (GONE)的應答包。用這個標誌來標記URL所指的資源永久消失了.


4. # 'proxy|P' (強制將當前URL送往代理模塊(proxy module))
  這個標誌,強制將substitution當作一個發向代理模塊的請求,並立即將共送往代理模塊。因此,必須確保substitution串是一個合法的URI (如, 典型的情況是以


5. 'last|L' (最後一條規則)
  中止重寫流程,不再對當前URL施加更多的重寫規則。這相當於perl的last命令或C的break命令。


6. 'next|N' (下一輪)
  重新從第一條重寫規則開始執行重寫過程,新開的過程中的URL不應當與最初的URL相同。 這相當於Perl的next命令或C的continue命令. 千萬小心不要產生死循環。


7. # 'chain|C' (將當前的規則與其後續規則綑綁(chained))
  當規則匹配時,處理過程與沒有綑綁一樣;如果規則不匹配,則綑綁在一起的後續規則也不在檢查和執行。


8. 'type|T=MIME-type' (強制MIME類型)
  強制將目標檔案的MIME-type為某MIME類型。例如,這可用來模仿mod_alias模塊對某目錄的ScriptAlias指定,通過強制將該目錄下的所有檔案的類型改為 「application/x-httpd-cgi」.


9. 'nosubreq|NS' (used only if no internal sub-request )
  這個標誌強制重寫引擎跳過為內部sub-request的重寫規則.例如,當mod_include試圖找到某一目錄下的默認檔案時 (index.xxx),sub-requests 會在Apache內部發生. Sub-requests並非總是有用的,在某些情況下如果整個規則集施加到它上面,會產生錯誤。利用這個標誌可排除執行一些規則。


10. 'nocase|NC' (模板不區分大小寫)


  這個標誌會使得模板匹配當前URL時忽略大小寫的差別。

11. 'qsappend|QSA' (追加請求串(query string))
  這個標誌,強制重寫引擎為Substitution的請求串追加一部分串,則不是替換掉原來的。借助這個標誌,可以使用一個重寫規則給請求串增加更多的資料。


12. 'noescape|NE' (不對輸出結果中的特殊字符進行轉義處理)
  通常情況下,mod_write的輸出結果中,特殊字符(如'%', '$', ';', 等)會轉義為它們的16進制形式(如分別為'%25', '%24', and '%3B')。這個標誌會禁止mod_rewrite對輸出結果進行此類操作。 這個標誌只能在 Apache 1.3.20及以後的版本中使用。


13. 'passthrough|PT' (通過下一個處理器)
  這個標誌強制重寫引擎用filename字段的值來替換內部request_rec資料結構中uri字段的值。. 使用這個標誌,可以使後續的其它URI-to-filename轉換器的Alias、ScriptAlias、Redirect等指令,也能正常處理RewriteRule指令的輸出結果。用一個小例子來說明它的語義:如果要用mod_rewrite的重寫引擎將/abc轉換為/def,然後用mod_alas將/def重寫為ghi,則要:

RewriteRule ^/abc(.*) /def$1 [PT]
Alias /def /ghi

如果PT標誌被忽略,則mod_rewrite也能很好完成工作,如果., 將 uri=/abc/... 轉換為filename=/def/... ,完全符合一個URI-to-filename轉換器的動作。接下來 mod_alias 試圖做 URI-to-filename 轉換時就會出問題。
注意:如果要混合都含有URL-to-filename轉換器的不同的模塊的指令,必須用這個標誌。最典型的例子是mod_alias和mod_rewrite的使用。


14. 'skip|S=num' (跳過後面的num個規則)
  當前規則匹配時,強制重寫引擎跳過後續的num個規則。用這個可以來模仿if-then-else結構:then子句的最後一條rule的標誌是skip=N,而N是else子句的規則條數。


15. 'env|E=VAR:VAL' (設定環境變數)
  設定名為VAR的環境變數的值為VAL,其中VAL中可以含有正則式的後向引用($N或%N)。這個標誌可以使用多次,以設定多個環境變數。這兒設定的變數,可以在多種情況下被引用,如在XSSI或CGI中。另外,也可以在RewriteCond模板中以%{ENV:VAR}的形式被引用。


16. 注意:一定不要忘記,在服務器範圍內的配置檔案中,模板(pattern)用以匹配整個URL;而在目錄範圍內的配置檔案中,目錄前綴總是被自動去掉後再進行模板匹配的,且在替換完成後自動再加上這個前綴。這個功能對很多種類的重寫是非常重要的,因為如果沒有去前綴,則要進行父目錄的匹配,而父目錄的訊息並不是總能得到的。一個例外是,當substitution中有http://打頭時,則不再自動增加前綴了,如果P標誌出現,則會強制轉向代理。


注意:如果要在某個目錄範圍內啟動重寫引擎,則需要在相應的目錄配置檔案中設定「RewriteEngine on」,且目錄的「Options FollowSymLinks」必須設定。如果管理員由於安全原因沒有打開FollowSymLinks,則不能使用重寫引擎。

http://hostname開頭),否則會從代理模塊得到一個錯誤. 這個標誌,是ProxyPass指令的一個更強勁的實現,將遠程請求(remote stuff)映射到本地服務器的名字空間(namespace)中來。

  注意,使用這個功能必須確保代理模塊已經編譯到Apache 服務器程式中了. 可以用「httpd -l 」命令,來檢查輸出中是否含有mod_proxy.c來確認一下。如果沒有,而又需要使用這個功能,則需要重新編譯``httpd''程式並使用mod_proxy有效。 http://thishost[:thisport]/, 從而生成一個新的URL,強制生成一個外部重定向(external redirection,指生的URL發送到客戶端,由客戶端再次以新的URL發出請求,雖然新URL仍指向當前的服務器). 如果沒有指定的code值,則HTTP應答以狀態值302 (MOVED TEMPORARILY),如果想使用300-400(不含400)間的其它值可以通過在code的位置以相應的數字指定,也可以用標誌名指定: temp (默認值), permanent, seeother.

注意,當使用這個標誌時,要確實substitution是個合法的URL,這個標誌只是在URL前增加http://thishost[:thisport]/前綴而已,重寫操作會繼續進行。如果要立即將新URL重定向,用L標誌來中重寫流程。 http://thishost[:port]的前綴,則mod_rewrite會自動將此前綴去掉。因此,利用http://thisthost做一個無條件的重定向到自己,將難以奏效。要實現這種效果,必須使用R標誌。

Flags是可選參數,當有多個標誌同時出現時,彼此間以逗號分隔。

摘自:http://bbs.flash2u.com.tw/dv_rss_xhtml_54_92453_4.html

2009年8月18日 星期二

Big5 form serialize後的編碼

Form(中文big5未urlencode) => form serialize => browser => 中文utf8 => php (中文utf8)
Form(中文big5已urlencode) => form serialize => browser => urlencode碼 => php urldecode (中文big5)

2009年8月17日 星期一

[Debug]WordPress Email Publish 無法發佈到MSN Space

將權限開放給合法的「寄件者」即可

outlook -> 打開信件 -> 點選寄件者 -> 電子郵件地址

2009年8月12日 星期三

最近跟Yahoo網站合作,共同製作某一頻道

Yahoo使用Scanmus發現我們網站有XSS漏洞
網路上有很多XSS filter
目前是參考OWASP_PHP_Filters的過濾機制
覺得很不錯

2009年8月11日 星期二

Static Routes in Redhat 9 - New isn't always better

Overview

With the introduction of Redhat version 8 and continued into version 9, the /etc/sysconfig/static-routes file no longer seems to function correctly.

Linux static routes changed in 8.0 to a new format. Now you are to create a file in /etc/sysconfig/network-scripts for each Ethernet interface you wish to create static routes on.

Example:

touch /etc/sysconfig/network-scripts/route-eth0

The syntax for this file is different from the traditional route format used in /etc/sysconfig/static-routes . Redhat has yet to document the change on their web site as of June 2003.

Syntax based on a usenet post go to /etc/sysconfig/network-scripts, make a file called route-devicename (ex: route-eth0) and populate it with your static routes for that device so if you wanted to make a static route to the 192.168.0.0/24 network through 152.3.182.5 type:

192.168.0.0/24 via 152.3.182.5

Persistent static routes for ANY linux distribution

You may use this method to add static routes and it will work under any Linux distribution. However, it is considered by some a 'hack' or the 'ugly way'.

Edit your /etc/rc.local file and add your static routes using the route statement.

Example:

route add -net 10.10.98.0 netmask 255.255.255.0 gw 10.164.234.132 dev eth1
route add -net 10.164.234.96 netmask 255.255.255.252 gw 10.164.234.132 dev eth1
route add -net 10.164.234.112 netmask 255.255.255.240 gw 10.164.234.132 dev eth1

Force the old static-routes file to work under Redhat 9

Clear out the new /etc/sysconfig/network-scripts/ifup-routes script so that you can populate it with the original shell script from Redhat 7.x.

cat /dev/null > /etc/sysconfig/network-scripts/ifup-routes
vi /etc/sysconfig/network-scripts/ifup-routes

type in the following (or copy and paste) not including the tilde lines:

#!/bin/sh

# adds static routes which go through device $1

if [ "$1" = "" ]; then
echo "usage: $0 "
exit 1
fi

if [ ! -f /etc/sysconfig/static-routes ]; then
exit 0
fi

# note the trailing space in the grep gets rid of aliases
grep "^$1 " /etc/sysconfig/static-routes | while read device args; do
/sbin/route add -$args $device
done
grep "^any " /etc/sysconfig/static-routes | while read ignore type net netmask mask bogus dev ; do
if [ "$dev" = "$1" ]; then
/sbin/route add -$type $net $netmask $mask $dev
fi
done

Remember to use /etc/sysconfig/network for your default gateway

If you only intend to add one route, your default gateway, then you need not worry about the static routes file or using the route command. Simply add your default gateway in /etc/sysconfig/network.

Example

NETWORKING=yes
HOSTNAME="hostname.linux.org"
GATEWAY="10.164.234.1"
GATEWAYDEV="eth0"
FORWARD_IPV4="yes"

2009年8月4日 星期二

yum install php-mysql Error: mysql conflicts with MySQL-server-community

MySQL 5.1がGAになってからしばらく経ったのに使った事無かったので、今やっている実験プロジェクトで採用してみた。

CentOSの標準パッケージだとmysqlのバージョンは5.0。mysql関連パッケージもmysql-5.0に依存しているので、その辺をクリアする必要がある。

とりあえず手順やエラーメッセージのみを簡潔に説明。

1. 既存のMySQLパッケージを(インストールされていれば)アンインストール

2. MySQL公式サイトからのRPMパッケージをインストール
この辺りからパッケージをダウンロード。使用したのは以下のもの。

* MySQL-client-community-5.1.34-0.rhel5.i386.rpm
* MySQL-server-community-5.1.34-0.rhel5.i386.rpm
* MySQL-shared-community-5.1.34-0.rhel5.i386.rpm

rpm -ivhで普通にインストールすればOK。

ここまでは特に問題ないと思うんだけど、php-mysqlをインストールしようとして問題発生。


3. php-mysqlのインストール

[root@host downloads]# yum install php-mysql
(中略)
mysql-5.0.45-7.el5.i386 from base has depsolving problems
--> mysql conflicts with MySQL-server-community
Error: mysql conflicts with MySQL-server-community

php-mysqlはCentOS標準のmysqlパッケージに依存してるんだけど、mysqlは先ほどインストールしたMySQL-server-communityと競合している。

rpm コマンドなら出来るけど、yumコマンドだと依存性を無視したインストールが出来ない。なので、php-mysqlをダウンロードしてrpmコマンドでインストールする必要がある。その為にはyum-utilsパッケージに含まれているyumdownloaderコマンドを使う。

[root@host downloads]# yum install yum-utils
[root@host downloads]# yumdownloader php-mysql
[root@host downloads]# rpm -ivh php-mysql-5.1.6-23.2.el5_3.i386.rpm
エラー: 依存性の欠如:
libmysqlclient.so.15 は php-mysql-5.1.6-23.2.el5_3.i386 に必要とされています
libmysqlclient.so.15(libmysqlclient_15) は php-mysql-5.1.6-23.2.el5_3.i386 に必要とされて います
php-pdo は php-mysql-5.1.6-23.2.el5_3.i386 に必要とされています

とりあえずphp-pdoは普通にyumでインストール。

libmysqlclient.soに関してだけど、MySQL 5.1をインストールするとインストールされるのはlibmysqlclient.so.16。php-mysql-5.1が必要としているのは libmysqlclient.so.15。ここで選択肢は2つかなぁと思う。

* 16から15にシンボリックリンクを貼る
* libmysqlclient.so.15をインストール

前者は簡単なので、今回は後者の方法を試してみた。libmysqlclient.so.15はmysql-5.0.45-7.el5.i386.rpmに含まれている。従って以下のような方法で取り出して、/usr/lib以下にコピー。

[root@host downloads]# yumdownloader mysql
[root@host downloads]# rpm2cpio mysql-5.0.45-7.el5.i386.rpm | cpio -id
[root@host downloads]# cp -a usr/lib/mysql/libmysqlclient* /usr/lib

これで準備完了。後は依存関係を無視してphp-mysqlをインストールして、apacheを再起動すれば、PHPのMySQL関連関数を使えるようになる。

[root@host downloads]# rpm -ivh --nodeps php-mysql-5.1.6-23.2.el5_3.i386.rpm
[root@host downloads]# /etc/init.d/httpd restart


摘自:http://www.kazu.tv/blog/archives/000770.html

wibiya widget