I have written a php script that I use it to upload data from a mobile (iOS) app to mysql server. The procedure works perfect and I can enter and get data from the server, but i have noticed when I post greek letters are being encoded wrongly ("Δέντρο" instead of Δέντρο). I have entered below on my php:
header('Content-Type: text/html; charset=utf-8');
but it still the data are not correct encoded. The words are not encoded correct even if I enter the data directly on the webpage instead posting them from app. The main code that I use is the following:
header('Content-Type: text/html; charset=utf-8');
require("../db/mySQL_dao.php");
$config = parse_ini_file("../../../municipapp.ini");
$returnValue = array();
if (empty($_REQUEST["name"]) || empty($_REQUEST["lon"]) || empty($_REQUEST["lan"])) {
$returnValue["status"]="400";
$returnValue["message"]="Didn't get all necessary info";
echo json_encode($returnValue);
return;
}
$issueName = htmlentities($_REQUEST["name"]);
$issue = htmlentities($_REQUEST["issue"]);
$issueComments = htmlentities($_REQUEST["comments"]);
$issueLon = htmlentities($_REQUEST["lon"]);
$issueLan = htmlentities($_REQUEST["lan"]);
$issueUserID = htmlentities($_REQUEST["userID"]);
$issueIsItOk = htmlentities($_REQUEST["isitok"]);
// Generate secure password
$salt = openssl_random_pseudo_bytes(16);
$secured_password = sha1($password . $salt);
$dbhost = trim($config["dbhost"]);
$dbuser = trim($config["dbuser"]);
$dbpassword = trim($config["dbpass"]);
$dbname = trim($config["dbname"]);
$dao = new mySQL_dao($dbhost, $dbuser, $dbpassword, $dbname);
$dao->openConnection();
// Register New Issue
$result = $dao->insertIssue($issueName, $issue, $issueComments, $issueLon, $issueLan, $issueUserID, $issueIsItOk);
if ($result) {
$issueDetails = $dao->getIssueDetails($issueName);
$returnValue["status"]="200";
$returnValue["message"]="Successfully Inserted";
$returnValue["issueId"]=$issueDetails["id"];
$returnValue["issueName"]=$issueDetails["name"];
} else {
$returnValue["status"]="400";
$returnValue["message"]="Could not insert data";
}
$dao->closeConnection();
echo json_encode($returnValue);
Check Collation of your database table. It is good variant if your collation, for example, is utf_general_ci. And it is bad variant if collation is latin1_swedish_ci, for example.
To check collation:
SELECT table_collation FROM information_schema.tables WHERE table_name = 'your_table';
To check charset:
SELECT cc.character_set_name
FROM information_schema.tables t,
information_schema.collation_character_set_applicability cc
WHERE t.table_collation = cc.collation_name
AND t.table_name = 'your_table';
To chacnge collation and character set:
ALTER TABLE your_table
CHARACTER SET utf8
COLLATE utf8_general_ci;
Also set charset and collation after connect:
SET NAMES utf8;
SET collation_connection = utf8_unicode_ci;
And try to insert without using htmlentities() for processing data and use htmlspecialchars instead.