Search code examples
phphttp-authentication

How to get image from HTTP-auth protected area?


I have a web-camera(D-LINK DCS-2130) and a web-site. Camera haven't "guest" option(or I haven't found where is it?), that so I need to authorize my site to show image from cam. What is a correct way to display an image?

I have 2 ideas:

  1. Create getImage.php, which will send login+pass(cURL) to the camera and get the image. In my site's code I'll use src="getImage.php"
  2. Create same getImage.php, which will send login+pass(cURL), get the image, store it localy into /myCameraSnapshots/Image.jpg wait() 1 second, then get new image and overwrite it localy.

The first way will do many requests to the camera (1 viewer — 1 request), second will get load my server's CPU, but will not disturb camera's authorization.

I hope You can say "You're an idiot! There's a easy way to get image from protected folder! Just do..." ;)


Solution

  • Assuming that you are doing proper authorization in getImage.php, the second method (using a cache) will likely be more efficient.

    If getImage.php uses a Content-Type header, then simply place /myCameraSnapshots out of the web server root (or restrict it to local access only), and then use readfile as needed to dump the contents when called.

    Depending on how often the image(s) on the camera change, you will need a method to invalidate the cached image as well (perhaps by deleting it), which should prompt getImage.php to request it again from the camera.