Funzione php non viene richiamata!

In un file a parte mi funziona, quindi è possibile che l'errore sia in questo .js della chat?
PHP:
var windowFocus = true;
var username;
var chatHeartbeatCount = 0;
var minChatHeartbeat = 1000;
var maxChatHeartbeat = 33000;
var chatHeartbeatTime = minChatHeartbeat;
var originalTitle;
var blinkOrder = 0;

var chatboxFocus = new Array();
var newMessages = new Array();
var newMessagesWin = new Array();
var chatBoxes = new Array();

$(document).ready(function(){
	originalTitle = document.title;
	startChatSession();

	$([window, document]).blur(function(){
		windowFocus = false;
	}).focus(function(){
		windowFocus = true;
		document.title = originalTitle;
	});
});

function restructureChatBoxes() {
	align = 0;
	for (x in chatBoxes) {
		chatboxtitle = chatBoxes[x];

		if ($("#chatbox_"+chatboxtitle).css('display') != 'none') {
			if (align == 0) {
				$("#chatbox_"+chatboxtitle).css('right', '20px');
			} else {
				width = (align)*(225+7)+20;
				$("#chatbox_"+chatboxtitle).css('right', width+'px');
			}
			align++;
		}
	}
}

function chatWith(chatuser) {
	createChatBox(chatuser);
	$("#chatbox_"+chatuser+" .chatboxtextarea").focus();
}

function createChatBox(chatboxtitle,minimizeChatBox) {
	if ($("#chatbox_"+chatboxtitle).length > 0) {
		if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
			$("#chatbox_"+chatboxtitle).css('display','block');
			restructureChatBoxes();
		}
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
		return;
	}

	$(" <div />" ).attr("id","chatbox_"+chatboxtitle)
	.addClass("chatbox")
	.html('<div class="chatboxhead"><div class="chatboxtitle">'+chatboxtitle+'</div><div class="chatboxoptions"><a href="javascript:void(0)" onclick="javascript:toggleChatBoxGrowth(\''+chatboxtitle+'\')">-</a> <a href="javascript:void(0)" onclick="javascript:closeChatBox(\''+chatboxtitle+'\')">X</a></div><br clear="all"/></div><div class="chatboxcontent"></div><div class="chatboxinput"><textarea class="chatboxtextarea" onkeydown="javascript:return checkChatBoxInputKey(event,this,\''+chatboxtitle+'\');"></textarea></div>')
	.appendTo($( "body" ));
			   
	$("#chatbox_"+chatboxtitle).css('bottom', '0px');
	
	chatBoxeslength = 0;

	for (x in chatBoxes) {
		if ($("#chatbox_"+chatBoxes[x]).css('display') != 'none') {
			chatBoxeslength++;
		}
	}

	if (chatBoxeslength == 0) {
		$("#chatbox_"+chatboxtitle).css('right', '20px');
	} else {
		width = (chatBoxeslength)*(225+7)+20;
		$("#chatbox_"+chatboxtitle).css('right', width+'px');
	}
	
	chatBoxes.push(chatboxtitle);

	if (minimizeChatBox == 1) {
		minimizedChatBoxes = new Array();

		if ($.cookie('chatbox_minimized')) {
			minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
		}
		minimize = 0;
		for (j=0;j<minimizedChatBoxes.length;j++) {
			if (minimizedChatBoxes[j] == chatboxtitle) {
				minimize = 1;
			}
		}

		if (minimize == 1) {
			$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
			$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
		}
	}

	chatboxFocus[chatboxtitle] = false;

	$("#chatbox_"+chatboxtitle+" .chatboxtextarea").blur(function(){
		chatboxFocus[chatboxtitle] = false;
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").removeClass('chatboxtextareaselected');
	}).focus(function(){
		chatboxFocus[chatboxtitle] = true;
		newMessages[chatboxtitle] = false;
		$('#chatbox_'+chatboxtitle+' .chatboxhead').removeClass('chatboxblink');
		$("#chatbox_"+chatboxtitle+" .chatboxtextarea").addClass('chatboxtextareaselected');
	});

	$("#chatbox_"+chatboxtitle).click(function() {
		if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') != 'none') {
			$("#chatbox_"+chatboxtitle+" .chatboxtextarea").focus();
		}
	});

	$("#chatbox_"+chatboxtitle).show();
}


