星期三, 五月 11, 2005

PHP get Global Varable

php 在 4.x 版以後要抓全域變數都要用 $_POST, $_GET, $_COOKIE... etc.
雖然只要在 php.ini 中, 去設定 register_globals 就可以了,
不過為了安全考量, 還是別這樣子改比較好,
但是自己懶得打那麼長的字怎麼辦?
所以還是來寫個小程式來搞定它.

foreach ($_REQUEST as $key => $val) {
$$key = $val;
}
這是全域的處理法, 還是不太建議使用,

因為這就跟之前一樣意思了, 還是有限制的使用比較好,

自己一定知道前一頁是如何傳送變數過來的,

所以用以下此程式碼會比較好一點.

foreach ($_POST as $key => $val) {
$$key = $val;
}

$_POST 可改為 $_COOKIE, $_SESSION, $_GET ... 等.

相關標籤

this is comment icon [回覆]

you can try a function -- extract()

Comment by Leo (04/06/2008 22:57)

this is comment icon 回 Leo [回覆]

哈~ 這方法不錯, 不過他有特別警告就是了.. :P Do not use extract() on untrusted data, like user-input ($_GET, ...). If you do, for example, if you want to run old code that relies on register_globals temporarily, make sure you use one of the non-overwriting extract_type values such as EXTR_SKIP and be aware that you should extract in the same order that's defined in variables_order within the php.ini.
Comment by Tsung (04/06/2008 23:11)

this is comment icon 請教個關於session的問題好嗎? [回覆]

抱歉,走投無路了,借個版面請教您一個問題,打擾之處請見諒!!
我自己有設計了一個登入的程式,每當第一次登入的時候就會產生2個session的暫存檔,一個有資料一個沒有資料,然後程式會告知我沒有登入要先登入,然後我再登入一次就可以順利進入了!
如果登出後,只要我沒有關閉瀏覽器,再次進入的時候就沒有上述的問題,請問這是什麼狀況啊?

我使用的是php5.2.4,apache2.2.4,mysql5

未避免佔用太多版面,如果需要看程式碼,煩請您告知,我再貼上來,謝謝你!!

Comment by maxii (04/17/2008 13:46)

this is comment icon 回 maxii [回覆]

我猜可能是 session_start() 跑了兩次? (還是您要將程式寄到我的信箱看看?)

Comment by Tsung (04/17/2008 16:49)

this is comment icon 對不起您的mail是? [回覆]

對不起請問您的mail是?

Comment by maxii (04/17/2008 17:32)

this is comment icon 回 maxii [回覆]

厄, 左邊有我 Mail 的圖片(My Mail) :)

Comment by Tsung (04/17/2008 17:35)

this is comment icon 我是白痴!! [回覆]

抱歉,找了很久就是沒看到,我以為那個是廣告,原來是您的email address,我馬上寄
麻煩您了!

Comment by maxii (04/17/2008 17:40)
Add this page to del.icio.us Add this page to Yahoo Taiwan's bookmark Add this page to MyShare

發表迴響

標題

內容 (限制 1000 字)

暱稱

電子郵件

個人網頁


 authimage


PS: 若無法留言, 請先確認是否有打開 JavaScript, 造成您的困擾, 實在萬分對不起 Orz...(如果無法留言, 勞煩可以發信給我好嗎? 謝謝.)
PS2: 若您的留言被誤判, 我都會再自行看過, 不需要一直重覆張貼~