2016-03-09 98 views
0

我需要連接到特定公司的API,他們提供了一個簡單的API,您可以通過簡單的URL獲取數據。 例如:https://example.com?user=xxx&pass=xxx&data=specificdata有沒有什麼辦法可以防止url注入?

我的問題是:

我有獲取參數,並連接到API函數:

public function urlRequest($base_url, $company_id, $user_name, $password, $parameters) { 
    if (isset($base_url) && isset($company_id) && isset($user_name) && isset($password) && count($parameters) > 0) { 
     $url = "https://$base_url?compID=$company_id&user=$user_name&pass=$password&$parameters"; 
     $response = file_get_contents($url); 
     $response = json_decode($response, true); 
     return $response; 
    } 
} 

1.Is有什麼辦法,我需要從URL注入保護和如果是這樣,哪些功能可以與它相關? (htmlentities等...)

2.哪些字符不能在url中,我需要防範,這是類似於SQL注入?

+1

它不是你的API,你不需要對傳遞給它的變量做任何事情。 –

回答

0

適合保護網址,如果有cursiosas人和更改網址的ID,他們可能會看到來自其他用戶的數據。

它可以保護它與md5函數PHP,我留下一個小例子。

<?php 

//Put it on a directoria safty and include it to get value. 
$clave = "4V}ee[TL&]=cA;kv-SYVg&vVKz!teJ_PW93G*u>F6z[R)]-TMeW.{~Q&Ee([email protected]]&=_sc1(jkc7p~b-d)|tNV^qR}Q^>@2"; 

//Set safety, for URL. 
$company_id = $ConDB->real_escape_string($row['company_id']);    

//Protect 'URL' for our page. 
$protectid = md5($clave.$company_id); 

//Your function. 
public function urlRequest($base_url, $company_id, $user_name, $password, $parameters) { 
    if (isset($base_url) && isset($company_id) && isset($user_name) && isset($password) && count($parameters) > 0) { 
     $url = "https://$base_url?compID=$protectid&user=$user_name&pass=$password&$parameters"; 
     $response = file_get_contents($url); 
     $response = json_decode($response, true); 
     return $response; 
    } 
} 



//To see return the outcome data, let we see how we can bring back our results with the md5 function (page: base_url) 

$id = $_GET['compID'];//GET 'ID' 'URL' with protected. 

//query 
$sql = "SELECT * FROM yourtable WHERE md5(CONCAT('$clave', company_id)) = '$id'"; 
$resultado = $ConDB->query($sql); 



?> 
相關問題