View Full Version : need help with a PHP Chat system


RickyH
01-18-06, 05:58 PM
here it is people lets hope it doesn't run in this though


<form action="chat.php3" method="post">
Name : <input type="text" name="name"><br>
Message : <input type="text" name="message"><br>
<input type="submit" value="Send">
</form>

<?
$messages = file("messages.html");
//parse the string
for ($i = 0; $i < count(messages); $i++){
echo($messages[$i]);
}
?>
<?php
if($name != "" && $message != ""){
$messages[(count(messages)+1)] = "$name : $message<br>\n";

$fh = fopen("messages.html", "w");
fwrite($fh, $messages[count(messages)]);
fclose($fh);
}
?>

<?

//
//
//
$header = "<html><body bgcolor=\"#000000\" text=\"#ffffff\">\n";
?>
<?

$header = "<html><head><meta http-equiv=\"refresh\" content=\"8\">".
"<meta name=\"robots\" content=\"noindex\"></head>".
"<body bgcolor=\"#000000\" text=\"#ffffff\">\n";

?>
<?

$footer = "<p align=\"center\"><font color=\"#000000\">".
"&copy; Ricky Houy 2006</font></p></body></html>";

?>


For future reference the [ php ] tags in forums stops the code being interpretted by the server/browser.

RickyH
01-18-06, 05:59 PM
anyone see the problem in that?? if it was working correct the chat would run directly into the forum

but it isn't if anyone knows where it is messing up please please tell me

RickyH
01-18-06, 06:00 PM
small update to it

<form action="chat.php3" method="post">
Name : <input type="text" name="name"><br>
Message : <input type="text" name="message"><br>
<input type="submit" value="Send">
</form>
<?
$messages = file("messages.html");
//parse the string
for ($i = 0; $i < count(messages); $i++){
echo($messages[$i]);
}
?>
<?php
if($name != "" && $message != ""){
$messages[(count(messages)+1)] = "$name : $message<br>\n";
$fh = fopen("messages.html", "w");
fwrite($fh, $messages[count(messages)]);
fclose($fh);
}
?>
<?
//
//
//
$header = "<html><body bgcolor=\"#000000\" text=\"#ffffff\">\n";
?>
<?
$header = "<html><head><meta http-equiv=\"refresh\" content=\"8\">".
"<meta name=\"robots\" content=\"noindex\"></head>".
"<body bgcolor=\"#000000\" text=\"#ffffff\">\n";
?>
<?
$footer = "<p align=\"center\"><font color=\"#000000\">".
"&copy; Ricky Houy 2006</font></p></body></html>";
?>

RickyH
01-18-06, 09:35 PM
I am beggining to think no one can help me.....

ranger
01-19-06, 01:09 AM
Sorry Ricky,

I use C#, I know little PHP. :eek:

Maybe you would like this site: http://www.phpopenchat.org

RickyH
01-19-06, 02:10 AM
Same here.....

I will check that site out thank you for giving me some help though

Kunax
01-19-06, 03:53 AM
you seem to be missing a few $ here and there

