Dynamic PHP Image "Loading..."

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
ansichart
Forum Newbie
Posts: 1
Joined: Tue Nov 04, 2008 8:23 am

Dynamic PHP Image "Loading..."

Post by ansichart »

I created a PHP script that checks several TCP ports on the viewer's IP address and then it displays an image corresponding to their open ports. The problem is, the image may take about 5-10 seconds to load since the script has to run a port scan first, before the image is displayed. I was wondering if there is a way (without using JavaScript) to have the image say "Scanning..." with maybe a GIF animation (like an AJAX loader) and then when the scan finishes, the image changes to the port scan result. It is very important that no JavaScript is used... it must all come from the image... meaning it would have to be an animated GIF image... how would I do this?
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Dynamic PHP Image "Loading..."

Post by crazycoders »

Not possible the way you are explaining this.

An image is a stream of data that you have to send all at once when it is finished. Not just a part of it. You can make the stream sleep but the browser could cut off the connection anytime.

The only way you can do this in a good way would be to send an HTML page with content that says "Please wait for port scan to complete. And image will eventually load below containing the port scan result." Then the image should be output using an image library.

Although this could be prone to attacks, you could use a script that scans for only 1 port at a time and display several images of each test. Just generate a page from PHP with all tests that you wish to do and each image beside each test would trigger the port scan for only one port.

Just remember to test all input from the images or this technique could actually be used to do mass port scans from anytool in the web. I'd limit the port scan capability per IP so that you don't get accused of port scanning the whole world wide web.

:)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Dynamic PHP Image "Loading..."

Post by pickle »

Ya, it's not possible with only 1 image. If Javascript is a complete no-go, you could have the page just display no image, and instead display a message saying "Please wait, image is being created" or something else, then have a META refresh that reloads the page in 5 or so seconds. On that reload, if the image exists, display it. If not, display the message again & reload in 5 more seconds.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Dynamic PHP Image "Loading..."

Post by Jenk »

Just set the alternate text to "image loading" or perhaps a heading/paragraph/etc with "Loading" then overlay it with the image.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Dynamic PHP Image "Loading..."

Post by onion2k »

Actually it is possible... but only if you're willing to delve into the arcane horror of Keep-Alive connections.

Basically you'd need to create an HTTP "Keep Alive" connection and send the headers of an animated gif that doesn't loop, and the first frame, then trickle more "please wait" frames as the scan is happening, and then create and send the final frame with whatever the user should see at the end. The user's browser will display each frame as it arrives and it'll stop on the last one. Which is what you want.

Now, this presents some interesting challenges ... first, I'm not sure how you create a keep alive connection in PHP, or even if it's possible. Second, you won't be able to use GD. GD creates complete images, you only want frames. You'll have to write something to make animated GIFs frame by frame yourself.

If this was 10 years ago and a discussion about Perl then I'd have been able to give you the code to do it because I remember playing with it at university. But it's not. So I can't.

Good luck.
mmj
Forum Contributor
Posts: 118
Joined: Fri Oct 31, 2008 4:00 pm

Re: Dynamic PHP Image "Loading..."

Post by mmj »

If you can't use JS then it won't be easy at all; if possible.
cboileau
Forum Newbie
Posts: 8
Joined: Sun Nov 02, 2008 8:28 am

Re: Dynamic PHP Image "Loading..."

Post by cboileau »

Pretty much impossible using the picture to say loading, however you could always Create a page that says "Scanning..." etc, and then have it redirect once the scan is complete and display the picture.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Dynamic PHP Image "Loading..."

Post by Mordred »

onion2k wrote:I'm not sure how you create a keep alive connection in PHP
You basically can't. The brower requests it, the server - if it decides to - accepts it (or denies it).

What you describe though (you eevil, eevil, man ;) ) is certainly doable, because it requires transporting of one HTTP response, which you can do regardless of whether the connection is keep-alive, pipelined or regular.

That said, this would put a strain on the server, as it won't close the connection as fast as it could. But anyway port scanning is a slow busyness, so the OP should be prepared to have a strained server.

And another thing, the easiest solution would be to have a chain of requests, each one scanning only one port and displaying the results from the scan up to now. Store the state in sessions, force the page to reload each X seconds, and voila.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Dynamic PHP Image "Loading..."

Post by VladSun »

What about using Content-Type: multipart/x-mixed-replace;boundary=$#@$#@

viewtopic.php?f=1&t=88928 ?
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply