1. 程式人生 > >PHP 無法取得 Hppt Request $_GET Params 值的解決方式 « 關於網路那些事...

PHP 無法取得 Hppt Request $_GET Params 值的解決方式 « 關於網路那些事...

這裡記錄一下,在 Nginx + PHP 環境無法取得 Http Get Request 的問題

在本機端,使用的是 apache 環境,會需要取得網址上的Get引數

<?php

    public function index(Request $request) {
        $message = $request->message;
      ...
    }

上傳至 Server Side (Nginx 環境) 卻發現無法取得引數

嘗試取得完整網址與$_GET是否能取得值

這時先測看看完整網址以及檢查$_GET

<?php

    public
function index(Request $request) { echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; print_r($_GET);

確定在 localhost 確實能完整捕捉到
Localhost 結果如:

http://localhost:8000?message=hello
Array
(
[message] => hello
)

但是在 server 上面有捕捉到完整網址,但同樣輸出的是空的 $_GET
Server 輸出結果如:

http://example.com?message=hello
Array
(
)

檢查Server side 設定

正式 Server 使用的是 Nginx

前往Server 設定中,檢查一下 nginx.conf ,發現原因是

try_files 沒有配置到 get 引數

location / {
    try_files $uri $uri/ /index.php;
}

改成下面這樣就能正常取得 get params

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

或者

location / {
    try_files $uri $uri/ /index.php?$args;
}


如果你喜歡我們的文章內容,請在這裡按個讚