• php 防盗链和盗取图片


    点我看图

     image.html

    <!DOCTYPE html>
    <html>
    <head>
    	<title>防盗链</title>
    	<meta charset="utf-8">
    </head>
    <body>
            <img src="http://127.0.0.1/fangdaolian/image.php?img_id=1" alt="" width="250" height="350" />
            <a href="http://127.0.0.1/fangdaolian/image.php?img_id=1" target="_blank">点我看图</a>
    </body>
    </html>
    

      

    image.php文件

    <?php
    class Imgdata{
       public $img_id;
    
       public function __construct($id){
           header('Content-Type: image/jpeg');//输出文件图片需要头格式
           $this->img_id = $id;
       }
    
        //输出图片
        public function output_img(){
        //进行防盗链处理
        if(isset($_SERVER['HTTP_REFERER'])){
        $parse_url = parse_url($_SERVER['HTTP_REFERER']);//对来源网址进行解析
        $allow_host_arr = [//运行访问的主机数组,实际项目开发时,可以通过配置文件进行配置
        '127.0.0.1',
        'yangxiongwei.xyz',
        '192.168.2.54'
        ];
        if(in_array($parse_url['host'], $allow_host_arr)){
        echo file_get_contents($this->img_id.".jpg");
        }else{
        echo file_get_contents('2.jpg');//如果是防盗链,则已警告图片进行展现
        }
        }else{
        echo file_get_contents('3.jpg');//如果是防盗链,则已警告图片进行展现
        }
        }
    }
    $img_id = $_GET['img_id'];//获取图片id
    $img = new Imgdata($img_id);//图片路径,一般存储在数据库里,用户无法获取真实路径,可根据图片ID来获取
    $img->output_img();//输出图片

     还有一种思路

    在静态变量中设置非本域名ip不可看图片

  • 相关阅读:
    URL参数加密专用
    错误
    js学习类
    .net第一个服务器控件
    javascript中的call()和apply()方法 原创实例
    FIS使用技巧
    自定义参数表单URL参数处理
    避免编写解决"不存在"问题的代码
    从 1.1.0 升级到 1.2.0 的注意事项
    jquery常用插件,应用解析
  • 原文地址:https://www.cnblogs.com/kevin-yang123/p/14177209.html
Copyright © 2020-2023  润新知