Hey guys! I am designing a chat room with php and the chat room is basically able to send messages and display messages<br/>##the chat is functioning basically##<br/> but the ooonnllyy problem is that, i wanna display an alert box each time a new message comes in #its a public chat room#.... pls guys i knw dis isn't very mch detailed but please, i really need your help pleeeasse. Thanks a lot

Recommended Answers

All 3 Replies

The problem with PHP is that PHP is executed server side, what this means is that it is run before the page is even loaded.

In terms of your chat room problem, this means that it won't be real time, or live. It can only possible occur once a refresh occurs. If you want it to be live, you shall want to use AJAX and JavaScript to query the database on an X second basis.

If you still want it to be PHP based then I would suggest when they enter the chatroom you store the time of the last post in a SESSION, then when they hit a 'refresh' button or reload the page it checks to see if anything has occured later than the stored variable, if it has then you can show a little box and if not then it can be left.

Yea, thanks a lot for that. Really put me on a path. But, the page is automatically refreshed evry 3 secs, so wat you are saying is, i shld get the current time of the last message and store it in a database, then if there is a change in it, then i shld display a notification. I think i get that a little. But, the thing is, how to i detect a change in last message.

You are partly on the right line, although I would firstly suggest using a SESSION variable instead of storing it in a database.

A SESSION variable is like a cookie, it can be used to store some information, and is unique to the user. I think you shall find this better than storing it in a database, because this would mean when you have multiple users, it shall be unique as to what messages they have seen instead of everyone being updated when ever a new message comes through.
This means that if I log in, I shall only see what messages I haven't seen, instead of messages everyone hasn't seen. (Not sure I really explained that very well, sorry).

When your page refreshes, store the variable in a SESSION (or in a database if you want), and hold information on at least the time it was posted, but possible also the title of it and who posted it.
Once you have this, when the page refreshes you shall want to sort the posts in ascending order and take the time of the first one (the latest post). You then want to compare that time with the stored time, if the stored time is the same then it hasn't been updated but if it is different then there has been an update.

This is the simplest method, if you want to do some crazy maths to work out the exact time change then you can, but it is easier to just check if they are the same or not.

Good luck and don't hesitate to ask for anymore help!

Just a quick note though, be careful about refreshing too often, because you shall potentially add a lot of stress to the server, especially with a lot of users.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.