Charset issue! Webpage transfer to another server - php

1) I migrated WEB page to another server! From Ms Server 2012/Xampp to Cento7/httpd. On Centos7 Web page contains question marks where should be a special characters "āīūņļš". Web page is built on PHP.
Old server is running old XAMP installation:
PHP Version 5.4.27
5.5.36 - MySQL
CentOS running:
apach/httpd
PHP 7.0.*
5.5.52-MariaDB
2) Both servers contains files with same encoding and DB tables with same charset colletions and both servers have "Server charset is UTF-8 Unicode (utf8)".
Only difference is "Server connection collation". Old server have "Collation" and CentOS have "utf8_general_ci".
3) I tried:
encode files to utf8
define utf8 in meta tags
header('Content-Type: text/html; charset=ISO-8859-1'); and
header('Content-Type: text/html; charset=utf8');
mysqli_set_charset($con,"utf8");
AddDefaultCharset UTF-8 in httpd.conf
I just don't understand, why on one server everything is OK and on another server text contains "?" when files/php code/db is the same! Is there a chance that the httpd doesn't have some module enabled?
And there is one more problem! Some php files and DB tables have different encoding and char collection. I tried change file encoding to UTF8 and it solved problem for static text in php files. Some text in db contains strange characters and db contains lot of information. In some cases mysqli_set_charset($con,"utf8"); works but there ar times when text randomly disappear when mysqli_set_charset is used!

Related

Diamond interrogation in PHP application

I have exactly the same application hosted in two different shared servers (one Windows and other Linux), both connecting in the same SQL Server database using PDO.
Somehow in one of the servers (the Linux one) all special chars that comes from SQL Server (like é, ç, õ) are being replaced by � .
The DB charset is Latin1_General_CI_AS and i'm sending the following header to the browser :
<meta charset="UTF-8">
Here is my connection to PDO :
$DBH = new PDO('dblib:host=111.111.111.11;dbname=mydbname;charset=utf8','myuser','mypass')
My question is : how can i find out what is happening here and why the same app runs fine in one server but not in another. Maybe is some configuration in php.ini or in the PDO object ?
I've downloaded a plugin for Chrome that allows you to change the page's character encoding. When i change it to 'Windows-1252', the data coming from the database are displayed correctly, BUT any other element on page that is generated dynamically by PHP gets the special chars replaced by symbols like ç and ã .
Please do not mark this question as duplicated, because it's not ; i can't change the DB charset to UTF-8. And because the application runs fine in the Windows server, i think there is something i can do to fix the issue in the Linux server, besides messing with the database configuration (which i don't have access at the moment).
Any ideas ?
Thanks !
Besides client encoding, which you did already by setting <meta charset="utf-8"/>, you will have to set your connection encoding as well (for both applications).
For PHP, the accepted answer in this post suggests you do the following:
ini_set('mssql.charset', 'UTF-8');
Edit:
For PDO, you can pass it in the connection string. Example:
$DBH = new PDO('mssql:dbname=spr_bank;host=hostname;charset=utf8', $userDB, $passwordDB);
Note that this won't change your actual table to UTF-8. This will only convert charsets for your connection, making it read/write the right characters. Even if it is a Latin1_General_CI_AS table/database.
Here is what fixed my issue :
Set up /your/local/freetds.conf file:
[sqlservername]
host=111.111.111.11
port=1433
tds version=7.0
client charset=UTF-8
Make sure your connection DSN is using the servername, not the IP:
'dsn' => 'dblib:host=sqlservername;dbname=yourdb
Make FreeTDS to use your local freetds.conf file as an unprivileged user from php script via env variables:
putenv('FREETDSCONF=/your/local/freetds.conf');

Special characters not showing on the web page after set to charset=UTF8

I have an app that developed with JS, PHP and for some reason it is not able to display the Spanish Accents in the dropdown box which the values are from the mysql table. For the same app, it display those Spanish accents without any problem on the windows web server IIS 8.5, but not in the Linux box which running REL 6.5 with apache 2.2 installed.
Here are what I did try and checked.
The charset=UTF-8 already set in the meta header in the html. The php.ini on both servers are set default to UTF-8
Default charset for Apache config file is set to UTF-8.
Checked the locale setting on Linux box to make sure include the es-UTF8 in the sysconfig.
Checked the chrome dev tool by inspect the response header and the content-type showing plain/html , charset=UTF-8, vary: accept encoding.
If I go into the phpMyAdmin and look at the raw table, the accents are stored in the MySQL table and I can search it too inside phpMyAdmin.
Run below query before executing actual query:
mysqli_query($con, 'SET names UTF8');

PHP Issues with character sets and collates

I working on a website project using Bootstrap 3. The character set I am using on my html pages is utf8. On localhost, I am using MySQL56 where the database for the project has utf8 as character set and utf8_general_ci as collate.
On the remote server, the database I created has the same character set and collate.
When I am doing test on local, the characters appear well but when I am doing the test on remote server, I get diamonds on some characters like 'é' or 'è'. Also, when I write data into the database table, those characters doesn't appear well in PHPMyAdmin, but everything is fine on my computer.
I don't understand where the problem comes from and how to fix it.
Thank you.
Make sure you're setting PHP headers correctly;
header('Content-Type: text/html; charset=utf-8');
You can either place this in a common include in PHP or edit your PHP.ini file and modify/add this line;
default_charset = "utf-8";

Bad encoding from database in php file using IIS 8.5

I must migrate large database and large php systems from php4 to php5.
Databases tables stored in UTF-8 format, but the data contain windows-1257;
All page in header is:
But I get data from database like this: AutomobiliĆø stovĆ«jimo;
var_dump(mysql_client_encoding($connect)); return utf8;
File encoding: windows-1257;
In Apache server (try Wamp in W7 and Windows server 2012) get normal data.
But IIS dont.. Mb IIS dont understand file encoding or etc..
I give up, and I need your help...
SOVLED: I change mysql configuration (my.ini) and set character_set_server utf8 to latin1
And now var_dump(mysql_client_encoding($connect)); return latin1;
And all projects works fine.
Databases tables stored in UTF-8 format, but the data contain
windows-1257.
Try converting the data from Windows-1257 to UTF-8 with something like:
$encoded = iconv ( "CP1257", "UTF-8", $string );

UTF8 characters from database don't show up properly in the browser - MySQL & PHP CodeIgniter

My database and tables are set to utf8_general_ci collation and utf8 charset. CodeIgniter is set to utf8. I've added meta tag charset=utf8, and I'm still getting something like: квартиры instead of cyrillic letters...
The same code running on the local machine works fine - Mac OSX. It's only breaking in the production machine, which is Ubuntu 11.10 64bit in AWS EC2. Static content from the .php files show up correctly, only the data coming from the database are messed up. Example page: http://dev.uzlist.com/browse/cat/nkv
Any ideas why?
Thanks.
FYI:
When I do error_log() the data coming from the database, it's the same values I'm seeing on the page. Hence, it's not the browser-server issue. It's something between mysql and php, since when I run SELECT * FROM categories, it shows the data in the right format. I'm using PHP CodeIgniter framework for database connection and query and as mentioned here, I have configured it to use utf8 connection and utf8_general_ci collation.
Make sure your my.cnf (likely to be in /etc/) has the following entries :
[mysqld]
default-character-set=utf8
default-collation=utf8_general_ci
character-set-server=utf8
collation-server=utf8_general_ci
init-connect='SET NAMES utf8'
[client]
default-character-set=utf8
You'll need to restart the mysql service once you make your changes.
Adding my comments in here to make this a little clearer.
Make sure the following HTTP header is being set so the browser knows what charset to expect.
Content-type: text/html; charset=UTF-8
Also try adding this tag into the top of your html <head> tag
<meta http-equiv="Content-type" value="text/html; charset=UTF-8" />
To make the browser show up correctly.you should check three points:
encoding of your script file.
encoding of connection.
encoding of database or table schema.
if all of these are compatible, you'll get the page you want.
The original data has been encoded as UTF-8, the result interpreted in Windows-1252 and then UTF-8 encoded again. This is really bad; it isn't about a simple encoding mismatch that a header would fix. Your data is actually broken.
If the data is ok in the database (check with SELECT hex(column) FROM myTable) to see if it was double encoded already in the database), then there must be your code that is converting it to UTF-8 on output.
Search your project for uses of function utf8_encode, convert_to_utf8, or just iconv or mb_convert_encoding. Running
$ grep -rn "\(utf8_\(en\|de\)code\|convert_to_utf8\|iconv\|mb_convert_encoding\)" .
On your application's /application folder should be enough to find something.
Also see config values for these:
<?php
var_dump(
ini_get( "mbstring.http_output" ),
ini_get( "mbstring.encoding_translation" )
);
Well, if you absolutely and positively sure that your mysql client encoding is set to utf8, there are 2 possible cases. One - double encoding - described by Esailija.
But there is another one: you have your data actually encoded in 1251, not in utf-8.
In this case you have to either recode your data or set proper encoding on the tables. Though it is not one button push task
Here is a manual (in russian) exаctly for that case: http://phpfaq.ru/charset#repair
In short, you have to dump your table, using the same encoding set on the table (to avoid recoding), backup that dump in safe place, then change table definitions to reflect the actual encoding and then load it back.
Potentially this may also be caused by the mbstring extension not being installed (which would explain a difference between your dev and production environments)
Check out this post, might give you a few more answers.
Try mysql_set_charset('utf8') after the mysql connect. Then it should works.
After 2 days of fighting this bug, finally figured out the issue. Thanks for #yourcommonsense, #robsquires, and a friend of mine from work for good resources that helped to debug the issue.
The issue was that at the time of the sql file dump to the database (import), charset for server, database, client, and connection was set to latin1 (status command helped to figure that out). So the command line was set to latin1 as well, which is why it was showing the right characters, but the connection with the PHP code was UTF8 and it was trying to encode it again. Ended up with double encoding.
Solution:
mysqldump the tables and the data (while in latin1)
dump the database
set the default charsets to UTF8 in /etc/my.cnf as Rob Squires mentioned
restart the mysql
create the database again with the right charset and collation
dump the file back into it
And it works fine.
Thanks all for contribution!

Categories