function chatHeartbeat(){

	var itemsfound = 0;
	
	if (windowFocus == false) {
 
		var blinkNumber = 0;
		var titleChanged = 0;
		for (x in newMessagesWin) {
			if (newMessagesWin[x] == true) {
				++blinkNumber;
				if (blinkNumber >= blinkOrder) {
					document.title = x+' says...';
					titleChanged = 1;
					break;	
				}
			}
		}
		
		if (titleChanged == 0) {
			document.title = originalTitle;
			blinkOrder = 0;
		} else {
			++blinkOrder;
		}

	} else {
		for (x in newMessagesWin) {
			newMessagesWin[x] = false;
		}
	}

	for (x in newMessages) {
		if (newMessages[x] == true) {
			if (chatboxFocus[x] == false) {
				//FIXME: add toggle all or none policy, otherwise it looks funny
				$('#chatbox_'+x+' .chatboxhead').toggleClass('chatboxblink');
			}
		}
	}
	
	$.ajax({
	  url: "chat.php?action=chatheartbeat",
	  cache: false,
	  dataType: "json",
	  success: function(data) {

		$.each(data.items, function(i,item){
			if (item)	{ // fix strange ie bug

				chatboxtitle = item.f;

				if ($("#chatbox_"+chatboxtitle).length <= 0) {
					createChatBox(chatboxtitle);
				}
				if ($("#chatbox_"+chatboxtitle).css('display') == 'none') {
					$("#chatbox_"+chatboxtitle).css('display','block');
					restructureChatBoxes();
				}
				
				if (item.s == 1) {
					item.f = username;
				}

				if (item.s == 2) {
					$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
				} else {
					newMessages[chatboxtitle] = true;
					newMessagesWin[chatboxtitle] = true;
					$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
				}

				$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
				itemsfound += 1;
			}
		});

		chatHeartbeatCount++;

		if (itemsfound > 0) {
			chatHeartbeatTime = minChatHeartbeat;
			chatHeartbeatCount = 1;
		} else if (chatHeartbeatCount >= 10) {
			chatHeartbeatTime *= 2;
			chatHeartbeatCount = 1;
			if (chatHeartbeatTime > maxChatHeartbeat) {
				chatHeartbeatTime = maxChatHeartbeat;
			}
		}
		
		setTimeout('chatHeartbeat();',chatHeartbeatTime);
	}});
}

function closeChatBox(chatboxtitle) {
	$('#chatbox_'+chatboxtitle).css('display','none');
	restructureChatBoxes();

	$.post("chat.php?action=closechat", { chatbox: chatboxtitle} , function(data){	
	});

}

function toggleChatBoxGrowth(chatboxtitle) {
	if ($('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display') == 'none') {  
		
		var minimizedChatBoxes = new Array();
		
		if ($.cookie('chatbox_minimized')) {
			minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
		}

		var newCookie = '';

		for (i=0;i<minimizedChatBoxes.length;i++) {
			if (minimizedChatBoxes[i] != chatboxtitle) {
				newCookie += chatboxtitle+'|';
			}
		}

		newCookie = newCookie.slice(0, -1)


		$.cookie('chatbox_minimized', newCookie);
		$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','block');
		$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','block');
		$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
	} else {
		
		var newCookie = chatboxtitle;

		if ($.cookie('chatbox_minimized')) {
			newCookie += '|'+$.cookie('chatbox_minimized');
		}


		$.cookie('chatbox_minimized',newCookie);
		$('#chatbox_'+chatboxtitle+' .chatboxcontent').css('display','none');
		$('#chatbox_'+chatboxtitle+' .chatboxinput').css('display','none');
	}
	
}

function checkChatBoxInputKey(event,chatboxtextarea,chatboxtitle) {
	 
	if(event.keyCode == 13 && event.shiftKey == 0)  {
		message = $(chatboxtextarea).val();
		message = message.replace(/^\s+|\s+$/g,"");

		$(chatboxtextarea).val('');
		$(chatboxtextarea).focus();
		$(chatboxtextarea).css('height','44px');
		if (message != '') {
			$.post("chat.php?action=sendchat", {to: chatboxtitle, message: message} , function(data){
				message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
				$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+username+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+message+'</span></div>');
				$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
			});
		}
		chatHeartbeatTime = minChatHeartbeat;
		chatHeartbeatCount = 1;

		return false;
	}

	var adjustedHeight = chatboxtextarea.clientHeight;
	var maxHeight = 94;

	if (maxHeight > adjustedHeight) {
		adjustedHeight = Math.max(chatboxtextarea.scrollHeight, adjustedHeight);
		if (maxHeight)
			adjustedHeight = Math.min(maxHeight, adjustedHeight);
		if (adjustedHeight > chatboxtextarea.clientHeight)
			$(chatboxtextarea).css('height',adjustedHeight+8 +'px');
	} else {
		$(chatboxtextarea).css('overflow','auto');
	}
	 
}

function startChatSession(){  
	$.ajax({
	  url: "chat.php?action=startchatsession",
	  cache: false,
	  dataType: "json",
	  success: function(data) {
 
		username = data.username;

		$.each(data.items, function(i,item){
			if (item)	{ // fix strange ie bug

				chatboxtitle = item.f;

				if ($("#chatbox_"+chatboxtitle).length <= 0) {
					createChatBox(chatboxtitle,1);
				}
				
				if (item.s == 1) {
					item.f = username;
				}

				if (item.s == 2) {
					$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxinfo">'+item.m+'</span></div>');
				} else {
					$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');
				}
			}
		});
		
		for (i=0;i<chatBoxes.length;i++) {
			chatboxtitle = chatBoxes[i];
			$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);
			setTimeout('$("#chatbox_"+chatboxtitle+" .chatboxcontent").scrollTop($("#chatbox_"+chatboxtitle+" .chatboxcontent")[0].scrollHeight);', 100); // yet another strange ie bug
		}
	
	setTimeout('chatHeartbeat();',chatHeartbeatTime);
		
	}});
}

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
 
<?php

$items = "nel mezzo di :) camin di ns vita mi ritrovai per una -) selva oscura";
$items_ok = str_replace(":)", "<img src='smile.gif'>", $items);
$items_ok = str_replace("-)", "<img src='smile.gif'>", $items_ok);
echo $items_ok;
?>

funziona anche a me
non credo tu abbia una versione di php inferiore alla 4
 
ciao
per il js non so come fare lo conosco pochissimo, ma (mi sembra) che non ci siano i tag <script type="text/javascript">......</script> ne (anche qui mi sembra) il richiamo alla jquery
HTML:
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
se ho detto una put....ta cancella tutto
 
io invece non ho capito quando richiami la funzione faccine()

non dovresti richiamarla in sendChat() ?

PHP:
$messagesan = faccine(sanitize($message));
 
Guarda il mio codice: la richiamo più volte. Comunque l'ho modificato anche come mi hai detto e non funziona :dipser:

