Shoutbox

PHP Shoutbox help - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP Shoutbox help (/showthread.php?tid=75185)

PHP Shoutbox help by M73A on 06-08-2007 at 06:05 PM

i've got this script for a shoutbox off of hotscripts.. i just wondered if it was possible to have the php insert a new line (on the top of the .txt file) and put the new shouts ontop, so older ones are at the bottom.

Its a simple script that adds whats entered into the text input bit onto the end of the .txt file..

any ideas?

Thanks

p.s. shall i post the code?


RE: PHP Shoutbox help by vikke on 06-08-2007 at 06:14 PM

I wish to see the code to understand what you mean. :)


RE: PHP Shoutbox help by toddy on 06-08-2007 at 06:22 PM

why would you want new shouts at the top??
you'd have to have the input field at the top too, other wise u would mean that you'd have to view it on full window, or keep scrolling down to add new shouts.


RE: PHP Shoutbox help by M73A on 06-08-2007 at 06:33 PM

quote:
Originally posted by toddy
why would you want new shouts at the top??
you'd have to have the input field at the top too, other wise u would mean that you'd have to view it on full window, or keep scrolling down to add new shouts.
yeah i managed to move the input field at the top too:P

i want the new ones ontop so i dont have to keep clearing it if it gets to long, it will just scroll for ages but the newest ones will be on view :)

heres the indexy one
code:
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body style="overflow-x:hidden;overflow-y:scroll;">

<//------- Start copying from this point -------->

<table class="shoutBox">
<tr><td class="shoutBox">
<p class="heading"><b>.:: ShoutBox ::.</b></p>
<p>
<center>
<form name="shoutForm" method="POST" action="addShout.php">
Name: <input type="text" name="user" size="15"><br>
Shout: <input type="text" name="shout" size="15"><br>
<input type="submit" value="Send!">
</center>
<hr>
<?php

  $fp = fopen("shouts.txt","r");
  while(($shout = fgetcsv($fp,1000,":")) !== FALSE) {
   
     echo "<b>".$shout[0]." : </b>".$shout[1]."<br>";
  }

  fclose($fp);
?>
</p>

</form>
</td></tr>
</table>

<//------- Finish copying from this point -------->

</body>
</html>






And heres the 'Add shout.php' one

code:
<?php

  /* -------------------------------------------------------

                           ShoutBox v1.1
                     By: Mark Lethbridge, 2007

     -------------------------------------------------------

     Install notes:
     Only thing that needs to be changed in this file is the
     line: $shoutbox_page = "index.php";
     You simple change "index.php" into the correct page
     that the shoutbox will appear in.

     -------------------------------------------------------- */

   $fp = fopen("shouts.txt","a");
   fwrite($fp,$_POST['user'].":".$_POST['shout']."\n");
   fclose($fp);
   $shoutbox_page = "http://pps3.awardspace.com/Shoutbox/index.php";
   header("Location: $shoutbox_page");

?>

RE: PHP Shoutbox help by Nathan on 06-08-2007 at 06:35 PM

Why dont you just use a mysql one?


RE: PHP Shoutbox help by M73A on 06-08-2007 at 06:37 PM

quote:
Originally posted by Napbree
Why dont you just use a mysql one?
free hosting account etc, didnt really want to use up the mysql database just for a shoutbox
RE: PHP Shoutbox help by Nathan on 06-08-2007 at 06:49 PM

You would use 1 table in that database.  Not a big deal.


RE: PHP Shoutbox help by M73A on 06-08-2007 at 06:54 PM

quote:
Originally posted by Napbree
You would use 1 table in that database.  Not a big deal.
well i still want to use this one so how can i make it new ontop :P
RE: PHP Shoutbox help by Veggie on 06-08-2007 at 07:13 PM

code:
<?
$file='file.txt';
$newcontent = 'blah';
$new = $newcontent . "\n" . file_get_contents($file);
$fp =fopen($file,'w');
fwrite($fp, $new);
fclose($fp);