edit.: is this the code you got or have the forum cut someoff?, try posting your source within ['code]['/code] tags(without the ' ofcause)

RickyH
01-19-06, 04:53 AM
you seem to be missing a few $ here and there

edit.: is this the code you got or have the forum cut someoff?, try posting your source within ['code]['/code] tags(without the ' ofcause)


Alot of it got cut off but ok i will do that.

RickyH
01-19-06, 04:54 AM
<form action="chat.php3" method="post">
Name : <input type="text" name="name"><br>
Message : <input type="text" name="message"><br>
<input type="submit" value="Send">
</form>
<?
$messages = file("messages.html");
//parse the string
for ($i = 0; $i < count(messages); $i++){
echo($messages[$i]);
}
?>
<?php
if($name != "" && $message != ""){
$messages[(count(messages)+1)] = "$name : $message<br>\n";
$fh = fopen("messages.html", "w");
fwrite($fh, $messages[count(messages)]);
fclose($fh);
}
?>
<?
//
//
//
$header = "<html><body bgcolor=\"#000000\" text=\"#ffffff\">\n";
?>
<?
$header = "<html><head><meta http-equiv=\"refresh\" content=\"8\">".
"<meta name=\"robots\" content=\"noindex\"></head>".
"<body bgcolor=\"#000000\" text=\"#ffffff\">\n";
?>
<?
$footer = "<p align=\"center\"><font color=\"#000000\">".
"&copy; Ricky Houy 2006</font></p></body></html>";
?>

RickyH
01-19-06, 04:55 AM
Does That help at all?

Kunax
01-19-06, 06:32 AM
not at all :)

Anyway im not sure what you actual want, but you can use phpbuilder.com and php.net to view the manual for referances and code samples, there maybe even be some chat examples in the source archives.

RickyH
01-19-06, 07:02 AM
Lol, i just want to know what is wrong with it. I could post it to where you can see the code, without t running in the forum. But I don't know how much that will help.

RickyH
01-19-06, 07:04 AM
fo|rm action="chat.php3" method="post"
Name : input type=text name=name"br
Message : input type="text" name=\"message"br
input type="submit" value="Send">
/form>
?
$messages = file("messages.html");
//parse the string
for ($i = 0; $i < count(messages); $i++){
echo($messages[$i]);
}
?>
<?php
if($name != "" && $message != ""){
$messages[(count(messages)+1)] = "$name : $message<br>\n";
$fh = fopen("messages.html", "w");
fwrite($fh, $messages[count(messages)]);
fclose($fh);
}
?>
?
//
//
//
$header = "<html><body bgcolor=\"#000000\" text=\"#ffffff\">\n";
?>
?
$header = "<html><head><meta http-equiv=\"refresh\" content=\"8\">".
"<meta name=\"robots\" content=\"noindex\"></head>".
"<body bgcolor=\"#000000\" text=\"#ffffff\">\n";
?>
?
$footer = "<p align=\"center\"><font color=\"#000000\">".
"&copy; Ricky Houy 2006</font></p></body></html>";
?>

leopold99
01-19-06, 07:04 AM
just what exactly is this code doing?

i just got back from a power supply design site and seen the first post
i entered the followimg info
name: ricky houy
message : testing

then clicked the button
when i did i was returned to the power supply site i was just on

RickyH
01-19-06, 07:05 AM
it is a chat system, the basic code for most PHP chat rooms

leopold99
01-19-06, 07:42 AM
but why did it return me to the power supply site?
when i make a post on sciforums i am always returned to the post i made

RickyH
01-19-06, 07:44 AM
Because of this
/form>
?

It just does that.

leopold99
01-19-06, 07:51 AM
that is about the lamest answer i have ever heard from a coder

RickyH
01-19-06, 07:53 AM
lol yep

I am a lazy person

i figured yo uwould just say oh ok and not question it.

Really, it is because it has no where else to go. So it goes to last index, or if it was the last page you viewed. Somtimes it is because of how your computer is set up.

Kinda goes off last pages viewed, or somthing of that sort.

I tend to not like answering these questions because most people are not educated with computers.

RickyH
01-19-06, 08:19 AM
leopold99, but I can tell you like an answer for your questions, so from now on any question you ask me i will give you the full answer.

RickyH
01-20-06, 08:47 PM
So, does anyone know? I've been trying to find out this answer and to no help at all.

PHP isn't my thing, but i'm positive some one here knows php better then me.

Voodoo Child
01-21-06, 06:34 PM
As someone pointed out, dollar signs on the all the variables would be a good start.
Are/ can you get error messages?

RickyH
01-21-06, 06:51 PM
There is no error message

The dollar signs are fine.

It just doesn't register the rest of the script.

Voodoo Child
01-22-06, 03:01 AM
for ($i = 0; $i < count(messages); $i++){
should be count($messages)

Is the error reporting/logging off, or are there no errors?

RickyH
01-22-06, 04:19 AM
should be count($messages)

Is the error reporting/logging off, or are there no errors?


There are no errors shown, it just shows up as it does on this site.

Kunax
01-22-06, 05:22 AM
this is very simple but its working


<html>
<head>
<title><title>
</head>
<body>

<?

//SAVE MESSAGES
if($HTTP_POST_VARS['submit']=="Send"){
$filename = 'messages.txt';

$handle = fopen($filename, 'a');
$msg = "\n".$HTTP_POST_VARS['name']." : ".$HTTP_POST_VARS['message'];
fwrite($handle, $msg);
fclose($handle);
}

//GET AND ECHO MESSAGES
$lines = file('messages.txt');
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
?>

<form action="chat.php" method="post">
Name: <input type="text" name="name" /><br />
Message: <input type="text" name="message" /><br />
<input type="submit" name="submit" value="Send" />
</form>
</body>
</html>

RickyH
01-22-06, 08:55 AM
this is very simple but its working




Thanks man i'll test it out :D But im thinking it might just be a better idea to go with a jsp chat system.

Kunax
01-22-06, 11:55 AM
You will be needing some form of script that will update the client, javascript is a solution.
What i made is also as i sayd very basic, there is no flood control, multipost or user loggin ect. ect. It's only the very basic for read/write and display of text

edit.: also its a freaking text file, the use of a database would be much better imo

RickyH
01-22-06, 12:24 PM
You will be needing some form of script that will update the client, javascript is a solution.
What i made is also as i sayd very basic, there is no flood control, multipost or user loggin ect. ect. It's only the very basic for read/write and display of text

edit.: also its a freaking text file, the use of a database would be much better imo


Lol, yea I'm taking chat rooms back to 1998.

But, it's all the person who wants it needs. But, yea your script worked. I added login, flood control, multi post and admin - the admin kinda sucks, but im doing it for free, so i'm not gonna go all out.

Thanks for your help bro.

Stryder
01-24-06, 06:13 AM
edit.: also its a freaking text file, the use of a database would be much better imo

Since Chat Rooms rarely cache the overall discussion unless there is "logging", they are probably best just using a File access as apposed to a database. Otherwise it would mean having to keep a SQL connection open if you didn't want any delay's which would have drastic implications on server performance.

Admittedly with a text file though there is a limit to the number of accesses that can be directly connected to it, so obviously there would have to be flood control scripts implace anyway.

Databases should really be saved for when you are dealing with multiple data structures, for handling login and user preferences etc, but not really for running a chat room from.

In all intense purposes a better chat program wouldn't even use an Interpreter, you'd really want to program a Binary "Daemon" that uses RAM for caching rather than filewrites with Database access being used for the reasons mentioned above.

It would lower the problems that running a Chat room through an Interpreter (PHP/Perl) with limited accesses to SQL and of course the webserver, which all have flood limits.

RickyH
01-24-06, 08:50 AM
Since Chat Rooms rarely cache the overall discussion unless there is "logging", they are probably best just using a File access as apposed to a database. Otherwise it would mean having to keep a SQL connection open if you didn't want any delay's which would have drastic implications on server performance.

Admittedly with a text file though there is a limit to the number of accesses that can be directly connected to it, so obviously there would have to be flood control scripts implace anyway.

Databases should really be saved for when you are dealing with multiple data structures, for handling login and user preferences etc, but not really for running a chat room from.

In all intense purposes a better chat program wouldn't even use an Interpreter, you'd really want to program a Binary "Daemon" that uses RAM for caching rather than filewrites with Database access being used for the reasons mentioned above.

It would lower the problems that running a Chat room through an Interpreter (PHP/Perl) with limited accesses to SQL and of course the webserver, which all have flood limits.


Well, he was only giving me a chat system and nothing more.

I put the rest together.

It wasn't supposed to be at it's best, trully. It was meant to go with the theme of the site, ( early 90's PHP scripting) Don't ask....

The who thing was meant to look kind of "cheap"

But, we changed our mind on it, and used the PHP until i finish with my JSP chat room.

Which, will be better then most chat rooms you see now a days.

Kunax
01-24-06, 12:48 PM
Since Chat Rooms rarely cache the overall discussion unless there is "logging", they are probably best just using a File access as apposed to a database. Otherwise it would mean having to keep a SQL connection open if you didn't want any delay's which would have drastic implications on server performance.

If what your making is just slightly bigger then a primitive shoutbox, you will quickly be needing a more advanced for of data storage, enter the database.
Connections can be closed after imidialty after the action you run againg the database finished, leaving no open connection, but maybe a lot of open/close calls. I dont know how heavy that is for a database, but since the user most often will get the same db connection on a webbased app, since his having the same web session, maybe its not to hard.... then again its still a open/close


Admittedly with a text file though there is a limit to the number of accesses that can be directly connected to it, so obviously there would have to be flood control scripts implace anyway.

I have a dislike for files, direct access seem like such a hack job to me, it might be "perfectly" safe and even needed sometimes(upload) but i just dont like direct access.
Like they say at zombo.com "the impossible is possible", and with users thats often true.


Databases should really be saved for when you are dealing with multiple data structures, for handling login and user preferences etc, but not really for running a chat room from.

Well first, if all you want is a shoutbox, you might be better of with a file based system. But if it's a chat system you after you will be needing to store alot of information, which a filebased system will be hard pressed to handle, like user handling, user preferences, chat/user historie and so on.


In all intense purposes a better chat program wouldn't even use an Interpreter, you'd really want to program a Binary "Daemon" that uses RAM for caching rather than filewrites with Database access being used for the reasons mentioned above.

It would lower the problems that running a Chat room through an Interpreter (PHP/Perl) with limited accesses to SQL and of course the webserver, which all have flood limits.

True but, that is only possible of you control the host, or by some miracle the hosting company allows your software on to there servers.