[Fwd: Re: [PHP] Chat with php]
| From: | Marco Tabini | Date: | Mon, 07 Oct 2002 19:18:23 +0000 |
| Subject: | [Fwd: Re: [PHP] Chat with php] | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-119127@lists.php.net to get a copy of this message | ||
Ok, off the top of my head (please don't be upset if it doesn't work
right off, because I'm doing this from memory):
Web page:
<script language=javascript>
function checkimage()
{
if (document.all.image1.width == 1)
{
document.all.fr1.src="http://www.mysite.com/chat.php";
}
}
function reloadimg()
{
document.all.image1.src="http://www.mysite.com/img.php";
setInterval ("reloadimg()", 2000); // every 2 secs
}
</script>
<body>
<iframe name="fr1" src="chat.php">
<img style="display=none" src="http://www.mysite.com/chat.php"
onLoad="javascript:checkimage();">
</body>
In chat.php you simply push out whatever is in the database (the whole
thing--or you come up with some method of just printing out what has
changed since the last poll, which is trivial and beyond the point
here).
In img.php:
<?
function checkdb()
{
// This returns true if new data is available
}
$filename = (checkdb() ? '1pixel.gif' : '2pixel.gif');
header ("Content-type: image/gif");
readfile ($filename);
?>
1pixel.gif is a one-pixel wide image, while 2pixel.gif is two pixels
wide (it just occurred to me that you can't have a zero-pixels wide
image, which would be quite pointless). The script basically reads in
either file and spits it out after setting the content type.
Once more, this script, as is, probably won't work because it was
programmed in my messy head rather than in a proper PHP environment, but
it should give you an idea of what needs to be done. In particular,
remember to use the "display" CSS attribute rather than visibility so
that the image will not take up any space on your page.
Also, an apology--I seem to have mailed some messages twice by mistake.
I was checking e-mail between eating my lunch and trying to get a tie on
and things might have gotten a bit out of hand ;-)
Let me know how it turns out!
----Forwarded Message-----
> From: Oliver Witt <oliverwitt@t-online.de>
> To: php-general@lists.php.net, Marco Tabini <marcot@tabini.ca>
> Subject: Re: [PHP] Chat with php
> Date: 07 Oct 2002 20:44:08 +0200
>
> Marco Tabini schrieb:
>
> > Simple trick (well, not so simple, but kind of a Columbus' Egg):
> >
> > 1) Create an <img> tag in your web page that is hidden
> >
> > 2) Point the img tag to a php script that returns:
> >
> > 1) An image with a pixel width of 1 if there is new data to pick
> > up from the server
> > 2) An image with a pixel width of 0 otherwise
> >
> > 3) Create a javascript that refreshes the image every second (or every
> > two seconds, or however often you feel necessary--the more often, the
> > more responsive and "real time" your chat system will be).
> >
> > 4) Add an OnLoad event to the img tag that points to a Javascript that
> > checks the width of the newly reloaded image. If the width is one, then
> > you reload your chat text content (which you may want to put in an
> > iframe to avoid having to reload the whole page).
> >
> > I don't have a script to show unfortunately, as it would probably be too
> > complicated and long for the list (and I have no time to write
> > it...<sigh>), but hopefully, this will give you the gist of it.
> >
> > Cheers,
> >
> > Marco
> >
>
> Okay, that sounds like something i can do, i just need to know how to hide
> and how to point the img tag at something.
> Olli
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>