?>

you cant use fopen to write to the top without over writing. Use this to get the file contents add it to the end of the new string then save

RE: PHP Shoutbox help by M73A on 06-08-2007 at 07:14 PM

lol sorry, im a complete php newb... where about would it have to go, and in what one, lol

quote:
Originally posted by M73A
s the 'Add shout.php' one

    code:
    <?php

      /* -------------------------------------------------------

                               ShoutBox v1.1
                         By: Mark Lethbridge, 2007

         -------------------------------------------------------

         Install notes:
         Only thing that needs to be changed in this file is the
         line: $shoutbox_page = "index.php";
         You simple change "index.php" into the correct page
         that the shoutbox will appear in.

         -------------------------------------------------------- */

$file='file.txt';
$newcontent = 'blah';
$new = $newcontent . "\n" . file_get_contents($file);
$fp =fopen($file,'w');
fwrite($fp, $new);
fclose($fp);

       fwrite($fp,$_POST['user'].":".$_POST['shout']."\n");
       fclose($fp);
       $shoutbox_page = "http://pps3.awardspace.com/Shoutbox/index.php";
       header("Location: $shoutbox_page");

    ?>






----------->Click Here To Sign Up To Nintendo.com VIP<-----------

like that?
RE: RE: PHP Shoutbox help by Veggie on 06-08-2007 at 07:16 PM

code:
<?php

  /* -------------------------------------------------------

                           ShoutBox v1.1
                     By: Mark Lethbridge, 2007

     -------------------------------------------------------

     Install notes:
     Only thing that needs to be changed in this file is the
     line: $shoutbox_page = "index.php";
     You simple change "index.php" into the correct page
     that the shoutbox will appear in.

     -------------------------------------------------------- */

$file='shouts.txt';
$new = $_POST['user'].":".$_POST['shout'] . "\n" . file_get_contents($file);
$fp =fopen($file,'w');
fwrite($fp, $new);
fclose($fp);
   $shoutbox_page = "http://pps3.awardspace.com/Shoutbox/index.php";
   header("Location: $shoutbox_page");

?>


I have no idea if that will work btw
RE: PHP Shoutbox help by Veggie on 06-08-2007 at 07:18 PM

this also isnt very secure, the input really should be striped of bad things, but i dont have time to do that for you.


RE: PHP Shoutbox help by M73A on 06-08-2007 at 07:19 PM

you rock veggie!! works perfect (L)! :D


RE: PHP Shoutbox help by Ezra on 06-08-2007 at 07:24 PM

You could also just use:

code:
fseek ($handle, 0);
To place the pointer at the beginning of the file ;)
RE: PHP Shoutbox help by M73A on 06-08-2007 at 07:25 PM

quote:
Originally posted by Veggie
this also isnt very secure, the input really should be striped of bad things, but i dont have time to do that for you.
lol bad things? :|

tell me what you meant and ill look into that one;)
RE: RE: PHP Shoutbox help by vikke on 06-08-2007 at 07:43 PM

quote:
Originally posted by M73A
quote:
Originally posted by Veggie
this also isnt very secure, the input really should be striped of bad things, but i dont have time to do that for you.
lol bad things? :|

tell me what you meant and ill look into that one;)

Evil javascript code for example.
RE: PHP Shoutbox help by Nathan on 06-08-2007 at 07:44 PM

You could delete files and directorys with that.


RE: PHP Shoutbox help by Ezra on 06-08-2007 at 07:44 PM

quote:
Originally posted by vikke
Evil javascript code for example.

A simple
code:
$stripped = strip_tags ($str);

should be enough, no SQL used so no SQL Injection ;)
RE: PHP Shoutbox help by M73A on 06-08-2007 at 09:30 PM

:|

er, is it hard to put in like $find javascript $ Delete


RE: PHP Shoutbox help by ShawnZ on 06-08-2007 at 09:49 PM

quote:
Originally posted by M73A
:|

