Codice PHP:
	
<?php 
define ('DBPATH','localhost'); 
define ('DBUSER','****'); 
define ('DBPASS','*********'); 
define ('DBNAME','miodatabase'); 
session_start(); 
global $dbh; 
$dbh = mysql_connect(DBPATH,DBUSER,DBPASS); 
mysql_selectdb(DBNAME,$dbh); 
if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); }  
if ($_GET['action'] == "sendchat") { sendChat(); }  
if ($_GET['action'] == "closechat") { closeChat(); }  
if ($_GET['action'] == "startchatsession") { startChatSession(); }  
if (!isset($_SESSION['chatHistory'])) { 
    $_SESSION['chatHistory'] = array();     
} 
if (!isset($_SESSION['openChatBoxes'])) { 
    $_SESSION['openChatBoxes'] = array();     
} 
function faccine($items){ 
  $items_ok = str_replace(":)", "[img]smile.gif[/img]", $items); 
  $items_ok = str_replace(":-)", "[img]smile.gif[/img]", $items_ok); 
  $items_ok = str_replace(":(", "<img src=\"triste.gif\">", $items_ok); 
  $items_ok = str_replace(":-(", "<img src=\"triste.gif\">", $items_ok); 
  $items_ok = str_replace(":P", "<img src=\"linguaccia.gif\">", $items_ok); 
  $items_ok = str_replace(":p", "<img src=\"linguaccia.gif\">", $items_ok); 
  $items_ok = str_replace(":-p", "<img src=\"linguaccia.gif\">", $items_ok); 
  $items_ok = str_replace(":-P", "<img src=\"linguaccia.gif\">", $items_ok); 
  $items_ok = str_replace(":D", "<img src=\"grandesmile.gif\">", $items_ok); 
  $items_ok = str_replace(":-D", "<img src=\"grandesmile.gif\">", $items_ok); 
  $items_ok = str_replace(":o", "<img src=\"stupito.gif\">", $items_ok); 
  $items_ok = str_replace(":O", "<img src=\"stupito.gif\">", $items_ok); 
  $items_ok = str_replace(":-o", "<img src=\"stupito.gif\">", $items_ok); 
  $items_ok = str_replace(":-O", "<img src=\"stupito.gif\">", $items_ok); 
  $items_ok = str_replace(";)", "<img src=\"occhiolino.gif\">", $items_ok); 
  $items_ok = str_replace(";-)", "<img src=\"occhiolino.gif\">", $items_ok); 
  $items_ok = str_replace(":v", "<img src=\"pacman.gif\">", $items_ok); 
  $items_ok = str_replace(":V", "<img src=\"pacman.gif\">", $items_ok); 
  $items_ok = str_replace(":-v", "<img src=\"pacman.gif\">", $items_ok); 
  $items_ok = str_replace(":-V", "<img src=\"pacman.gif\">", $items_ok); 
  $items_ok = str_replace(">:( ", "<img src=\"occhistrizzati.gif\">", $items_ok); 
  $items_ok = str_replace(">:-(", "<img src=\"occhistrizzati.gif\">", $items_ok); 
  $items_ok = str_replace(":/", "<img src=\"perplesso.gif\">", $items_ok); 
  $items_ok = str_replace(":-/", "<img src=\"perplesso.gif\">", $items_ok); 
  $items_ok = str_replace(":'(", "<img src=\"lacrima.gif\">", $items_ok); 
  $items_ok= str_replace(":'-(", "<img src=\"lacrima.gif\">", $items_ok); 
  $items_ok = str_replace("8)", "<img src=\"occhialichiari.gif\">", $items_ok); 
  $items_ok = str_replace("8-)", "<img src=\"occhialichiari.gif\">", $items_ok); 
  $items_ok = str_replace("B|", "<img src=\"occhialiscuri.gif\">", $items_ok); 
  $items_ok = str_replace("B-|", "<img src=\"occhialiscuri.gif\">", $items_ok); 
  $items_ok = str_replace("b|", "<img src=\"occhialiscuri.gif\">", $items_ok); 
  $items_ok = str_replace("b-|", "<img src=\"occhialiscuri.gif\">", $items_ok); 
  $items_ok = str_replace("<3", "<img src=\"cuore.gif\">", $items_ok); 
  $items_ok = str_replace("3:)", "<img src=\"diavoletto.gif\">", $items_ok); 
  $items_ok = str_replace("3:-)", "<img src=\"diavoletto.gif\">", $items_ok); 
  $items_ok = str_replace("o:)", "<img src=\"angioletto.gif\">", $items_ok); 
  $items_ok = str_replace("O:)", "<img src=\"angioletto.gif\">", $items_ok); 
  $items_ok = str_replace("0:)", "<img src=\"angioletto.gif\">", $items_ok); 
  $items_ok = str_replace("o:-)", "<img src=\"angioletto.gif\">", $items_ok); 
  $items_ok = str_replace("O:-)", "<img src=\"angioletto.gif\">", $items_ok); 
  $items_ok = str_replace("0:-)", "<img src=\"angioletto.gif\">", $items_ok); 
  $items_ok = str_replace("o.O", "<img src=\"occhiopiùgrande.gif\">", $items_ok); 
  $items_ok = str_replace(">:o", "<img src=\"felicissimostrizzati.gif\">", $items_ok); 
  $items_ok = str_replace(">:O", "<img src=\"felicissimostrizzati.gif\">", $items_ok); 
  $items_ok = str_replace(">:-o", "<img src=\"felicissimostrizzati.gif\">", $items_ok); 
  $items_ok = str_replace(">:-O", "<img src=\"felicissimostrizzati.gfi\">", $items_ok); 
  $items_ok = str_replace(":3", "<img src=\"perplesso.gif\">", $items_ok); 
  $items_ok = str_replace(":-3", "<img src=\"perplesso.gif\">", $items_ok); 
  $items_ok = str_replace("*_*", "<img src=\"indifferente.gif\">", $items_ok); 
  $items_ok = str_replace("*-*", "<img src=\"indifferente.gif\">", $items_ok); 
  $items_ok = str_replace("-_-", "<img src=\"indifferente.gif\">", $items_ok); 
   
  return $items_ok; 
} 
function chatHeartbeat() { 
     
    $sql = "select * from chat where (chat.to = '".mysql_real_escape_string($_SESSION['utente'])."' AND recd = 0) order by id ASC"; 
    $query = mysql_query($sql); 
    $items = ''; 
    $chatBoxes = array(); 
    while ($chat = mysql_fetch_array($query)) { 
        if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) { 
            $items = $_SESSION['chatHistory'][$chat['from']]; 
        } 
        $chat['message'] = sanitize($chat['message']); 
        $items .= <<<EOD 
                       { 
            "s": "0", 
            "f": "{$chat['from']}", 
            "m": "{$chat['message']}" 
       }, 
EOD; 
    if (!isset($_SESSION['chatHistory'][$chat['from']])) { 
        $_SESSION['chatHistory'][$chat['from']] = ''; 
    } 
    $_SESSION['chatHistory'][$chat['from']] .= <<<EOD 
                           { 
            "s": "0", 
            "f": "{$chat['from']}", 
            "m": "{$chat['message']}" 
       }, 
EOD; 
         
        unset($_SESSION['tsChatBoxes'][$chat['from']]); 
        $_SESSION['openChatBoxes'][$chat['from']] = $chat['sent']; 
    } 
    if (!empty($_SESSION['openChatBoxes'])) { 
    foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) { 
        if (!isset($_SESSION['tsChatBoxes'][$chatbox])) { 
            $now = time()-strtotime($time); 
            $time = date('g:iA M dS', strtotime($time)); 
            $message = "Inviato: $time"; 
            if ($now > 180) { 
                $items .= <<<EOD 
{ 
"s": "2", 
"f": "$chatbox", 
"m": "{$message}" 
}, 
EOD; 
    if (!isset($_SESSION['chatHistory'][$chatbox])) { 
        $_SESSION['chatHistory'][$chatbox] = ''; 
    } 
    $_SESSION['chatHistory'][$chatbox] .= <<<EOD 
        { 
"s": "2", 
"f": "$chatbox", 
"m": "{$message}" 
}, 
EOD; 
            $_SESSION['tsChatBoxes'][$chatbox] = 1; 
        } 
        } 
    } 
} 
    $sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['utente'])."' and recd = 0"; 
    $query = mysql_query($sql); 
    if ($items != '') { 
        $items = substr($items, 0, -1); 
    } 
