[英]Special Characters with Swift(Fixed check bottom of the main post)
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 > ömer 大家好,我有一个关于用户注册的快速项目,但是当用户输入的特殊字符“ö,ü,ğ”存在问题时,我用xcode ok打印了这些字符,但是发送的json字符串转换为ö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 öMer
UserRegister.php 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 ) : 固定(必须更改.php文件mysql连接):
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());
}
The problem is htmlentities() function. 问题是htmlentities()函数。 It converts all applicable characters to HTML entities.
它将所有适用的字符转换为HTML实体。 http://php.net/manual/en/function.htmlentities.php
http://php.net/manual/zh/function.htmlentities.php
It does not prevent SQL injection. 它不会阻止SQL注入。 So you don't need it.
因此,您不需要它。 You can look this.
你可以看这个。 How can I prevent SQL injection in PHP?
如何防止PHP中进行SQL注入?
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());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.