Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP和htaccess保护图像_Php_Apache_.htaccess - Fatal编程技术网

使用PHP和htaccess保护图像

使用PHP和htaccess保护图像,php,apache,.htaccess,Php,Apache,.htaccess,我在互联网上用PHP进行长期的图像保护研究后发现了这段代码。当我在本地主机a中遇到这个问题时,演示运行得非常好。我用一些php代码检查器测试了代码,然后发现了错误:语法错误,意外的“为什么htaccess文件中有标记和其他HTML标记?”?另外,为什么CSS中有标记?PHP代码是否真的包含所有HTML标记,比如行中的等?如果是这样,先把那些东西扔掉。 <br> <?php<br> if (!isset($_GET['onlyHappensFromHTACCESS']

我在互联网上用PHP进行长期的图像保护研究后发现了这段代码。当我在本地主机a中遇到这个问题时,演示运行得非常好。我用一些php代码检查器测试了代码,然后发现了错误:语法错误,意外的“为什么htaccess文件中有

标记和其他HTML标记?”?另外,为什么CSS中有

标记?PHP代码是否真的包含所有HTML标记,比如行中的

等?如果是这样,先把那些东西扔掉。
<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>
<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>
<br>
RewriteEngine on<br>
RewriteCond %{HTTP_REFERER} ^$<br>
RewriteCond %{SCRIPT_FILENAME} image\.php<br>
RewriteRule (.*) image.php?onlyHappensFromHTACCESS=denied [QSA,L]<br>
<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>