header('Content-type: application/json'); 
?> 
{ 
        "items": [ 
            <?php echo ".faccine($items).";?> 
        ] 
} 
<?php 
            exit(0); 
} 
function chatBoxSession($chatbox) { 
     
    $items = ''; 
     
    if (isset($_SESSION['chatHistory'][$chatbox])) { 
        $items = $_SESSION['chatHistory'][$chatbox]; 
    } 
    return "faccine.($items)."; 
} 
function startChatSession() { 
    $items = ''; 
    if (!empty($_SESSION['openChatBoxes'])) { 
        foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) { 
            $items .= chatBoxSession($chatbox); 
        } 
    } 
    if ($items != '') { 
        $items = substr($items, 0, -1); 
    } 
header('Content-type: application/json'); 
?> 
{ 
        "username": "<?php echo $_SESSION['utente'];?>", 
        "items": [ 
            <?php echo ".faccine($items).";?> 
        ] 
} 
<?php 
    exit(0); 
} 
function sendChat() { 
    $from = $_SESSION['utente']; 
    $to = $_POST['to']; 
    $message = $_POST['message']; 
    $_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time()); 
     
    $messagesan = sanitize($message); 
    if (!isset($_SESSION['chatHistory'][$_POST['to']])) { 
        $_SESSION['chatHistory'][$_POST['to']] = ''; 
    } 
    $_SESSION['chatHistory'][$_POST['to']] .= <<<EOD 
                       { 
            "s": "1", 
            "f": "{$to}", 
            "m": "{$messagesan}" 
       }, 
EOD; 
    unset($_SESSION['tsChatBoxes'][$_POST['to']]); 
    $sql = "insert into chat (chat.from,chat.to,message,sent) values ('".mysql_real_escape_string($from)."', '".mysql_real_escape_string($to)."','".mysql_real_escape_string($message)."',NOW())"; 
    $query = mysql_query($sql); 
    echo "1"; 
    exit(0); 
} 
function closeChat() { 
    unset($_SESSION['openChatBoxes'][$_POST['chatbox']]); 
     
    echo "1"; 
    exit(0); 
} 
function sanitize($text) { 
    $text = htmlspecialchars($text, ENT_QUOTES); 
    $text = str_replace("\n\r","\n",$text); 
    $text = str_replace("\r\n","\n",$text); 
    $text = str_replace("\n","
",$text); 
    return $text; 
} 
?>
 
Grazie a tutti in anticipo per il vostro aiuto,