0

Hi everyone i have a swift project about user registration, but i have problems with special characters like "ö,ü,ğ" when user entered thats they prints in xcode ok , but when sending json string that's converting like this ömer > &oumlmer

how can i fix this ? how can i change encoding for this special characters.

Code :

let request = NSMutableURLRequest(url: (URL(string: "xxxx.com/userregister.php") )! );
request.httpMethod = "POST";
let userstring = "My name is ömer";  
print(userstring)  //Everything okey printing : My name is Ömer
requst.httpBody=userstring.data(using: .utf8)    //I think problem is here.Its saving to database like : My name is &oumlMer

UserRegister.php

<?php 
require("Conn.php");
require("MySQLDao.php");

$email = htmlentities($_POST["email"]);
$password = htmlentities($_POST["password"]);
$name = htmlentities($_POST["name"]);
$gsm = htmlentities($_POST["gsm"]);
$onayid = "0";
$surname = htmlentities($_POST["surname"]);
$qrcodeid = htmlentities($_POST["qrcodeid"]);
$returnValue = array();

if(empty($email) || empty($password))
{
$returnValue["status"] = "error";
$returnValue["message"] = "Missing required fieldd";
echo json_encode($returnValue);
return;
}

$dao = new MySQLDao();
$dao->openConnection();
$userDetails = $dao->getUserDetails($email);

if(!empty($userDetails))
{
$returnValue["status"] = "error";
$returnValue["message"] = "User already exists";
echo json_encode($returnValue);
return;
}

$secure_password = md5($password); // I do this, so that user password cannot be read even by me

$result = $dao->registerUser($email,$secure_password,$name,$gsm,$onayid,$surname,$qrcodeid);

if($result)
{
$returnValue["status"] = "Success";
$returnValue["message"] = "User is registered";
echo json_encode($returnValue);
return;
}

$dao->closeConnection();

?>

Connection codes :

public function openConnection() {
$this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
    mysql_query("SET character_set_results=utf8", $this->conn);
    mb_language('uni'); 
    mb_internal_encoding('UTF-8');
    mysql_select_db($dbname, $this->conn);
    mysql_query("set names 'utf8'",$this->conn);
if (mysqli_connect_errno())
echo new Exception("Could not establish connection with database");
}

FIXED (must be change .php file mysql connection ) :

if (!$this->conn->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $this->conn->error);
    exit();
} else {
    printf("Current character set: %s\n", $this->conn->character_set_name());
} 

1 Answers1

0

The problem is htmlentities() function. It converts all applicable characters to HTML entities. http://php.net/manual/en/function.htmlentities.php

It does not prevent SQL injection. So you don't need it. You can look this. How can I prevent SQL injection in PHP?

Try this.

if (!$this->conn->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $this->conn->error);
    exit();
} else {
    printf("Current character set: %s\n", $this->conn->character_set_name());
}
Community
  • 1
  • 1
Hakan SONMEZ
  • 2,176
  • 2
  • 21
  • 32