unitistar

Chat Php/Jquery

May 20th, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. create chat.txt
  2.  
  3. index.html
  4. <!DOCTYPE HTML>
  5. <html>
  6. <meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />
  7. <title>Chat</title>
  8. <head>
  9. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  10. <script type="text/javascript" src="core.js"></script>
  11. <script type="text/javascript">
  12.  
  13.   // ask user for name WITH popup prompt
  14.   var name = prompt("Enter your chat name:", "Guest");
  15.  
  16.  // default name is 'Guest'
  17.  if (!name || name === ' ') {
  18.    name = "Guest";
  19.  }
  20.  
  21.  // strip tags
  22.  name = name.replace(/(<([^>]+)>)/ig,"");
  23.  
  24.  // display name on page
  25.  $("#name-area").html("You are: <span>" + name + "</span>");
  26.  
  27.  // kick off chat
  28.  var chat =  new Chat();
  29.  
  30.  $(function() {
  31.  
  32.     chat.getState();
  33.  
  34.     // watch textarea for key presses
  35.     $("#sendie").keydown(function(event) {  
  36.  
  37.         var key = event.which;  
  38.  
  39.         //all keys including return.
  40.         if (key >= 33) {
  41.  
  42.             var maxLength = $(this).attr("maxlength");
  43.             var length = this.value.length;  
  44.  
  45.             // don't allow new content if length is maxed out
  46.             if (length >= maxLength) {
  47.                 event.preventDefault();
  48.             }
  49.         }
  50.                                                                                                     });
  51.     // watch textarea for release of key press
  52.     $('#sendie').keyup(function(e) {  
  53.  
  54.        if (e.keyCode == 13) {
  55.  
  56.              var text = $(this).val();
  57.              var maxLength = $(this).attr("maxlength");
  58.              var length = text.length;
  59.  
  60.              // send
  61.              if (length <= maxLength + 1) {
  62.                chat.send(text, name);
  63.                $(this).val("");
  64.              } else {
  65.                $(this).val(text.substring(0, maxLength));
  66.              }
  67.        }
  68.     });
  69.  });
  70. </script>
  71. <body onload="setInterval('chat.update()', 1000)">
  72. </head>
  73. </body>
  74. <div id="page-wrap">
  75.  
  76.    <h2>Chat</h2>
  77.  
  78.    <p id="name-area"></p>
  79.  
  80.    <div id="chat-wrap"><div id="chat-area"></div></div>
  81.  
  82.    <form id="send-message-area">
  83.        <p>Your message: </p>
  84.        <textarea id="sendie" maxlength = '100'></textarea>
  85.    </form>
  86.  
  87. </div>
  88.  
  89.  
  90. core.js
  91.  
  92. function Chat () {
  93.    this.update = updateChat;
  94.    this.send = sendChat;
  95.    this.getState = getStateOfChat;
  96. }
  97.  
  98. //gets the state of the chat
  99. function getStateOfChat(){
  100.  if(!instanse){
  101.     instanse = true;
  102.     $.ajax({
  103.         type: "POST",
  104.         url: "process.php",
  105.         data: {
  106.               'function': 'getState',
  107.            'file': file
  108.            },
  109.         dataType: "json",
  110.         success: function(data){
  111.           state = data.state;
  112.           instanse = false;
  113.         },
  114.      });
  115.  }
  116. }
  117.  
  118. //Updates the chat
  119. function updateChat(){
  120.   if(!instanse){
  121.     instanse = true;
  122.       $.ajax({
  123.         type: "POST",
  124.         url: "process.php",
  125.         data: {
  126.               'function': 'update',
  127.            'state': state,
  128.            'file': file
  129.            },
  130.         dataType: "json",
  131.         success: function(data){
  132.           if(data.text){
  133.            for (var i = 0; i < data.text.length; i++) {
  134.                            $('#chat-area').append($("<p>"+ data.text[i] +"</p>"));
  135.                        }
  136.           }
  137.           document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight;
  138.           instanse = false;
  139.           state = data.state;
  140.         },
  141.      });
  142.   }
  143.   else {
  144.     setTimeout(updateChat, 1500);
  145.   }
  146. }
  147.  
  148. //send the message
  149. function sendChat(message, nickname)
  150. {
  151.    updateChat();
  152.    $.ajax({
  153.       type: "POST",
  154.       url: "process.php",
  155.       data: {
  156.             'function': 'send',
  157.          'message': message,
  158.          'nickname': nickname,
  159.          'file': file
  160.         },
  161.       dataType: "json",
  162.       success: function(data){
  163.         updateChat();
  164.       },
  165.    });
  166. }
  167.  
  168.  
  169. process.php
  170.  
  171. <?php
  172.  
  173.    $function = $_POST['function'];
  174.  
  175.    $log = array();
  176.  
  177.    switch($function) {
  178.  
  179.       case('getState'):
  180.           if (file_exists('chat.txt')) {
  181.               $lines = file('chat.txt');
  182.           }
  183.           $log['state'] = count($lines);
  184.           break;  
  185.  
  186.       case('update'):
  187.          $state = $_POST['state'];
  188.          if (file_exists('chat.txt')) {
  189.             $lines = file('chat.txt');
  190.          }
  191.          $count =  count($lines);
  192.          if ($state == $count){
  193.             $log['state'] = $state;
  194.             $log['text'] = false;
  195.          } else {
  196.             $text= array();
  197.             $log['state'] = $state + count($lines) - $state;
  198.             foreach ($lines as $line_num => $line) {
  199.                 if ($line_num >= $state){
  200.                       $text[] =  $line = str_replace("\n", "", $line);
  201.                 }
  202.             }
  203.             $log['text'] = $text;
  204.          }
  205.  
  206.          break;
  207.  
  208.       case('send'):
  209.            $nickname = htmlentities(strip_tags($_POST['nickname']));
  210.         $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
  211.         $message = htmlentities(strip_tags($_POST['message']));
  212.         if (($message) != "\n") {
  213.           if (preg_match($reg_exUrl, $message, $url)) {
  214.              $message = preg_replace($reg_exUrl, '<a href="'.$url[0].'" target="_blank">'.$url[0].'</a>', $message);
  215.           }
  216.              fwrite(fopen('chat.txt', 'a'), "<span>". $nickname . "</span>" . $message = str_replace("\n", " ", $message) . "\n");
  217.         }
  218.         break;
  219.    }
  220.    echo json_encode($log);
  221. ?>
Advertisement
Add Comment
Please, Sign In to add comment