title: Protecting Images using PHP and htaccess
body: <p>i found this code after long research on internet for image protection with PHP. the demo was working perfectly when come to do in my localhost a facing the problem. i test the code with some php code checkers then i found the error: syntax error, unexpected '&lt;', expecting end of file.  </p>
  
  <p>can any one please help on this code with the error: syntax error, unexpected '&lt;', expecting end of file for below code:</p>
  
  <p>image.php</p>
  
  <pre><code>&lt;br&gt;
  &lt;?php&lt;br&gt;
  if (!isset($_GET['onlyHappensFromHTACCESS'])) {&lt;br&gt;
     $_GET['f'] = "protectedImages/" . $_GET['f'];&lt;br&gt;
      $type = getFileType($_GET['f']);&lt;br&gt;
      if (acceptableType($type)) {&lt;br&gt;
          if (goodTiming()) {&lt;br&gt;
              header("Content-type: $type");&lt;p&gt;&lt;/p&gt;
  &lt;p&gt;         echo file_get_contents($_GET['f']);&lt;br&gt;
              exit;&lt;br&gt;
          }&lt;br&gt;
      }&lt;br&gt;
      header('HTTP/1.1 403 Forbidden');&lt;br&gt;
      exit;&lt;br&gt;
  }&lt;/p&gt;
  &lt;p&gt;function getFileType($file) {&lt;br&gt;
    if (function_exists("mime_content_type"))&lt;br&gt;
      return mime_content_type($file);&lt;br&gt;
    else if (function_exists("finfo_open")) {&lt;br&gt;
      $finfo = finfo_open(FILEINFO_MIME_TYPE);&lt;br&gt;
      $type = finfo_file($finfo, $file);&lt;br&gt;
      finfo_close($finfo);&lt;br&gt;
      return $type;&lt;br&gt;
    }&lt;br&gt;
    else {&lt;br&gt;
      $types = array(&lt;br&gt;
        'jpg' =&gt; 'image/jpeg', 'jpeg' =&gt; 'image/jpeg', 'png' =&gt; 'image/png',&lt;br&gt;
        'gif' =&gt; 'image/gif', 'bmp' =&gt; 'image/bmp'&lt;br&gt;
      );&lt;br&gt;
      $ext = substr($file, strrpos($file, '.') + 1);&lt;br&gt;
      if (key_exists($ext, $types)) return $types[$ext];&lt;br&gt;
      return "unknown";&lt;br&gt;
    }&lt;br&gt;
  }&lt;/p&gt;
  &lt;p&gt;function acceptableType($type) {&lt;br&gt;
      $array = array("image/jpeg", "image/jpg", "image/png", "image/png");&lt;br&gt;
      if (in_array($type, $array))&lt;br&gt;
          return true;&lt;br&gt;
      return false;&lt;br&gt;
  }&lt;/p&gt;
  &lt;p&gt;function goodTiming() {&lt;br&gt;
      $n = time();&lt;br&gt;
      session_start();&lt;br&gt;
      if ($n - $_SESSION['lastcheck'] &gt; 2 )&lt;br&gt;
          return false;&lt;br&gt;
      return true;&lt;br&gt;
  }&lt;/p&gt;
  &lt;p&gt;?&gt;&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt;&lt;br&gt;
  &lt;html&gt;&lt;br&gt;
  &lt;head&gt;&lt;br&gt;
    &lt;meta http-equiv="Content-type" content="text/html; charset=utf-8" /&gt;&lt;br&gt;
    &lt;title&gt;Image Denied&lt;/title&gt;&lt;br&gt;
    &lt;style type="text/css" media="screen"&gt;&lt;br&gt;
      body {&lt;br&gt;
          background-color: #ccc;&lt;br&gt;
          font-family: Helvetica, Arial;&lt;br&gt;
      }&lt;br&gt;
      #wrapper {&lt;br&gt;
          margin: 30px auto;&lt;br&gt;
          background-color: #ffffff;&lt;br&gt;
          -moz-border-radius: 15px;&lt;br&gt;
          -webkit-border-radius: 15px;&lt;br&gt;
          border-radius: 15px;&lt;br&gt;
          width: 800px;&lt;br&gt;
          padding: 20px;&lt;br&gt;
      }&lt;br&gt;
    &lt;/style&gt;&lt;br&gt;
  &lt;/head&gt;&lt;/p&gt;
  &lt;p&gt;&lt;div id="wrapper"&gt;&lt;br&gt;
    &lt;h3&gt;Access Denied!&lt;/h3&gt;&lt;br&gt;
    &lt;p&gt;You have tried to access an image, but due to security reasons, you cannot view the image.&lt;/p&gt;&lt;/p&gt;
  &lt;p&gt;  &lt;p&gt;If you wish to use the image you requested, please contact me.&lt;/p&gt;&lt;br&gt;
  &lt;/div&gt;&lt;br&gt;
  &lt;/html&gt;&lt;br&gt;
  &lt;/p&gt;
  </code></pre>
  
  <p>index.php</p>
  
  <pre><code>&lt;br&gt;
  &lt;?php session_start(); $_SESSION['lastcheck'] = time(); ?&gt;&lt;br&gt;
  &lt;html&gt;&lt;br&gt;
  &lt;head&gt;&lt;br&gt;
      &lt;title&gt;Page Title&lt;/title&gt;&lt;br&gt;
      &lt;style type="text/css"&gt;&lt;br&gt;
          .image {&lt;br&gt;
              overflow: hidden;&lt;br&gt;
              position: relative;&lt;br&gt;
              float: left;&lt;br&gt;
          }&lt;br&gt;
          .image .cover, .image .cover img {&lt;br&gt;
              position: absolute;&lt;br&gt;
              top: 0px;&lt;br&gt;
              left: 0px;&lt;br&gt;
              width: 100%;&lt;br&gt;
              height: 100%;&lt;br&gt;
          }&lt;br&gt;
      &lt;/style&gt;&lt;br&gt;
  &lt;/head&gt;&lt;br&gt;
  &lt;body&gt;&lt;br&gt;
      &lt;div class="image"&gt;&lt;br&gt;
          &lt;img src="image.php?f=image.jpg" alt="Image" /&gt;&lt;br&gt;
          &lt;div class="cover"&gt;&lt;img src="imageCover.gif" alt=""  /&gt;&lt;/div&gt;&lt;br&gt;
      &lt;/div&gt;&lt;br&gt;
  &lt;/body&gt;&lt;br&gt;
  &lt;/html&gt;&lt;br&gt;
  </code></pre>
  
  <p>.htaccess in main folder</p>
  
  <pre><code>&lt;br&gt;
  RewriteEngine on&lt;br&gt;
  RewriteCond %{HTTP_REFERER} ^$&lt;br&gt;
  RewriteCond %{SCRIPT_FILENAME} image\.php&lt;br&gt;
  RewriteRule (.*) image.php?onlyHappensFromHTACCESS=denied [QSA,L]&lt;br&gt;
  </code></pre>
  
  <p>.htaccess in protectedImages folder</p>
  
  <pre><code>&lt;br&gt;
  #Prevent directory listing&lt;br&gt;
  Options -Indexes&lt;p&gt;&lt;/p&gt;
  &lt;p&gt;#Prevent images from being viewed&lt;br&gt;
  &lt;Files *&gt;&lt;br&gt;
    deny from all&lt;br&gt;
  &lt;/Files&gt;&lt;br&gt;
  &lt;/p&gt;
  </code></pre>