在PHP中现防盗链

  参考了一些方法,发现防盗思路很简单,可以用一个php脚本fileshow.php?id=**去取图片或文件,通过传递过来的参数寻找文件的路径,在代码里隐藏文件的真实地址.具体做法是判断链接过来的地址是否是本站地址,是就输出请求图片, 否则就为其他网站非法盗链,可以输出一个盗链图片.

  实例代码为下:
程序代码 程序代码
<?
  
        //存放图片的文件夹在服务器上的绝对地址
        $imgRootPath = "pics/2005/11/";
        //例外允许连接的网址,注意:自身域名不需要填入,设定为肯定可以连接图片
        $excludeReferArr = array("www.163.com", "163.com");

        doJudgeReferer($excludeReferArr);//判断是否

        $imgRelPath=$_REQUEST['id'];//src参数

        if(empty($imgRelPath)){
            doOutputMsgImg("未指定要查看的图片!");
            exit;
        }


        $srcSplitArr = explode(".", $imgRelPath);//获得图片后缀名
        $srcSuffix = $srcSplitArr[count($srcSplitArr)-1];
        $srcSuffix = strtolower($srcSuffix);

        $imgAbsPath = "$imgRootPath$imgRelPath";  //更换复杂地址

    
        if(!file_exists($imgAbsPath))
        {
            doOutputMsgImg("对不起,此图片链接已经失效!");
        }
                                    
        else if($srcSuffix == "gif") {  
            header ("Content-type: image/gif");
            $image = imagecreatefromgif ($imgAbsPath);
            imagegif ($image);
            imagedestroy ($image);
            
        }
        else if($srcSuffix == "jpg") {
            header ("Content-type: image/jpeg");
            $image = imagecreatefromjpeg ($imgAbsPath);
            imagejpeg ($image);
            imagedestroy ($image);
        }
        else if($srcSuffix == "png") {
            header ("Content-type: image/png");

            $image = imagecreatefrompng ($imgAbsPath);
            imagepng ($image);
            imagedestroy ($image);
        }
        else {
            doOutputMsgImg("图像类型不支持");
        }
    

   //**************
   //相关函数
   //*************

    function doJudgeReferer($excludeReferArr) {
        $referUrl=parse_url($_SERVER["HTTP_REFERER"]);
        $referHost = $referUrl[host];
        if($referHost!="" && $referHost!=$_SERVER["HTTP_HOST"] && !in_array($referHost, $excludeReferArr))
        {
            doOutputMsgImg("这是来自".$referUrl[host]."的盗链!");
            exit;
        }
    }

    function doMarkImage($inImg,$inMarkStr="Powered by Wreny.com") {
        $black = imagecolorallocate ($inImg, 0, 0, 0);

        $imgWidth = imagesx($inImg);
        $imgHeight = imagesy($inImg);
        //289-108,86
        drawTxt($inImg,$inMarkStr, ($imgWidth-strlen($inMarkStr)*9),($imgHeight-16), $black);
    }

    function doOutputMsgImg($msg, $imgWidth=468,
        $imgHeight=60, $imgFgColorArr=array(0,0,0), $imgBgColorArr=array(255,255,255)) {
        $img = imagecreatetruecolor($imgWidth, $imgHeight);

        // 用白色背景加黑色边框画个方框
        $backColor = imagecolorallocate($img, 255, 255, 255);
        $borderColor = imagecolorallocate($img, 0, 0, 0);
        imagefilledrectangle($img, 0, 0, $imgWidth - 1, $imgHeight - 1, $backColor);
        imagerectangle($img, 0, 0, $imgWidth - 1, $imgHeight - 1, $borderColor);

        $imgFgColor = imagecolorallocate ($img, $imgFgColorArr[0], $imgFgColorArr[1], $imgFgColorArr[2]);
        drawTxt($img, $msg, ($imgWidth-strlen($msg)*9)/2,($imgHeight/2-16),$imgFgColor);
        doMarkImage($img);

        header('Content-type: image/png');
        imagepng($img);
        imagedestroy($img);
    }

    function isCharVilid($inStr, $inPos) {
        if(strlen($inStr) < ($inPos+1)) {
            return true;
        }
        else {

            for($iLoop=0,$iCounter=0;$iLoop<=$inPos; $iLoop++){
                if(substr($inStr, $iLoop, 1)<='~') {
                    $iCounter++;
                }
            }

            return ( ($iCounter % 2) == 0 );
        }
    }

    function drawTxt($image, $string, $x, $y, $color) {
        $fp = fopen("chinese.fon", "r"); //WIN98中,此文件在:c:\windows\command 下
        if (feof($fp)) {
            fclose($fp);
            return 0;
        }

        //GBK
        $strings = preg_split(
            '/((?:[\\x80-\\xFF][\\x40-\\xFF])+)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE
        );

        //print_r($strings);

        $isch = false;

        for ($p = 0, $count = count($strings); $p < $count; $p ++) {
            if ($isch) {
                $string = $strings[$p];
                for ($i = 0, $l = strlen($string) - 1; $i < $l; $i += 2) {
                    $qh = ord($string{$i}); // get ascii code
                    $offset = (94 * ($qh - 0xA0 - 1) + (ord($string{$i + 1}) - 0xA0 - 1)) * 32;

                    fseek($fp, $offset, SEEK_SET);
                    $buffer = unpack('n*', fread($fp, 32));
                    //        $buffers[$offset] = $buffer;
                    for ($yy = 1, $ypos = $y; $yy <= 16; $yy ++, $ypos ++) {
                        $bits = $buffer[$yy];
                        for ($xbit = 32768, $xpos = $x; $xbit > 0; $xbit >>= 1, $xpos ++)  {
                            if ($bits & $xbit) {
                                imagesetpixel($image, $xpos, $ypos, $color);
                            }
                        }
                    }
                    $x += 16;
                }
            }
            else {
                imagestring($image, 12, $x, $y, $strings[$p], $color);
                $x += strlen($strings[$p]) * 9;
            }
            $isch = !$isch;
        }

        return 0;
    }
?>



文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
248
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.