0

May be I'm asking a wrong question, I just don't know how to define it more specific, sorry about that.

I want to create links (src in img tag) that looks like that : "bla-bla.com/7e0cfd63-dfc1-4038-a859-ff201dea7b65" and be able to "decode" it back.

Reason why>

My links (src in img tag) are looking like that now "myDomain/somefolder/image1.jpg" , so if user will change "image1.jpg" to "image2.jpg" he will get a "direct access" (I think it's called like that).

So I want to prevent "direct access", but still be able to share links.

May be I'm over-complicating the issue and there is another - easy way to do it, or some php extension for this job, please let me know.

Server: win 2012 r2 Apache + php5 + MySQL

P.S.

"7e0cfd63-dfc1-4038-a859-ff201dea7b65" - this is a UUID, right? I saw it in youtube video links.

Google search gave a lot of results for UUID/GUID scripts/technics/how it works and so on, but I was not able to find an implementation for links...

P.P.S

if you go to my profile image here on stackoverflow:

https://www.gravatar.com/avatar/4141ef5b3f1196534e59f42f76fe0d0f?s=48&d=identicon&r=PG&f=1

and change f to d you will get a different image

https://www.gravatar.com/avatar/4141ef5b3f1196534e59f42f76fe0d0d?s=48&d=identicon&r=PG&f=1

This is what i don't want to happen.

It_Never_Works
  • 213
  • 2
  • 10

1 Answers1

1

I would suggest using:

  • mod_rewrite
  • a decoding php script and
  • your database to map UUIDs to paths

mod_rewrite

You use this to redirect all URLs that match a fake folder to the decoding script. Naming the fake folder imgs, apache configuration would look like (not tested):

RewriteEngine on 
RewriteRule ^/img/(.*)$ get_image.php?uuid=$1 [L]

This should match a URL like /img/7e0cfd63-dfc1-4038-a859-ff201dea7b65 and redirect to /get_image.php?uuid=7e0cfd63-dfc1-4038-a859-ff201dea7b65. (Your images now source is: src="/img/7e0cfd63-dfc1-4038-a859-ff201dea7b65")

get_image.php

This script will use the database to figure out the real location of the image which might as well be outside of the web-root of your application! Apache, however, needs to have access to the file (based on filesystem permissions). For this example lets assume all your images are into /data/mydomain/images. Your script might look like:

$image_id = $_GET['uuid'];
// ...
$sql = "SELECT * FROM images WHERE uuid='$image_id' LIMIT 1";
$result = mysql_query($sql);
$r = mysql_fetch_array($result);

// DB has the RELATIVE path
$imagepath = "/data/mydomain/images" . $r['path'];

// Produce proper Image
header("Content-Type: image/jpeg");
header("Content-Length: " . filesize($imagepath));

echo file_get_contents("$imagepath");

Note, that more code will be needed if you want to support more image/mime types.

Database

Finally, in your database you need at least two fields in the images table:

  • uuid: The image's UUID. Make that the primary key or if you prefer to have auto_increment integer key, index this column (for performance)
  • rpath: The relative path to the image in the storage location (for example: /somefolder/image1.jpg)

That should be all :). I have not tested any of the above but when put all together should achieve what you want

urban
  • 5,392
  • 3
  • 19
  • 45
  • For each image/file on server I calculate SHA1, can I use it instead of UUID? or it is a bad practice for some reason? – It_Never_Works Apr 10 '16 at 09:37
  • In theory you can use anything you like, but SHA1 is not suggested since it is broken! SHA256 should do it – urban Apr 10 '16 at 09:45
  • @It_Never_Works No worries :) – urban Apr 10 '16 at 09:49
  • First of all this method works fine, does exactly what I want. In addition it solved my another concern - no need to add alias to directory with images, so they are ONLY accessibly from outside through the script. **The only disadvantage is - no caching. Is it possible to add caching somehow?** – It_Never_Works Apr 20 '16 at 07:30
  • @It_Never_Works: Not 100% sure, but wouldn't cache work if you set the correct headers? Example [here](http://stackoverflow.com/a/7324564/3727050) – urban Apr 20 '16 at 07:36
  • You know what? .. You are absolutely right, if headers set correctly - cache works !!! – It_Never_Works Apr 21 '16 at 08:33