php字元轉義函數參考

來源:互聯網
上載者:User
  1. if(phpversion() < '5.3.0') {
  2. set_magic_quotes_runtime(0);
  3. }
複製代碼

>> 無法通過函數來定義magic_quotes_gpc,因此建議在伺服器上統一開啟,寫程式的時候應該在來判斷下,避免沒開啟gpc引起安全問題 通過addslashes對gpc進行時候轉義時,應注意當使用者提交數組資料時對索引值和值的過濾

  1. if(!get_magic_quotes_gpc()) {

  2. $_get = daddslashes($_get);
  3. $_post = daddslashes($_post);
  4. $_cookie = daddslashes($_cookie);
  5. $_files = daddslashes($_files);
  6. }
  7. function daddslashes($string, $force = 1) {
  8. if(is_array($string)) {
  9. foreach($string as $key => $val) {
  10. unset($string[$key]);
  11. $string[addslashes($key)] = daddslashes($val, $force);
  12. }
  13. } else {
  14. $string = addslashes($string);
  15. }
  16. return $string;
  17. }

  18. ?>>

複製代碼

利用在使用者輸入或輸出時候轉義html實體以防止xss漏洞的產生!

今天碰到一個處理檔案特殊字元的事情,再次注意到這個問題,在php中:

  1. $str = "ffff\0ffff";
  2. echo(strlen($str));
  3. echo("\n");
  4. for($i=0;$iecho("\n");
複製代碼

輸出結果: ---------------------- 9 102 102 102 102 0 102 102 102 102

替換特殊字元的例子

  1. $str = "ffff\0ffff";
  2. $str = str_replace("\x0", "", $str);
  3. //或者用$str = str_replace("\0", "", $str);
  4. //或者用$str = str_replace(chr(0), "", $str);
  5. echo(strlen($str));
  6. echo("\n");
  7. for($i=0;$iecho("\n");
複製代碼

輸出結果: ---------------------- 8 102 102 102 102 102 102 102 102

八進位ascii碼例子:

  1. //注意,符合正則\[0-7]{1,3}的字串,表示一個八進位的ascii碼。
  2. $str = "\0\01\02\3\7\10\011\08\8"; //這裡的\8不符合要求,被修正為"\\8" (ascii為92和56)
  3. echo(strlen($str));
  4. echo("\n");
  5. for($i=0;$iecho("\n");
複製代碼

輸出結果: ---------------------- 11 0 1 2 3 7 8 9 0 56 92 56

十六進位ascii碼例子:

  1. $str = "\x0\x1\x2\x3\x7\x8\x9\x10\x11\xff";
  2. echo(strlen($str));
  3. echo("\n");
  4. for($i=0;$iecho("\n");
複製代碼

輸出結果: ---------------------- 10 0 1 2 3 7 8 9 16 17 255 php特殊字元轉義詳解phpRegex逸出字元的例子PHP常用逸出字元函數PHP轉義Regex字元的函數php實現防注入與表單提交值轉義的代碼mysql語句中字元轉義的方法舉例php字元轉義的相關注意事項

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.