er, is it hard to put in like $find javascript $ Delete

wtf o.o
RE: PHP Shoutbox help by Ezra on 06-08-2007 at 09:52 PM

quote:
Originally posted by M73A
$find javascript $ Delete

The function I showed you removes (or at least tries to) remove all HTML + PHP from the input string. That includes javascript and should leave clean text.
RE: PHP Shoutbox help by M73A on 06-08-2007 at 10:07 PM

quote:
Originally posted by Ezra
quote:
Originally posted by M73A
$find javascript $ Delete

The function I showed you removes (or at least tries to) remove all HTML + PHP from the input string. That includes javascript and should leave clean text.

oh crap sorry, i thought that was an example of something malicious:P sorreh ^_^


hm i tried putting it in but it didnt work. where would you advise to put it?
RE: PHP Shoutbox help by Ezra on 06-08-2007 at 10:23 PM

quote:
Originally posted by M73A
hm i tried putting it in but it didnt work. where would you advise to put it?

code:
$fp = fopen("shouts.txt","a");
   fseek($fp, 0);
   fwrite($fp,strip_tags($_POST['user']).":".strip_tags($_POST['shout'])."\n");
   fclose($fp);
   $shoutbox_page = "http://pps3.awardspace.com/Shoutbox/index.php";
   header("Location: $shoutbox_page");

Bold parts are the changed parts.

I also added my other suggestion about your add to top of the file problem, the fseek will put the pointer at the start of the file.

EDIT: However, I'm not sure how fwrite writes the string to it, if it will overwrite what is already there or it will add it before the rest, I actually think it's the first, so it might not be that helpful :P, but the strip_tags are unaffected, so you can still copy that part :D

EDIT2: Indeed I was right, there is no prepend mode, so with fseek the contents will be overwritten, nevermind that then, just use the other suggestions about the prepending.
RE: PHP Shoutbox help by M73A on 06-08-2007 at 10:32 PM

YAYYYY

Thanks :D:D:D

come here asking about locations, and get extra help about security!!

Thanks buddy!


RE: PHP Shoutbox help by Nathan on 06-09-2007 at 10:08 AM

Where is it btw, i'd like to see it :P


RE: PHP Shoutbox help by Felu on 06-09-2007 at 10:27 AM

If a username has ':' or a shout contains ':', this might not work properly :P.


RE: PHP Shoutbox help by M73A on 06-09-2007 at 06:13 PM

quote:
Originally posted by Napbree
Where is it btw, i'd like to see it :P
http://pps3.awardspace.com/ its there, mentality got me to help him make some ps3 clan thing, personally i think he should buy me a ps3:P
RE: PHP Shoutbox help by Nathan on 06-09-2007 at 07:03 PM

Jesus, thats very unsecure, and spam not proof


RE: PHP Shoutbox help by M73A on 06-09-2007 at 09:42 PM

quote:
Originally posted by Napbree
Jesus, thats very unsecure, and spam not proof
lol yep ^_^
RE: PHP Shoutbox help by Nathan on 06-09-2007 at 10:11 PM

add this:

code:
if(empty($_POST['shout'])) {
echo "Sorry its empty";
}


RE: PHP Shoutbox help by M73A on 06-09-2007 at 10:14 PM

hm, i did... and it seems to work / go funny

code:
$file='shouts.txt';
if(empty($_POST['shout'])) {
echo "Sorry its empty";
}
$new = strip_tags($_POST['user']).":".strip_tags($_POST['shout']) . "\n" . file_get_contents($file);
$fp =fopen($file,'w');
fwrite($fp, $new);
fclose($fp);
   $shoutbox_page = "http://pps3.awardspace.com/Shoutbox/index.php";
   header("Location: $shoutbox_page");

?>

RE: PHP Shoutbox help by Ezra on 06-09-2007 at 11:56 PM

You can't use a header re-locate after outputting anything to the page. Always use header before any echo and other outputting functions.