script livre d'or

mistertitan

Membre expert
Club iGen
4 Septembre 2003
1 175
132
43
Garches
www.400iso.org
j'ai une petite requète avec mon script livre d'or

Bloc de code:
        <form action="livre.php3"
              name="messager"
              method="post">
          Nom :
          <br />
          <input type="text"
                name="person"
                size="50" />
          <br />
          Message :
          <br />
          <textarea name="message"
                cols="50">
</textarea>
          <br />
          <input name="submit"
                type="submit"
                value="Ecrire"
                border="0" /><input name="reset"
                type="reset"
                value="Annuler"
                border="0" />
        </form><?
        #       the $chat_file_ok is a txt file (or whatever else) I use for messages storage
        $chat_file_ok = "../msg.txt";

        #       $chat_lenght is the number of messages displayed
        $chat_lenght = 50;

        /*      $max_file_size is the maximum file size the msg.txt file can reach
                assuming that any chatter doesn't write a message longer than
                $max_single_msg_lenght (this case: 100,000 bytes = 100Kb)               */
        $max_single_msg_lenght = 10000;
        $max_file_size = $chat_lenght * $max_single_msg_lenght;

        # check if file size is over maximum    (set with $max_file_size )
        $file_size= filesize($chat_file);

        /*      if file size is more than allowed then
                                reads last $chat_lenght messages (last lines of msg.txt file)
                                and stores them in $lines array
                                then deletes the "old" msg.txt file and create a new msg.txt
                                pushing the "old" messages stored in $lines array into the
                                "new" msg.txt file using $msg_old.
                        Note: this is done in order to avoid huge msg.txt file size.            */
                                
        if ($file_size > $max_file_size) {
                # reads file and stores each line $lines array elements
                $lines = file($chat_file_ok);
                # get number of lines
                $a = count($lines);
                $u = $a - $chat_lenght;
                for($i = $a; $i >= $u ;$i--)
                {
                        $msg_old = $lines[$i] . $msg_old;
                }
                $deleted = unlink($chat_file_ok);
                $fp = fopen($chat_file_ok, "a+");
                $fw = fwrite($fp, $msg_old);
                fclose($fp);
        }

        /* the following is because every message has to be
         placed into one single line in the msg.txt file.
         You can render \n (new lines) with "<br>" html tag anyway.     */

        $msg = str_replace ("\n"," ", $message);

        /*      if the user writes something...
                the new message is appended to the msg.txt file
                REMEMBER: the message is appended, hence, if
                you want the last message to be displayed as the
                first one, you have to
                        1. store the lines (messages) into the array
                        2. read the array in reverse order
                        3. post the messages in the output file (the chat)
                        
         I added these three lines in order to avoid buggy html code and slashes        */
        $msg = str_replace ("\n"," ", $message);
        $msg = str_replace ("<", " ", $msg);
        $msg = str_replace (">", " ", $msg);
        $msg = stripslashes ($msg);




        if ($msg != ""){
        $fp = fopen($chat_file_ok, "a+");
        $fw = fwrite($fp, "\n<small><small>".date("d/m/Y H")."h".date("i")." </small></small><br>- $person : &laquo; $msg &raquo;<br><br>");
        fclose($fp);
        }

        $lines = file($chat_file_ok);
        $a = count($lines);

        $u = $a - $chat_lenght;

        #       reads the array in reverse order and outputs to chat
        for($i = $a; $i >= $u ;$i--){
                        echo $lines[$i].""; # pour ajouter qqchose entre les lignes : echo $lines[$i]."<hr>";
                }

        ?>

C'est un pote que je ne vois plus qui me l'avais filé.
et quand on écrit un message sur 2 lignes, il remet tout sur une seule ligne.
J'aimerais savoir qi quelqu'un saurait comment le modifier pour que un saut a la ligne se transforme en <br>
 
Arf bon j'ai pas la motivation de m'attaquer à ton code le principe est d'utiliser la fonction nl2br()

Moi par exemple lorsque j'ai un message je fais deux petite chose qu'on pourrait penser inutiles mais bon ça sert en fait :mouais:

Soit la fonction :


function securehtml($content)
{
$content = ereg_replace("<","&lt;",$content);
$content = ereg_replace(">","&gt;",$content);
$content = ereg_replace("&lt;br&gt;","<br />",$content);
$content = ereg_replace("&lt;br /&gt;","<br />",$content);
return $content;
}

qui évite les codes html dans les messages ;)

et derrière


$message= nl2br(securehtml($message));

qui transforme tes \n en <br /> et supprime tout code autre ne html (pour l'html t'es pas obligé mais c'est plus propre)
 
Bon il est tard mais je me suis motivé poiur regarder :

3. post the messages in the output file (the chat)

I added these three lines in order to avoid buggy html code and slashes */
$msg = str_replace ("\n"," ", $message);
$msg = str_replace ("<", " ", $msg);
$msg = str_replace (">", " ", $msg);
$msg = stripslashes ($msg);

Ici à la place tu mets
$msg = str_replace ("\n","<br />", $message);
$msg = str_replace ("<", "[FONT=Garamond,mon]&lt;
", $msg);
$msg = str_replace (">", "
[/FONT]&gt;", $msg);
$msg = stripslashes ($msg);
 
Il me semble que ça serait mieux d'utiliser une fonction inclue dans PHP pour traduire certains caractères utilisés en HTML. Comme par exemple, la fonction htmlentities().