顯示具有 PHP 標籤的文章。 顯示所有文章
顯示具有 PHP 標籤的文章。 顯示所有文章

2017年5月11日 星期四

【PHP】X-Frame-Options 回應標頭

這幾天使用 Checkmarx 做白箱弱掃時,Javascript 的部份一直出現一個中度風險的項目:

【Client Cross Frame Scripting Attack】

Google 了一下似乎在 PHP 內的 header 加上 X-Frame-Options 相關設定就能解決此問題

在 PHP 頁面中各別設定:

完全禁止任何 iframe 請求
```php header("X-Frame-Options: DENY") ``` 只允許同 Domain 來源的請求
```php header("X-Frame-Options: SAMEORIGIN") ``` 只允許某網址的請求
```php header("X-Frame-Options: ALLOW-FROM http://www.google.com") ```

在 Apache 中設定:

```php Header always append X-Frame-Options DENY ```
雖然此方應該能有效避免【Client Cross Frame Scripting Attack】這類攻擊,
但在 Checkmarx 的白箱弱掃時,似乎還是會誤判為無效,待持續觀察 ...


2017年4月6日 星期四

【Python】Codility in Python : Lesson 13 - Fibonacci Numbers【Ladder】

markdown Fibonacci Numbers 第一題:【Ladder】
Count the number of different ways of climbing to the top of a ladder.

You have to climb up a ladder. The ladder has exactly N rungs, numbered from 1 to N.
With each step, you can ascend by one or two rungs. More precisely:

with your first step you can stand on rung 1 or 2,
if you are on rung K, you can move to rungs K + 1 or K + 2,
finally you have to stand on rung N.
Your task is to count the number of different ways of climbing to the top of the ladder.

For example, given N = 4, you have five different ways of climbing, ascending by:

  1, 1, 1 and 1 rung,
  1, 1 and 2 rungs,
  1, 2 and 1 rung,
  2, 1 and 1 rungs, and
  2 and 2 rungs.

Given N = 5, you have eight different ways of climbing, ascending by:

  1, 1, 1, 1 and 1 rung,
  1, 1, 1 and 2 rungs,
  1, 1, 2 and 1 rung,
  1, 2, 1 and 1 rung,
  1, 2 and 2 rungs,
  2, 1, 1 and 1 rungs,
  2, 1 and 2 rungs, and
  2, 2 and 1 rung.

The number of different ways can be very large,
so it is sufficient to return the result modulo 2P, for a given integer P.

Write a function:

  def solution(A, B)

that, given two non-empty zero-indexed arrays A and B of L integers,
returns an array consisting of L integers specifying the consecutive answers;
position I should contain the number of different ways of climbing the ladder with A[I] rungs modulo 2B[I].

For example, given L = 5 and:

  A[0] = 4 B[0] = 3
  A[1] = 4 B[1] = 2
  A[2] = 5 B[2] = 4
  A[3] = 5 B[3] = 3
  A[4] = 1 B[4] = 1

the function should return the sequence [5, 1, 8, 0, 1], as explained above.

Assume that:
  L is an integer within the range [1..30,000];
  each element of array A is an integer within the range [1..L];
  each element of array B is an integer within the range [1..30].


方法一:參考 Code Says 的做法
Correctness:100%、Performance:100%
```python def solution(A, B): limit = len(A) result = [0] * len(A) B = [(1 << item) - 1 for item in B] # 取得費氏數列 fib = [0] * (limit + 2) fib[1] = 1 for i in range(2, limit + 2): fib[i] = fib[i - 1] + fib[i - 2] for i in range(limit): result[i] = fib[A[i] + 1] & B[i] return result ```

完整練習題 source code 請參閱:github

2017年4月5日 星期三

【PHP】解決 SQLSTATE[HY000]: General error: 2031 No data supplied for parameters in prepared statement.

記錄本日工作時遇到的詭異問題,PHP 在撈 DB 資料時出現了下列錯誤訊息:
SQLSTATE[HY000]: General error: 2031 No data supplied for parameters in prepared statement.

原因是如果參數有兩個名稱重複時,就會造成這個錯誤
```sql select * from student where sid = :id or tid = :id ```

將參數改為不同名稱就能解決這個問題
```sql select * from student where sid = :sid or tid = :tid ```

2017年2月13日 星期一

【UTF-8】解決 PHP 實作 HTML to Excel,在 ie11 檔名出現亂碼的問題

一般來說,在 PHP 中要把 HTML 中的 TABLE 轉為 EXCEL,
只要加上下列 header 描述,就能自訂匯出的檔案名稱
```php <?php header("Content-Disposition: attachment; filename=" . $fileName . ".xls;); ?> ```

但偏偏使用 ie11 時,就是會莫名其妙產生中文檔名變成亂碼的問題,
此時必須再加上下列設定,才能將亂碼恢復成正常的顯示
```php <?php header("Content-Disposition: attachment; filename=" . $fileName . ".xls; filename*=UTF-8''" . urlencode($fileName) . ".xls"); ?> ```