Jump to content

Shoutbox in PHP without MySQL database


El Estrago

Recommended Posts

Hi,

 

I want to make a Shoutbox in Php without using a MySQL-database. This is the code I've got:

<?php
$nickname = strip_tags($_POST['nickname']);
$message = strip_tags($_POST['message']);
?>



<html>
<body>
<<<HERE<b><h3><center>Shout Box</center></h3></b>
<iframe src="chat.txt" width="700" height="250"></iframe>
<form method="post" name="form">
Nickname: <input type="text" name="nickname" value="<?php echo $nickname;?>"><br>
Message: <input type="text" name="message" value="<?php echo $message;?>">
<input type="submit" value="Submit">
</form>
<a href="#">Refresh</a>


<?php

if ($_POST['nickname'] && $_POST['message']){
$fp = fopen("chat.txt", "a");
$stuff = $nickname . ": " . $message . "n";
fputs($fp, $stuff);
} else {
echo "Please enter nickname and message.";
}

?>

</body>
</html>

 

Problem is: it isn't working. :-P

It does read things from chat.txt, but I cant get the input-box working (let people add stuff to chat.txt).

What do I do wrong?

Link to comment
Share on other sites

Your not reading the text file though, your opening it currently to append to the text but not read from it..

$urls="http://www.yourdomain.com/filename.ext";
$page = join("",file("$urls"));
$kw = explode("\n", $page);

for($i=0;$i<count($kw);$i++){
echo $kw[$i];
}

 

also change

$stuff = $nickname . ": " . $message . "n";

to

$stuff = $nickname . ": " . $message . "\n";

 

in your existing code posted..

 

Link to comment
Share on other sites

Your not reading the text file though, your opening it currently to append to the text but not read from it..

$urls="http://www.yourdomain.com/filename.ext";
$page = join("",file("$urls"));
$kw = explode("\n", $page);

for($i=0;$i<count($kw);$i++){
echo $kw[$i];
}

 

also change

$stuff = $nickname . ": " . $message . "n";

to

$stuff = $nickname . ": " . $message . "\n";

 

in your existing code posted..

 

Where should I past that first part?

Also: is it possible to have the shoutbox refreshed every second? (Having just one frame refreshed while the rest of the site 'stays')

Link to comment
Share on other sites

You would paste the extra part I mentioned, anywhere you want it to display the text in its "Shoutbox" form.

 

As for the frame, yes it is possible. Just make it load in an iframe or something, and use a piece of javascript in the HTML for that frame that refreshes the frame every x seconds.

 

an example of my code plus a refresh in a file for use in an iframe would be..

<html>
<head>
<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
//   -->
</script>
</head>
<body onload="JavaScript:timedRefresh(30000);">

<?php
$urls="http://www.yourdomain.com/filename.ext";
$page = join("",file("$urls"));
$kw = explode("\n", $page);

for($i=0;$i<count($kw);$i++){
echo $kw[$i]."<br />";
}
?>

</body>
</html>

 

A huge note worth mentioning is.. storing data in something like this is not highly advisable. A couple of the key reasons being..

One is as this file grows, your bandwidth is going to consume quickly if your on a hosting package with limited bandwidth its going to hurt eventually. Say you get about 2000 lines of shouts each shout is 100 words or so, your looking at maybe 1) a 500kb file size on that text file (or bigger, maybe bigger). Now that doesnt seem like a lot, but lets pretend for a minute you get a spout of 100 people coming to your site in an hour. Thats 5mb of data used again doesnt seem like much but now imagine if they site there for an hour, your script is refreshing to 100 people every 30 seconds thats 500kb * 100 users * 120 refreshes per user in that hour. So in that hour you just popped 5,000kb 120 times.. thats 600,000kb or 585mb of bandwidth. And thats all for 1 hour, imagine a day where you've got many hours back to back like that.. then times that by 30 days..

 

Also on the same note, short of hosting your site on a cloud or grid server system where resources are mirrored for heavy useage. A single file can get whats called a bottle neck effect. Think of a full bottle of soda for example. Unscrew the cap, and flip it upside down. It doesn't all come out at once does it. Takes a minute right? Think about that say concept as your file. Eventually it will get over used with all the constant calls to it, that some users will come up with a file not found error, which in turn will break your script.

 

And biggest reason for me why I wouldnt do this text file as storage. Is its a security threat. Someone who knows what they are doing, or someone who is using bots to probe for things as such will eventually XSS attack your site, exploiting that file and dumping malisious code in it, and when it renders for them in the iframe can and will all them to do whatever said code does. It sucks trust me Ive had clients who have had it happen to them, clean up on all levels reallly sucks.

 

Im going to go out on a limb say since your new to coding, you feel php is more than you can handle right now adding javascript, or the ability to use a database rather than a flat file seems like wayyy to much. I used to feel and think the same way so many years ago that I would go way above and beyond to go out of my way and find ways to avoid using javascript and mysql. What I have to say to you with that is don't avoid it, embrace it.. and then run with it.

 

If you need further help feel free to message me on AIM (in profile). I wont serve as your end all be all resource of information and I wont do everything for you but Ill be happy to help when and if I can to getting you where you want to go with things. If you truly desire learning the craft, and are motivated enough to actually do the work needed to achieve the goals you want.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.