2017-07-10 92 views
-2

我有一個網址,我試圖提取一些參數。 我的網址可以有兩種形式,
無論是 '代碼' 或 '刷新' 但它返回null,甚至想到了參數設置
URL鏈接 - >與 'CODE'http://www.example.com/token.php?client_id=hello&client_number=blahblah&type=authcode&code=hello23423

網址 - > WITH '刷新'http://www.example.com/token.php?client_id=hello&client_number=blahblah&type=authrefresh&refresh=74388bye

$token = "NULL"; 
$client = $_GET['client_id']; 
$secret = $_GET['client_number']; 
$type = $_GET['type']; 

if (isset($_POST["code"])) { 
    $token = $_GET['code']; 
} 

if (isset($_POST["refresh"])) { 
    $token = $_GET['refresh']; 
} 

echo $client; // $client, $secret and $type is printed without any issues 
echo $secret; 
echo $type; 
echo $token; <---- Always returns NULL even though the URL has the parameters CODE or REFRESH 
+6

'isset($ _ POST [ 「代碼」])'?爲什麼'後'?爲什麼? –

回答

6

你混合$_GET$_POST(這些是不可互換)。

當你傳遞參數的查詢字符串,你將永遠使用$_GET

if (isset($_GET["code"])) { 
    $token = $_GET['code']; 
} 

if (isset($_GET["refresh"])) { 
    $token = $_GET['refresh']; 
} 

希望這有助於!

+0

我感覺很傻。感謝您指出:) –

0

您可以嘗試使用$_REQUEST替代$_GET$_POST

http://php.net/manual/it/reserved.variables.request.php

的關聯數組默認包含$ _GET內容,$ _ POST E $ _COOKIE。

+1

這將意味着,問題中的代碼將工作,但是,如果OP只使用查詢字符串來傳遞數據,那麼實際上沒有任何意義。如果有明確的解決方案,這看起來更像是一種解決方法。 –

+0

同意!只是想分享這種可能性,以避免混淆使用一種方法來檢索收到的所有參數。最好只是將它添加爲對問題的評論。對不起,:) –

+0

在安全方面使用請求是非常冒險的 – Akintunde007

0

你有當您通過查詢字符串(以URL)傳遞參數(當你設置形式GET方法也可以),以瞭解何時用戶$_GET$_POST使用

$_GET

$_POST從發送信息使用POST方法的一種形式

在您的情況下您使用查詢字符串傳遞數據,因此您必須檢查數據是否設置爲$_GET而不是$_POST

if (isset($_GET["code"])) { 
    $token = $_GET['code']; 
} 

if (isset($_GET["refresh"])) { 
    $token = $_GET['refresh']; 
} 

如果你仍然混淆使用$_REQUEST它支持查詢字符串(GET)均通過參數和POST也

if (isset($_REQUEST["code"])) { 
     $token = $_REQUEST['code']; 
    } 

    if (isset($_REQUEST["refresh"])) { 
     $token = $_REQUEST['refresh']; 
    }