Page 1 of 2

Flat File Based Chat Help

Posted: Sat Oct 13, 2012 1:43 pm
by pHp_n0ob
Can anyone please write a php code for a chat script as below:
For eg.
A user "user1" wrote "hi".
Another user "user2" wrote "how are you".
Another user "user3" wrote "Hi Everybody!".


So the messages are being written on file "chat.txt".
Now the challenge for me is How to sort the messages as Newest First.
I mean,it should show

user3:Hello Everybody!
user2:how are you
user1:hi

I have tried everything as per my knowledge
(I'm a n0ob :D )
but failed...can anyone write me the code for what I said. :( You can check my previous Topics

Re: Flat File Based Chat Help

Posted: Sat Oct 13, 2012 2:09 pm
by twinedev
The writing the file to file is pretty simple (I would recommend writing the file to a ram drive over a file on a hard drive if possible).

To read in the file to display with file() function
http://www.php.net/manual/en/function.file.php

It will now be in an array, then you can use count() to get the size of the array and then loop from count() - 1 to zero to echo out the chat

Not very efficient at all, but does it the way you want it.

-Greg

Re: Flat File Based Chat Help

Posted: Sat Oct 13, 2012 2:43 pm
by McInfo
This script outputs lines of a file in reverse order.

Code: Select all

<?php
header('Content-Type: text/plain');
$output = '';
$handle = fopen('data.txt', 'r');
if (is_resource($handle)) {
    while ($line = fgets($handle)) {
        $output = $line.$output;
    }
    fclose($handle);
}
echo $output;

Re: Flat File Based Chat Help

Posted: Sat Oct 13, 2012 11:24 pm
by pHp_n0ob
McInfo wrote:This script outputs lines of a file in reverse order.

Code: Select all

<?php
header('Content-Type: text/plain');
$output = '';
$handle = fopen('data.txt', 'r');
if (is_resource($handle)) {
    while ($line = fgets($handle)) {
        $output = $line.$output;
    }
    fclose($handle);
}
echo $output;
Thanks for the help...please explain the code also :)

Re: Flat File Based Chat Help

Posted: Sun Oct 14, 2012 10:26 am
by McInfo
↓ This tag tells PHP to interpret what follows as PHP code.

Code: Select all

<?php
↓ This is a call to the header() function which is used to set HTTP response headers. The Content-Type header tells the browser what kind of document it should expect to receive. The text/plain MIME type means that the document will be plain text. Usually, the content type defaults to text/html; but I didn't want to deal with escaping HTML that might be in the data file; and I didn't want to use HTML tags like <br> or <pre> to force line endings to be visible in the browser.

Code: Select all

header('Content-Type: text/plain');
↓ This statement initializes a variable named $output to hold an empty string. The string will act as a buffer to collect file data.

Code: Select all

$output = '';
↓ Here, the fopen() function is called to create a file resource which is assigned to the variable $handle. The file opened is "data.txt". The filename has no directory path before it, so it is probably located in the same directory as the PHP script. The second argument passed to fopen(), "r", causes the file to be opened in read mode.

Code: Select all

$handle = fopen('data.txt', 'r');
↓ This conditional statement ensures that fopen() actually returned a resource rather than FALSE (a boolean).

Code: Select all

if (is_resource($handle)) {
↓ This statement is a little more complex. First, the function fgets() is called to retrieve a line from the data file. Then, the returned value is assigned to the $line variable. Finally, the value of the assignment expression (which happens to be equal to the value stored in $line) is evaluated as a boolean that tells the while loop whether to process the statements in its block (the statements between the curly braces). Because most strings become TRUE when cast as boolean, the while loop will continue as long as fgets() returns a string. (There is a problem with this statement. See the note at the end of my post.*) When fgets() runs out of lines to read, it returns FALSE and the loop terminates.

Code: Select all

    while ($line = fgets($handle)) {
↓ This statement concatenates (joins) the $line string with the $output string and assigns the result to the $output variable. Because subsequent lines are placed at the beginning of the buffer, the lines will later be displayed in reverse order.

Code: Select all

        $output = $line.$output;
↓ This curly bracket marks the end of the while block.

Code: Select all

    }
↓ Calling fclose() releases the file resource. PHP would close it automatically with its garbage cleanup after the script ends, but it is a good practice to free resources as soon as possible.

Code: Select all

    fclose($handle);
↓ This curly bracket marks the end of the if block.

Code: Select all

}
↓ Finally, the string containing the buffered lines is echoed out.

Code: Select all

echo $output;
*Note: There is a problem with the while loop condition. As I said, most strings become TRUE when cast as boolean. However, "0" becomes FALSE. This is not a problem for most lines because fgets() returns the line ending; so the line "0" is actually "0\n" or "0\r\n" depending on the convention used by the program that last saved the file. Such strings become TRUE when cast as boolean. The problem is that the last line in a file might not have a line ending. In that case, fgets() returns "0" and that line will be ignored.

↓ Here, the value returned by fgets() must be identical (not just equal) to FALSE.

Code: Select all

    while (($line = fgets($handle)) !== false) {

Re: Flat File Based Chat Help

Posted: Sun Oct 14, 2012 12:08 pm
by Benjamin
8O

Re: Flat File Based Chat Help

Posted: Sun Oct 14, 2012 2:00 pm
by pHp_n0ob
McInfo wrote:↓ This tag tells PHP to interpret what follows as PHP code.

Code: Select all

<?php
↓ This is a call to the header() function which is used to set HTTP response headers. The Content-Type header tells the browser what kind of document it should expect to receive. The text/plain MIME type means that the document will be plain text. Usually, the content type defaults to text/html; but I didn't want to deal with escaping HTML that might be in the data file; and I didn't want to use HTML tags like <br> or <pre> to force line endings to be visible in the browser.

Code: Select all

header('Content-Type: text/plain');
↓ This statement initializes a variable named $output to hold an empty string. The string will act as a buffer to collect file data.

Code: Select all

$output = '';
↓ Here, the fopen() function is called to create a file resource which is assigned to the variable $handle. The file opened is "data.txt". The filename has no directory path before it, so it is probably located in the same directory as the PHP script. The second argument passed to fopen(), "r", causes the file to be opened in read mode.

Code: Select all

$handle = fopen('data.txt', 'r');
↓ This conditional statement ensures that fopen() actually returned a resource rather than FALSE (a boolean).

Code: Select all

if (is_resource($handle)) {
↓ This statement is a little more complex. First, the function fgets() is called to retrieve a line from the data file. Then, the returned value is assigned to the $line variable. Finally, the value of the assignment expression (which happens to be equal to the value stored in $line) is evaluated as a boolean that tells the while loop whether to process the statements in its block (the statements between the curly braces). Because most strings become TRUE when cast as boolean, the while loop will continue as long as fgets() returns a string. (There is a problem with this statement. See the note at the end of my post.*) When fgets() runs out of lines to read, it returns FALSE and the loop terminates.

Code: Select all

    while ($line = fgets($handle)) {
↓ This statement concatenates (joins) the $line string with the $output string and assigns the result to the $output variable. Because subsequent lines are placed at the beginning of the buffer, the lines will later be displayed in reverse order.

Code: Select all

        $output = $line.$output;
↓ This curly bracket marks the end of the while block.

Code: Select all

    }
↓ Calling fclose() releases the file resource. PHP would close it automatically with its garbage cleanup after the script ends, but it is a good practice to free resources as soon as possible.

Code: Select all

    fclose($handle);
↓ This curly bracket marks the end of the if block.

Code: Select all

}
↓ Finally, the string containing the buffered lines is echoed out.

Code: Select all

echo $output;
*Note: There is a problem with the while loop condition. As I said, most strings become TRUE when cast as boolean. However, "0" becomes FALSE. This is not a problem for most lines because fgets() returns the line ending; so the line "0" is actually "0\n" or "0\r\n" depending on the convention used by the program that last saved the file. Such strings become TRUE when cast as boolean. The problem is that the last line in a file might not have a line ending. In that case, fgets() returns "0" and that line will be ignored.

↓ Here, the value returned by fgets() must be identical (not just equal) to FALSE.

Code: Select all

    while (($line = fgets($handle)) !== false) {
Thanks for the great explaination friend and for the much time you spent on me...but sorry again,its again showing last post first. The reverse system is not working :( I'm using "a+" mode for opening and then to write.here's the demo and here's the code thanx again :-)

Re: Flat File Based Chat Help

Posted: Sun Oct 14, 2012 2:51 pm
by McInfo
Look in your data file. There is only one line.

Re: Flat File Based Chat Help

Posted: Sun Oct 14, 2012 2:55 pm
by twinedev
Browsing to http://topz.iwebs.ws/priyanka/Love.txt (the content of the file you are trying to display backwards) shows:
<--- WWW.WAPFTP.BIZ --->
TestingTesting2Testing3
Going to the test site you gave saying it isn't reversing it is giving:
TestingTesting2Testing3<--- WWW.WAPFTP.BIZ --->
So the "TestingTesting2Testing3" line (which is the second line in the text file) IS displaying before the "<--- WWW.WAPFTP.BIZ --->" line (which is the first line in the text file).

So it is working, you just are not putting each item on it's own line, you need to do $msg."\n" when writing the data to the file. Also you need add some <BR> or whatever you want to use between each line like:

Code: Select all

$output = $line."<br>\n".$output;


BTW, since this will be for a "chat", I would assume that this page will update often so people can see the latest replies. Say you have 10 people in chat, and you do one update per second, that is 600 page calls PER MINUTE, do you really need two different sites tracking all 600 calls per minute? (on top of the is 600 times a minute the text file will read... 600 times per minute that the apache log files will have to update... this is why HTTP based chat is so wasteful)

-Greg

Re: Flat File Based Chat Help

Posted: Sun Oct 14, 2012 3:44 pm
by twinedev
I just refreshed the page to see if anyone else posted... LOL I don't know who did it, but someone decided to let you know about some basics missing...

Re: Flat File Based Chat Help

Posted: Sun Oct 14, 2012 3:54 pm
by McInfo
twinedev wrote:I just refreshed the page to see if anyone else posted... LOL I don't know who did it, but someone decided to let you know about some basics missing...
It had to be done. Many programmers don't take the issue of filtering data seriously enough.

Re: Flat File Based Chat Help

Posted: Sun Oct 14, 2012 10:13 pm
by pHp_n0ob
twinedev wrote:I just refreshed the page to see if anyone else posted... LOL I don't know who did it, but someone decided to let you know about some basics missing...
I got it...LOL...I knew it but had left because I was focusing on "writing data".Now I did it :-D

Re: Flat File Based Chat Help

Posted: Sun Oct 14, 2012 10:15 pm
by pHp_n0ob
McInfo wrote:
twinedev wrote:I just refreshed the page to see if anyone else posted... LOL I don't know who did it, but someone decided to let you know about some basics missing...
It had to be done. Many programmers don't take the issue of filtering data seriously enough.
I'm not a programmer now, just a newbie...this alert was a lesson for me,thanx

Re: Flat File Based Chat Help

Posted: Sun Oct 14, 2012 10:17 pm
by pHp_n0ob
twinedev wrote:Browsing to http://topz.iwebs.ws/priyanka/Love.txt (the content of the file you are trying to display backwards) shows:
<--- http://WWW.WAPFTP.BIZ --->
TestingTesting2Testing3
Going to the test site you gave saying it isn't reversing it is giving:
TestingTesting2Testing3<--- http://WWW.WAPFTP.BIZ --->
So the "TestingTesting2Testing3" line (which is the second line in the text file) IS displaying before the "<--- http://WWW.WAPFTP.BIZ --->" line (which is the first line in the text file).

So it is working, you just are not putting each item on it's own line, you need to do $msg."\n" when writing the data to the file. Also you need add some <BR> or whatever you want to use between each line like:

Code: Select all

$output = $line."<br>\n".$output;


BTW, since this will be for a "chat", I would assume that this page will update often so people can see the latest replies. Say you have 10 people in chat, and you do one update per second, that is 600 page calls PER MINUTE, do you really need two different sites tracking all 600 calls per minute? (on top of the is 600 times a minute the text file will read... 600 times per minute that the apache log files will have to update... this is why HTTP based chat is so wasteful)

-Greg
600 files/minute...will it cause any problem...any extra load on server or other reason?

Re: Flat File Based Chat Help

Posted: Mon Oct 15, 2012 4:43 am
by Mordred
There is a reason database systems exist beyond writing stuff to a text file. Do not use flat files in production, it won't work.