Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your current solution doesn't work because <code>mod_rewrite</code> can only be used to rewrite the request to a single destination, but you seem to want the request to take a detour to your PHP script, then continue onward to the image. It might be possible to cause a subrequest that would cause the PHP script to get triggered, but I don't think it would be possible to control whether or not the original request continued on to the image in that scenario.</p> <p>The best course of action here is to have your PHP file print out the actual image data (not an image tag referencing the image) after it does whatever checking/logging you intend it to do. You can do this with <a href="http://www.php.net/manual/en/function.readfile.php" rel="nofollow noreferrer"><code>readfile()</code></a>, provided that you send the right headers. After making sure the file is one of the images you want to serve up (and not some arbitrary file on your system...), you'll at least need to <a href="https://stackoverflow.com/questions/2455476/determining-a-local-files-mime-type-content-type-with-php">determine the appropriate content type</a>, then print out the data. It's also a good idea to take caching (see <a href="https://stackoverflow.com/questions/909672/file-get-contents-or-readfile-for-displaying-filesystyem-image/916251#916251">this answer</a>, as well as <a href="https://stackoverflow.com/questions/947638/why-do-images-served-from-my-web-server-not-cache-on-the-client/947690#947690">this one</a>) into consideration.</p> <p>Combining some of the techniques mentioned, a simple pseudo-example of the referrer script would be as follows. Note that you should research the best way to implement the techniques described, and you need to pay particular attention to security since you're opening files and printing their contents.</p> <pre><code>$filename = /* sanitized file name */; log_img(/* log some data about the request */); if (file_exists($filename) &amp;&amp; allowedToView($filename)) { // Assume we're not on PHP 5.3... $types = array( 'gif' =&gt; 'image/gif', 'png' =&gt; 'image/png', 'jpg' =&gt; 'image/jpg', ); $parts = pathinfo($filename); $ext = strtolower($parts['extension']); if (array_key_exists($ext, $types)) { $mime = $types[$ext]; $size = filesize($filename); $expires = 60 * 60 * 24 * 30; if (!empty($_SERVER['IF-MODIFIED-SINCE'])) { $modified = filemtime($filename); $cached = strtotime($_SERVER['IF-MODIFIED-SINCE']); if ($modified &lt;= $cached) { header('HTTP/1.1 304 Not Modified'); exit(); } } header("Content-Type: $mime"); header("Content-Length: $size"); header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT'); header('Cache-control: private, max-age=' . $expires); readfile($filename); exit(); } } header("HTTP/1.0 404 Not Found"); exit(); </code></pre> <p>And as far as the .htaccess file goes, it would just be something like this (the stuff that you added to the query string is available in <code>$_SERVER</code> anyway, so I see no point in manually passing it to the script):</p> <pre><code>RewriteEngine on RewriteRule \.(jpg|png|gif)$ /include/referrer.php [NC] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload