Forum Moderators: coopster

Message Too Old, No Replies

filesize problems

         

panas

1:25 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



I have made a php chat webpage with some javascript. I don't use a database to store the messages. Instead I use a text file.
I am trying to make the page to refrseh automaticaly when a new message is sent from a user. So I made an interval of 3 secs for the page and every 3 secs I am using the filesize(chat.txt) to check if the size of the file has changed (so I can refresh the page to show the new messages). The problem is that the filesize command always returns the same file size even if someone has send a new message. the filesize is corect only when I refresh the page.
Thank you in advance!

dcrombie

3:08 pm on Dec 6, 2004 (gmt 0)



1) PHP doesn't run in the browser - only on the web server;
2) The browser only talks to the server at the instant when a page is requested.

lZakl

4:21 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



dcrombie is right, thus the fact you always see "Chat Update" or something of the such on the older web-based, text-stored chat sites. Short of making an Applet in JAVA, the Update button (at which time the form calls to itself) might be your best bet.

panas

4:32 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



You mean that if I call filesize() after the page is loaded (from a jacascript), it won't work?
I just do it in my page in specific intervals and the command return me the filesize (not the correct). I mean that the command is running even though it is in the client side.
Sorry if my question is stupid...

lZakl

6:14 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



I don't really understand your last post, however let me elaborate.

A Javascript can access any content that it has pre-loaded, or loaded to the browser for the current browsing session. Once the browser is finished loading, that's it. No more talking to the server.

Even though you might have some dynamic things going on via JavaScript, there is no real interaction with the server once the page is done loading. All the information that the JavaScript is using is already loaded to the browser. JavaScript cannot talk to the server interactively and wait for a file size update. The only thing that this will return will be the original filesize when the page was loaded.

Hope this helps :)

panas

10:21 am on Dec 7, 2004 (gmt 0)

10+ Year Member



Now I understand what you meant.

Thanks for your enlightening answers.

panas

10:52 am on Dec 17, 2004 (gmt 0)

10+ Year Member



After a lot of searching I have find a solution.
I use a hidden iframe in my page. A page page which is refreshed every 3 secs reads the filesize and if anything has changed, it reads the file and pass it to the main page.

Everything can be done

mincklerstraat

11:25 am on Dec 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, if
filesize()
is in your php script, checking the size of the text file on your own server, this will work. You might also want to use:

header(Cache-control: nocache);

and maybe some other cache headers suggested here [be.php.net] to make sure browsers don't cache the content; normally they won't cache PHP output since it doesn't have relevant cache headers, but some browsers might just figure that nothing could have possibly changed after only 3 seconds - I know I've encountered this with images.

If this functionality is really important to you, you might still want to look into getting a java applet like jpilot. PHP has a pretty lousy reputation when it comes to chat apps since it's stateless, though this will probably be changing somewhat with 'streams' etc - but then you really have to know what you're doing. An applet will give your users a 'smoother' chat experience and might also help you save bandwidth. If you really, really want to do this with PHP and javascript, you can check out the article Crouching Javascript, Hidden PHP [sitepoint.com].