Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End php

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #235963
    akshaya
    Participant

    in Building a jQuery/PHP Powered Chat Room i was going through some problem in php file. In update case, i’ve changed the name of the with a variable it is not updating in the webpage. but when i give the file name it is displaying i want to develop a dynamic chat room so help me to do so.
    code:
    <?php

    $function = $_POST['function'];
    $chatting=$_POST['nickname'].'txt';
    $log = array();
    
    switch($function) {
    
         case('getState'):
    
             if(file_exists($chatting)){
               $lines = file($chatting);
             }
             else{
                $chat = fopen($$chatting, "w") or die("Unable to open file!");
                chmod($chat, 0777);
                $lines = file($chatting);
             }
             $log['state'] = count($lines); 
             break; 
    
         case('update'):
            $state = $_POST['state'];
            $$chatting = $_POST['nickname'].'txt';
            if(file_exists($chatting)){
               $lines = file($chatting);
             }
             else{
                $chat = fopen($chatting, "w") or die("Unable to open file!");
                chmod($chat, 0777);
                $lines = file($chatting);
             }
             $count =  count($lines);
             if($state == $count){
                 $log['state'] = $state;
                 $log['text'] = false;
    
                 }
                 else{
                     $text= array();
                     $log['state'] = $state + count($lines) - $state;
                     foreach ($lines as $line_num =&gt; $line)
                       {
                           if($line_num &gt;= $state){
                         $text[] =  $line = str_replace("\n", "", $line);
                           }
    
                        }
                     $log['text'] = $text; 
                 }
    
             break;
    
         case('send'):
          $nickname = htmlentities(strip_tags($_POST['nickname']));
             $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
              $message = htmlentities(strip_tags($_POST['message']));
         if(($message) != "\n"){
    
             if(preg_match($reg_exUrl, $message, $url)) {
                $message = preg_replace($reg_exUrl, '<a href="'.$url[0].'">'.$url[0].'</a>', $message);
                } 
    
    
             fwrite(fopen($$chatting, 'a'), "<span>". $nickname . "</span>" . $message = str_replace("\n", " ", $message) . "\n"); 
         }
             break;
    
    }
    
    echo json_encode($log);
    

    ?>
    if i run this code i couldn’t display what the user is typing
    please help me out.

    #236197
    danallen
    Participant

    Have you validated your basic design? Is jquery/php chat going to get what you need for sure? Neither jquery nor php are chat systems with the communications and user management components built in. If you are doing this for fun, no problem, but if someone is planning to use what you are making, you might be putting yourself in a bad position.

    This information is preliminary.

    From research I needed to do, I found a lot of information indicating that php is not a good choice for building a chat room, unless the number of concurrent participants is no more than 20. The reason is that the overhead of running a php script every second or so for every participant puts a big load on the server. A good chat system can be embeded within a php site, but the chat itself is going to work better if it is built with one of the existing chat platforms, such as jabber, irc, and quite a few others. You need to know which protocol you are going to use, and start with a package that gives you a starting point. Trying to build this totally from scratch without a chat platform is going to be long road..

    The way to build a chat facility is to start with one of the basic chat services that run a daemon. You probably will need to have your own vps or dedicated server, not shared hosting.

    Hopefully, I have this all wrong.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.