• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Restrict direct image viewing

I'm trying to do something similar (the same?) here.

I want images to display when they're part of one of my pages, but not if accessed directly by URL.

For instance: index.php has an image on it, with src = "./images/foo.jpg". I want it to show for everyone on index.php. But if they try to go to the URL ./images/foo.jpg directly it shouldn't deliver them anything.

The standard way to approach this is to deliver the image by a function. Thus, the image src = "image.php?=foo", or in CI something like "image/foo". The image is now delivered through a controller. And the function, whatever it is, only delivers the image if it's being accessed through one of the site's normal pages.

But the problem is the following. In normal PHP you can just hard-code the image PHP file to do what you want. If you want to use a CI controller to do the trick, you can't. Because: every controller is run through the core CI files (index, etc.) - in other words, your image-delivery function is automatically a normal page of your site - there's no way to distinguish it from any other page. Any way of disabling access to it will disable the images in your normal pages too.

The only real way I can see to get around this is to, manually in each and every "normal" page, set a session variable. If that variable is present, images are shown. Eventually it will run out and images won't be shown by directly accessing the image controller anymore. But this is very clumsy. Anyone got a better way?

[eluser]Chris Williams[/eluser]
When I blocked access via htaccess, I was able to call those images using the "readfile" function.

Thing is, that's not really a PHP solution. It's an Apache solution, and I might not use Apache (or have access to make .htaccess files).

Is there a strictly CodeIgniter way of doing this?

[eluser]Rick Jolly[/eluser]
1. Save your images above the webroot.
2. Have an "image" controller that, when called from the view by the browser, uses readfile to return the image.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.