Message Truncation UTF-8 Problems With PHP and Sajax library

I recently had a long night debugging this: My application using PHP, Sajax and MySQL truncated messages from the first non-ascii character to the end of the message. For example: if I typed “yö tulee” (finnish sentence means that “night is coming”) only “y” was inserted into the database.

The problem was with Sajax library. Sajax does not handle UTF-8, but there’s an easy fix for it: open the Sajax.php, locate the code in the following snippet and add the bolded line to your Sajax.php:

// Bust cache in the head
header (“Expires: Mon, 26 Jul 1997 05:00:00 GMT”); // Date in the past
header (“Last-Modified: ” . gmdate(“D, d M Y H:i:s”) . ” GMT”);
// always modified
header (“Cache-Control: no-cache, must-revalidate”); // HTTP/1.1
header (“Pragma: no-cache”); // HTTP/1.0
header (“Content-type: charset=UTF-8”);
$func_name = $_GET[“rs”];

This forces the Sajax library to use UTF-8 charset when it transmits data between the browser and the server. Also if you are using MySQL with UTF-8 you need to read this. Also encoding the SQL query string with $sql = utf8_encode($sql); might help.

One thought on “Message Truncation UTF-8 Problems With PHP and Sajax library

Comments are closed.