我需要连接到特定公司的API,它们提供了一个简单的api,您可以通过简单的URL获取数据。 例如:https://example.com?user=xxx&pass=xxx&data=specificdata
我的问题是:
我有一个获取参数并连接到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.我有什么方法可以防止网址注入,如果是这样,哪些功能可以与它相关? (htmlentities等...)
2.哪些字符不能在url中我需要保护,这类似于sql注入吗?
答案 0 :(得分:0)
适合保护网址,如果有人名并更改了网址的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(qyag?_KC@]&=_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);
?>