I am trying to make a web based chat room and I am storing messages in a text file. But when writing to the file sometimes the line just breaks and starts a new one mid-string, my code for writing:
$_msg_file=fopen("message.txt","a");
stream_filter_append($_msg_file, 'convert.iconv.UTF-8/OLD-ENCODING');
///WRITE TO FILE
if($_SESSION["class"]==="admin")
{
fwrite($_msg_file,"<li><b style='color:#FF9393;'>".$_SESSION["name"]."</b> ".$_POST["msg"]."</li>
");
}else {
fwrite($_msg_file,"<li><b style='color:black;'>".$_SESSION["name"]."</b> ".$_POST["msg"]."</li>
");
}
fclose($_msg_file);
Output: http://pastebin.com/0cucqbTW
My jQuery for posting text:
var data={};
data["why"]="send";
data["msg"]=$(".box_msg").val();
if(data["msg"].length<100 && data["msg"].length>1)
{
$.ajax({
type:"POST",
url:"send.php",
data:data,
success:function(response)
{
$(".box_msg").val("");
},
error:function(response)
{
$("#chat").text(response);
}
});