David Walsh的文章:Tips for Protecting Querystring Keys & Values in PHP,建議了以下五種保護Querystring參數和值的方法:

  1. 整數需指定資料型態(Typecast Value when Expecting Numbers):確保整數不會被當成字串而造成錯誤。
  2. $id = (int) $_GET['i'];
  3. 淨化輸入值(Sanitize Input):建立各種類型資料的處理函式,確保資料傳遞時的一致性與安全性。類似在Dreamweaver裡面的GetSQLValueString函式。
  4. 關閉REGISTER_GLOBALS(Make Sure REGISTER_GLOBALS is Off)。
  5. 不使用有意義的變數名稱(Don’t Make Variable Names Meaningful):David原本的建議如下,但是有一位仁兄在回應中則持反對的意見,認為這並無助於安全性(還是可以從數值的一致性猜出變數的意義)。
  6. <!-- no! -->
    <a href="/profile.php?user_id=<?php echo $user_id; ?>">Your Profile</a>
    <!-- yes! -->
    <a href="/profile.php?q=<?php echo $user_id; ?>">Your Profile</a>
    
  7. 將數值加密(Encrypt Querystring Values):使用md5來加密參數值。
  8. <a href="/profile.php?i=<?php echo md5($user_id); ?>">Click here</a>

最後,請問有人知道「Querystring」的中文怎麼翻譯嗎?

補記:jaceju我的部落格建議Querystring可譯為「網址參數列 (字串) 」,裡面的變數就稱為「網址參數」 (例如 ?abc=123 裡的 abc)

Posted by yoren on 2008年 05月 28日