PHP and img src=""

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danturn
Forum Newbie
Posts: 17
Joined: Wed Sep 02, 2009 10:23 am

PHP and img src=""

Post 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
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: PHP and img src=""

Post 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.)
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: PHP and img src=""

Post 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)
danturn
Forum Newbie
Posts: 17
Joined: Wed Sep 02, 2009 10:23 am

Re: PHP and img src=""

Post 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
Post Reply