1

I have a webpage that needs to display an image (mjpeg) from an IP camera on the same network. The camera requires authentication, so using the following code on the page obviously displays nothing:

<img src="http://10.0.0.74/mjpg/1/video.mjpg" />

However, if I navigate to the camera's setup page I get a 401 challenge and I can then enter my credentials. If I then return to the original page without closing the browser and refresh it, the image displays correctly.

My question is; how can I automate this using PHP/Javascript so the user doesn't have to do a manual login to the camera before seeing the image?

I have already tried using the following PHP function called on page load with the url of the image (as an attempt to pre-authorise the request, rather than actually doing anything useful), this seems to complete ok without throwing any errors, but the image is still not displayed on the page.

function authenticateInput($url, $user, $pass){
    $c = curl_init($url);
    $authString = '$user:$pass';
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c, CURLOPT_USERPWD, $authString);
    curl_exec($c);
}
AlexB
  • 91
  • 1
  • 8
  • `didn't seem to work` is not very helpful, what does that request do exactly? And you would need to store the cookie and make another request using the same session cookie to get the image in php and serve it to the browser. – jeroen May 17 '18 at 09:49
  • I doubt if it is even possible, I think browsers don't want to do this for security reasons. – Andrii Maletskyi May 17 '18 at 09:50
  • @jeroen I've edited to explain more. Thanks for the tip about cookies, I'll have a look into it. – AlexB May 17 '18 at 09:55
  • Step 1: Learn to differentiate between stuff going on on the server side, and stuff going on on the client side. Making this request in your server-side code could at most “log in” _your server_ to the camera. Of course this has zero effect on the client. – CBroe May 17 '18 at 09:55
  • I might be wrong, but isn't that a very basic authentication form where you're required to use the user and pass in the http request, like so `http://username:password_@10.0.0.74/mjpg/1/video.mjpg` ??? Very unsafe though as the credentials travel unencrypted over your network... – Werner May 17 '18 at 10:03
  • @Werner I have actually tried this as a test but it seems browsers are moving to block it (https://www.chromestatus.com/feature/5669008342777856) – AlexB May 17 '18 at 10:19
  • 1
    @AlexB And, with good reason! Then your PHPcURL route would be the best one. I think you just need to play around with the options. Read up on https://stackoverflow.com/questions/3008817/login-to-remote-site-with-php-curl – Werner May 17 '18 at 10:27

0 Answers0