PHP Chat

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

PHP Chat

Post by d3ad1ysp0rk »

http://www.404-design.net/chat/chat.php

IFrame refreshes every 2 seconds.

Can you guys give me some ideas on how to improve it? My friend was right, constant refreshing is annoying, but it's the only way I've thought of to do it if theres a lot of ppl in the room..
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

PHP isn't great for this, might want to use one of the many cgi/perl/etc scripts out there for doing it *shrug*
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Thanks for the input, and I knew PHP wouldn't be great for it from the start, but I'm stubborn.. and don't know cgi/perl :P
Also, I know you can just copy n paste the scripts, but I like to know what it's doing so I can fix it or edit it later on..
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

There is a trick I vaguely remember, possibly using frames and js. Try googling around - should track it down pretty quickly.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

i have a similar problem, and would like to look at some of those cgi/perl scripts... but i can't find any!! would anyone happen to know where i could find some?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Incase anyone wanted to know how I did it, to give me some ideas on how to improve the design or whatever.. go ahead! :)

chat.php

Code: Select all

<?php
session_start();
if($_POST['mode']=="signin"){
   $_SESSION['username'] = $_POST['username'];
}
echo <<<EOM
<html>
<body>
<div align="center">
EOM;
if(isset($_SESSION['username'])){
   echo <<<EOT
   <iframe src="body.php" scrolling="auto" width="500" height="400" marginheight="0" marginwidth="0" vspace="0" hspace="0" name="bodyframe" frameborder="1"></iframe><br />
   <iframe src="bottom.php"  scrolling="no" width="500" height="30" marginheight="0" marginwidth="0" vspace="0" hspace="0" name="bottomframe" frameborder="1"></iframe><br />
EOT;
}
else {
   echo <<<EOT
   Enter a username to use for chatting:<br />
   <form action="chat.php" method="post">
   <input type="text" name="username"><br />
   <input type="hidden" name="mode" value="signin">
   <input type="submit" value="Start Chatting!">
   </form>
EOT;
}
echo "<br /><br />";
include("counter.php");
$myvar1 = getcount("uniq");
echo "Unique Visitors: " . $myvar1 . "<br />";
echo <<<EOZ
</div>
</body>
</html>
EOZ;
?>
body.php

Code: Select all

<?php
echo <<<EOT
<html>
<body>
<script language="javascript">
setTimeout("window.location.reload()", 2000);
</script>

EOT;
include ("chatcontent.txt");
echo "</body>\n</html>";
?>
bottom.php

Code: Select all

<?php
session_start();
if($_POST['submitted']==1){
   $username = $_SESSION['username'];
   $intext = $_POST['intext'];
   $intext = format($intext);
   $chatfile = fopen("chatcontent.txt", "a");
   fwrite($chatfile, "<b>" . $username . "</b>: $intext<br>\n");
   fclose($chatfile);
}
echo <<<EOT
<form action="bottom.php" method="post"><input type="hidden" name="submitted" value="1"><input type="text" size="60" name="intext">&nbsp;&nbsp;<input type="submit" name="submit" value="Send!"></form>
EOT;
function format($input){
   $input = stripcslashes($input);
   $input = str_replace("<script", " ", "$input");
   $input = str_replace("<iframe", " ", "$input");
   $input = str_replace("<div", " ", "$input");
   $input = str_replace("<span", " ", "$input");
   $input = str_replace("<hr", " ", "$input");
}
?>
Last edited by d3ad1ysp0rk on Fri Apr 21, 2006 1:04 pm, edited 1 time in total.
Evan
Forum Newbie
Posts: 11
Joined: Tue Feb 24, 2004 5:14 pm
Location: Hell
Contact:

Post by Evan »

Hello everyone, I have (what I think) is a pretty valid way to refresh a chat, but I need help with the code to get it working, i think it is possible using WHILE and FOR loops, I dont know, i need some help. I will give 50mb free hosting to whoever helps me out... I just REALLY need this done See my post for more info
viewtopic.php?t=18560&highlight=
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Evan: this is a code topic about chat & refreshing. Try the project help forum - you might find someone to help you out there.

(Or google around for a free script which doesn't "blink").
Evan
Forum Newbie
Posts: 11
Joined: Tue Feb 24, 2004 5:14 pm
Location: Hell
Contact:

Post by Evan »

Heh, OK thanks

sorry bout that
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Could you allow users to choose how often they want the screen to refresh, set it as a session variable or sommat?

Mac
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

have a look at this example. Dunno how it works
http://voc.onio.de/voc.php

Source here
http://vochat.com/

Mark
Post Reply