dongtaidai0492 2016-05-03 17:21
浏览 276
已采纳

在img src或链接中隐藏/转换敏感信息

I am working with an XML API provider. They are providing me with certain image sources.

<img src="http://api-provider.com/image/?key=YOUR_KEY&id=ID"/>

Now to get any image I have to provide the key I am using for the API. So someone could simply look at the page source and get the key.

Is there anyway to bypass this and hide the key somehow?

  • 写回答

1条回答 默认 最新

  • dtd58256 2016-05-03 17:39
    关注

    You can make a php file to load the image and put the API Key in your php file so that API Key will not be readable by others like this:

    $API_Key = "Your_API_Key_Here";
    $url = "http://api-provider.com/image/?key={$API_Key}&id={$_GET['id']}";
    header('Content-type: image/jpeg');
    readfile($url);
    

    Then in your Image src tag put your own link: http://yourdomain.com/get_image.php?id={$Image_Id}

    But it will use your server resources as the Image would be read from your server. Also if someone gets this URL he can use this without API Key so it will not be different than showing the API Key in URL at the first place, maybe you can verify the Session and then display the image.


    As pointed by @miken32 to ensure that user is not trying to use your server directly, you can multiply with a random number and then do base64_encode to the id. Like this:

    $Image_Src = "http://yourdomain.com/get_image.php?id=".base64_encode($Image_Id*18);
    

    and in your PHP file before reading the image first verify that the id is a valid number by reversing the encoding you did above.

    $Image_Id = $_GET['id'];
    $Image_Id = base64_decode($Image_Id);
    if (is_numeric($Image_Id)){
       $Correct_Image_Id = $Image_Id / 18;
       if ($Correct_Image_Id > 0){
          //read image here
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch