Forum Moderators: coopster
I am trying to enable ajax based shoutbox on my site (it is not my code). My site charset is UTF-8, and mysql database collation utf8_unicode_ci.
When I post message on cyrillic to chatbox, everything is ok, no problem with text. When I refresh page - all text is messed up. Text is getting unreadable and in my DB in this shoutbox table I have понеоо, not cyrillic symbols.
Here is the code, which is reponsible for submitting comments to the database
:
// Why doesn't htmlentities() figure this one out? who knows
function jal_special_chars ($s) {
$s = htmlspecialchars( $s, ENT_COMPAT , _JAL_ISO );
if ( strtolower ( _JAL_ISO )!= 'utf-8' )
$s = utf8_decode ( $s );
return str_replace("---","−-−",$s);
}////////////////////////////////////////////////////////////
// Functions Below are for submitting comments to the database
////////////////////////////////////////////////////////////
// When user submits and javascript fails
if (isset($_POST['shout_no_js'])) {
if ($_POST['shoutboxname']!= '' && $_POST['chatbarText']!= '') {
jal_addData($_POST['shoutboxname'], $_POST['chatbarText'], $_POST['shoutboxurl']);
jal_deleteOld(); //some database maintenance
//setcookie("jalUserName",$_POST['shoutboxname'],time()+60*60*24*30*3,'/');
//setcookie("jalUrl",$_POST['shoutboxurl'],time()+60*60*24*30*3,'/');
//take them right back where they left off
header('location: '.$_SERVER['HTTP_REFERER']);
} else echo "You must have a name and a comment";
}
//only if a name and a message have been provides the information is added to the db
if ($jal_user_name!= '' && $jal_user_text!= '' && $jalSendChat == "yes") {
jal_addData($jal_user_name,$jal_user_text,$jal_user_url); //adds new data to the database
jal_deleteOld(); //some database maintenance
}
function jal_addData($jal_user_name,$jal_user_text,$jal_user_url) {
global $jal_number_of_comments, $user, $host, $db, $pass, $prefix;
$pos = strpos($jal_user_text, "[url");
if(is_int($pos)) {
return;
}
if(!use_url && strlen($jal_user_url) > 8){
return;
}
//the message is cut of after 500 letters
$jal_user_name = strip_tags($jal_user_name);
$jal_user_name = substr(trim($jal_user_name), 0,11);
$jal_user_text = strip_tags($jal_user_text);
$jal_user_text = substr($jal_user_text,0,500);
// CENSORS .. default is off. To turn it on, uncomment the line below. Add new lines with new censors as needed.
//$jal_user_text = str_replace("#*$!", "****", $jal_user_text);
//$jal_user_text = jal_special_chars(trim($jal_user_text));
$jal_user_name = (empty($jal_user_name))? "Anonymous" : jal_special_chars($jal_user_name);
$jal_user_url = ($jal_user_url == "http://")? "" : jal_special_chars($jal_user_url);
$conn = mysql_connect($host, $user, $pass);
mysql_select_db($db, $conn);
mysql_query("INSERT INTO ".$prefix."liveshoutbox (time,name,text,url) VALUES ('".time()."','".mysql_real_escape_string($jal_user_name)."','".mysql_real_escape_string($jal_user_text)."','".mysql_real_escape_string($jal_user_url)."')", $conn);
}