使用PHP和htaccess保护图像

时间:2017-03-22 10:08:41

标签: php apache .htaccess

我在使用PHP进行长期的互联网图像保护研究后发现了这段代码。当我在localhost中遇到问题时,演示工作正常。我用一些PHP代码检查器测试代码然后我发现错误:语法错误,意外'<&#;;期待文件结束。

任何人都可以帮助解决此代码的错误:语法错误,意外'<&#;;期待以下代码的文件结尾:

image.php

<br>
<?php<br>
if (!isset($_GET['onlyHappensFromHTACCESS'])) {<br>
   $_GET['f'] = "protectedImages/" . $_GET['f'];<br>
    $type = getFileType($_GET['f']);<br>
    if (acceptableType($type)) {<br>
        if (goodTiming()) {<br>
            header("Content-type: $type");<p></p>
<p>         echo file_get_contents($_GET['f']);<br>
            exit;<br>
        }<br>
    }<br>
    header('HTTP/1.1 403 Forbidden');<br>
    exit;<br>
}</p>
<p>function getFileType($file) {<br>
  if (function_exists("mime_content_type"))<br>
    return mime_content_type($file);<br>
  else if (function_exists("finfo_open")) {<br>
    $finfo = finfo_open(FILEINFO_MIME_TYPE);<br>
    $type = finfo_file($finfo, $file);<br>
    finfo_close($finfo);<br>
    return $type;<br>
  }<br>
  else {<br>
    $types = array(<br>
      'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png',<br>
      'gif' => 'image/gif', 'bmp' => 'image/bmp'<br>
    );<br>
    $ext = substr($file, strrpos($file, '.') + 1);<br>
    if (key_exists($ext, $types)) return $types[$ext];<br>
    return "unknown";<br>
  }<br>
}</p>
<p>function acceptableType($type) {<br>
    $array = array("image/jpeg", "image/jpg", "image/png", "image/png");<br>
    if (in_array($type, $array))<br>
        return true;<br>
    return false;<br>
}</p>
<p>function goodTiming() {<br>
    $n = time();<br>
    session_start();<br>
    if ($n - $_SESSION['lastcheck'] > 2 )<br>
        return false;<br>
    return true;<br>
}</p>
<p>?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><br>
<html><br>
<head><br>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" /><br>
  <title>Image Denied</title><br>
  <style type="text/css" media="screen"><br>
    body {<br>
        background-color: #ccc;<br>
        font-family: Helvetica, Arial;<br>
    }<br>
    #wrapper {<br>
        margin: 30px auto;<br>
        background-color: #ffffff;<br>
        -moz-border-radius: 15px;<br>
        -webkit-border-radius: 15px;<br>
        border-radius: 15px;<br>
        width: 800px;<br>
        padding: 20px;<br>
    }<br>
  </style><br>
</head></p>
<p><div id="wrapper"><br>
  <h3>Access Denied!</h3><br>
  <p>You have tried to access an image, but due to security reasons, you cannot view the image.</p></p>
<p>  <p>If you wish to use the image you requested, please contact me.</p><br>
</div><br>
</html><br>
</p>

的index.php

<br>
<?php session_start(); $_SESSION['lastcheck'] = time(); ?><br>
<html><br>
<head><br>
    <title>Page Title</title><br>
    <style type="text/css"><br>
        .image {<br>
            overflow: hidden;<br>
            position: relative;<br>
            float: left;<br>
        }<br>
        .image .cover, .image .cover img {<br>
            position: absolute;<br>
            top: 0px;<br>
            left: 0px;<br>
            width: 100%;<br>
            height: 100%;<br>
        }<br>
    </style><br>
</head><br>
<body><br>
    <div class="image"><br>
        <img src="image.php?f=image.jpg" alt="Image" /><br>
        <div class="cover"><img src="imageCover.gif" alt=""  /></div><br>
    </div><br>
</body><br>
</html><br>
主文件夹中的

.htaccess

<br>
RewriteEngine on<br>
RewriteCond %{HTTP_REFERER} ^$<br>
RewriteCond %{SCRIPT_FILENAME} image\.php<br>
RewriteRule (.*) image.php?onlyHappensFromHTACCESS=denied [QSA,L]<br>

。protectedcat文件夹中的.htaccess

<br>
#Prevent directory listing<br>
Options -Indexes<p></p>
<p>#Prevent images from being viewed<br>
<Files *><br>
  deny from all<br>
</Files><br>
</p>

0 个答案:

没有答案