Ecco il codice finale:
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 src='\smile.gif'>", $items); 
  $items_ok = str_replace(":-)", "<img src='\smile.gif'>", $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 = faccine(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","<br>",$text); 
    return $text; 
} 
?>
<html>
<head>
<link type="text/css" rel="stylesheet" media="all" href="/css/chat.css" />
<link type="text/css" rel="stylesheet" media="all" href="/css/screen.css" />

<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/chat.js"></script>
</head>
<body>
<?php
// salvo nella sessione l'username dell'utente
$_SESSION['username'] = $_SESSION['utente'];

// effettuo una query su un'ipotetica tabella di utenti
$res = mysql_query("SELECT username FROM utenti WHERE stato = 1 AND username != '".$_SESSION['utente']."'");

while ($row = mysql_fetch_array($res)){
  // creo dinamicamente il link per aprire la chat
  echo "<a href=\"javascript:void(0)\" onclick=\"javascript:chatWith('" . $row['username'] . "')\">Chatta con " . $row['username'] . "</a><br>";
}
?>

</body>
</html>
 
Ciao,

non conosco php, ma il replace in asp è simile, ho fatto una prova in php e funziona anche a me.

Nel JS hai un replace message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");

sostituzione di < con &lt; > con &gt; e \" non &quot;

Prova a commentarlo.

Solo una teoria, non conoscendo php.

Valeria.
 
questa non la capisco
PHP:
<?php echo ".faccine($items)."; ?>
gli apici non dovrebbero esserci
PHP:
<?php echo faccine($items) ?>
 
Salve

da profana

se aggiungo

Codice:
$items_ok = str_replace("<","&lt;", $items_ok);
$items_ok = str_replace(">","&gt;", $items_ok);
$items_ok = str_replace("\"","&quot;", $items_ok);

all'esempio

Codice:
 <?php
function faccine($items){
  $items_ok = str_replace(":)", "<img src='smile.gif'>", $items);
  $items_ok = str_replace(":-)", "<img src='smile.gif'>", $items_ok);
  $items_ok = str_replace(":(", "<img src=\"smile.gif\">", $items_ok);
  //eccetera.....

$items="pinco :) pallo eccetera.....:-)  aaaaaa a :( SSSSSSS";

Risultato html

pinco &lt;img src='smile.gif'&gt; pallo eccetera.....&lt;img src='smile.gif'&gt; aaaaaa a &lt;img src=&quot;smile.gif&quot;&gt; SSSSSSS


:confused:
 
@Valeria
si, giusto
sostituisci i caratteri a questa stringa "<img src='smile.gif'>" e non piu a questa ": )" perche è stata sostituita dall'istruzione precedente
dove ti sei persa?
 
Tornando a noi,
ti conviene partire da quando stampi il messaggio e tornare indietro passo passo e verificare che il messaggio venga dato in pasto alla funzione faccine() perche secondo me questo passaggio viene saltato
 
quello a pagina tre è il codice javascript, non penso che c'èntri
 
@criric ciao
la funzione la richiama qui
PHP:
//....
header('Content-type: application/json');
?>
{
        "items": [
            <?php echo ".faccine($items).";?>
        ]
}

<?php
            exit(0);
}
//.....
anche io gli ho detto di togliere gli apici, ma dice che non funzia lo stesso

comunque: prova a togliere la funzione(vedi sotto): la chat funzia come dovrebbe?

PHP:
//....
header('Content-type: application/json');
?>
{
        "items": [
            <?php echo $items;?>
        ]
}

<?php
            exit(0);
}
//.....
 
Ultima modifica:
@criric ciao
la funzione la richiama qui
PHP:
//....
header('Content-type: application/json');
?>
{
        "items": [
            <?php echo ".faccine($items).";?>
        ]
}

<?php
            exit(0);
}
//.....
anche io gli ho detto di togliere gli apici, ma dice che non funzia lo stesso

comunque: prova a togliere la funzione(vedi sotto): la chat funzia come dovrebbe?

PHP:
//....
header('Content-type: application/json');
?>
{
        "items": [
            <?php echo $items;?>
        ]
}

<?php
            exit(0);
}
//.....

Ho provato direttamente a togliere tutta la funzione e a lasciare i riferimenti nel php, ma non mi visualizza nessun errore :confused: come è possibile?
 

Discussioni simili