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 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)