Two functions in the same time PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fos
Forum Newbie
Posts: 3
Joined: Mon Sep 07, 2009 4:50 pm

Two functions in the same time PHP

Post by fos »

Dear,

this is my first post so... hello world !
Well, I created a simple chat server in C++ that is running on my server (debian 5.0.2). I can connect to my server through telnet but I want to create a graphic front-end in php. I've founded a good class "telnet_class.php" to manage the connection and it works but when I lunch a member function of this class the system wait for the timeout (5 seconds) and only after this it draw the web page. Is there a method to lunch this function in a separate instance. (I read something about exec but I haven't understood how to use it).
Sorry for my poor English.

Thanks, Francesco
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Two functions in the same time PHP

Post by Eran »

you should probably flush the output buffer during the process - http://us2.php.net/manual/en/function.flush.php
read the notes, its sometimes not enough on its own
fos
Forum Newbie
Posts: 3
Joined: Mon Sep 07, 2009 4:50 pm

Re: Two functions in the same time PHP

Post by fos »

I found some information about the flush and the exec function but I don't understand how to lunch a function that is in the php script; If I lunch the function "whoami" it works, but it's a function of the bash.
I'll post you the code:

Code: Select all

 
<?php
 
include_once ('telnet_class.php');
 
function form_show() {
 
    print "<div align=\"center\"><b>Chat client</b></div>";
 
    print "<form name=\"form1\" method=\"post\" action=\"telnet.php\">
        <input type=\"hidden\" name=\"op\" value=\"telnet\">
        <br> port: <input type=\"text\" name=\"port\" value=\"2020\" size=\"20\" maxlength=\"5\">
        <br> nick: <input type=\"text\" name=\"nick\" size=\"20\" maxlength=\"8\">
        <br> timeout: <input type=\"text\" name=\"timeout\" size=\"16\" value=\"10\">
        <br> <input type=\"submit\" name=\"submit\" value=\"submit\">
    </form>";
 
}
 
 
function telnet($port, $nick, $timeout) {
 
    print "
    <br> <b>port:</b> $port
    <br> <b>nick:</b> $nick
    <br> <b>timeout:</b> $timeout sec";
 
    $conn = new telnet;
    
    $conn->set_host("localhost");
    $conn->set_port($port);
    $conn->connect();
 
    //for ($i = 0; $i < 10000; $i++) if (!($i % 1000))  print "Staus: $i";  
    
    $conn->write($nick);
 
    $ps = run_in_background("whoami");
    while(is_process_running($ps))
   {
     echo(" . ");
       ob_flush(); flush();
            sleep(1);
   }
        
    //$conn->read_to("\021"); //Istruction that block the script
    //$varaible = $conn->get_buffer();
    //echo $varaible;
 
}
 
function run_in_background($Command, $Priority = 0) {
    if($Priority)
    $PID = shell_exec("nohup nice -n $Priority $Command 2> /dev/null & echo $!");
    else
      $PID = shell_exec("nohup $Command 2> /dev/null & echo $!");
      
   return($PID);
}
 
function is_process_running($PID) {
    exec("ps $PID", $ProcessState);
   return(count($ProcessState) >= 2);
}
 
switch($_POST[op]) {
    case "telnet":
        telnet($_POST[port], $_POST[nick], $_POST[timeout]);
        break;
    default: form_show();
}
    
?>
 
Thanks
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Two functions in the same time PHP

Post by Eran »

By flushing the output buffer, you can send output to the client while the script is still running, which is what I think you want. This is different from exec which is used to execute an external program.
fos
Forum Newbie
Posts: 3
Joined: Mon Sep 07, 2009 4:50 pm

Re: Two functions in the same time PHP

Post by fos »

Flush() works great ! But now there's an other problem. How can I create an interface where the user can scroll the message received like a ListView in gtkmm.
Post Reply