Page 1 of 1

PHP and img src=""

Posted: Wed Sep 02, 2009 10:30 am
by danturn
Hey guys,

I'm trying to grab an image from a web server using

Code: Select all

img src="http://10.50.200.23/CGI/Screenshot"
this works fine, but it asks me to authenticate every time...

is there any way to send the authentication within the request?

I was initially trying to do this using something like this:

Code: Select all

        $screenshotreq ="POST CGI/Screenshot HTTP/1.0\r\n";
        $screenshotreq.="Host: 10.50.200.23\r\n";
        $screenshotreq.="Authorization: Basic 10014535:4535\r\n";
        $screenshotreq.="Connection: close\r\n";
        $screenshotreq.="Content-Type: application/x-www-form-urlencoded\r\n";
        $response = "";
        $sock = fsockopen($ip, 80, $errno, $errstr, 30);
        if ($sock) {
            fwrite($sock, $post . $xml);
        while (!feof($sock)) {
            $response .= fgets($sock, 128);
        }
        fclose($sock);
        }
}
But I can't work out how to interpret the result into an image in a table

Any help grandly appreciated (i'm a n00b when it comes to PHP and am really struggling!)

Dan

Re: PHP and img src=""

Posted: Wed Sep 02, 2009 11:07 am
by Skara
Ok... yeah.. I've never worked with sockets, so I'm going to make an assumption that $response contains the image data. If that's the case...

Code: Select all

//get_auth_image.php
 
        $screenshotreq ="POST CGI/Screenshot HTTP/1.0\r\n";
        $screenshotreq.="Host: 10.50.200.23\r\n";
        $screenshotreq.="Authorization: Basic 10014535:4535\r\n";
        $screenshotreq.="Connection: close\r\n";
        $screenshotreq.="Content-Type: application/x-www-form-urlencoded\r\n";
        $response = "";
        $sock = fsockopen($ip, 80, $errno, $errstr, 30);
        if ($sock) {
            fwrite($sock, $post . $xml);
            while (!feof($sock)) {
                $response .= fgets($sock, 128);
            }
            fclose($sock);
        }
 
        header('Content-Type: image/jpeg');  //assuming it IS a jpg
        echo $response;

Code: Select all

//somepage.php
echo '<img src="get_auth_image.php?.jpg" alt="" />';
 
The ?.jpg at the end is a just-in-case to make some browsers happier about having a php file within an img tag. (It doesn't actually do anything.)

Re: PHP and img src=""

Posted: Wed Sep 02, 2009 11:10 am
by jazz090
try changing the src of your image to this: 'http://username:password@10.50.200.23/CGI/Screenshot'

but you will expose the username and password for that site. i recommend creating a separate folder for images ONLY and password protect it with apache, that way you will only expose username/password to your images and not the whole site (make sure passes are different)

Re: PHP and img src=""

Posted: Thu Sep 03, 2009 3:34 am
by danturn
Hey Skara,

Thanks a lot for that... im gonna give that a go today/.

Thanks too Jazz... i did try that before but it didnt work (the web server is on a cisco IP phone so it's a bit off an oddity!)

Dan