Advertisement
gitlez

Simple Chat Script

Jun 15th, 2011
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.69 KB | None | 0 0
  1. <?php
  2. $chat = "./chatlogs.txt";
  3. $strdata = $_POST['msg'];
  4. $username = $_POST['sn'];
  5. $maxLines = 15;
  6. $message = '';
  7. function nameColor($name){
  8.     return substr(md5($name),0,6);
  9. }
  10.  
  11. if(!file_exists($chat)){
  12.     file_put_contents($chat, '');
  13. }
  14.  
  15. $chatLines = explode(PHP_EOL,file_get_contents($chat));
  16. if(($linesOver = (count($chatLines) - $maxLines)) > 0){
  17.     $chatLines = array_slice($chatLines,$linesOver);
  18. }
  19. $chatLines = implode(PHP_EOL,$chatLines);
  20.  
  21.  
  22. if(strlen($_POST['submit']) > 0){
  23.     if (strlen(trim($strdata,' .')) > 0 && strlen($username) > 0){
  24.         $chatLines .= '<span style="font-weight: bold;color: ' . nameColor($username) . ';">' . $username . '</span>: ' .  $strdata . "<br />" . PHP_EOL;
  25.         $fh = fopen($chat,'w');
  26.         fwrite($fh, $chatLines);
  27.         fclose($fh);
  28.     }else if(strlen($username) === 0){
  29.         $message =  "<font color=\"FF0000\">You must enter a username!</font>";
  30.     }
  31. }
  32. ?>
  33. <html>
  34.     <head>
  35.         <title>Simple Chat Page</title>
  36.     </head>
  37. <body onLoad="document.getElementById('msg').focus();">
  38.     <?php
  39.         echo $chatLines . PHP_EOL . '<hr />' . PHP_EOL . $message . PHP_EOL;
  40.     ?>
  41.     <form method="POST" autocomplete="off">
  42.         <?php
  43.             if(strlen($username) > 0){
  44.                 echo '<input type="hidden" name="sn" value="' . $username . '">';
  45.             }else{
  46.                 echo 'Name: <input type="text" size="7" name="sn" value="' . $username . '">';
  47.             }
  48.         ?>
  49.         Message: <input type="text" id="msg" name="msg">
  50.         <input type="submit" name="submit" value="Send">
  51.         <input type="submit" name="submit" value="Check for new messages">
  52.     </form>
  53. </body>
  54. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement