views:

514

answers:

6

I'm trying this code (on my local web server)

<?php
echo 'the word is / думата е '.$_GET['word'];
?>

but I get corrupted result when enter ?word=проба

the word is / думата е ����

The document is saved as 'UTF-8 without BOM' and headers are also UTF-8. I have tried urlencode() and urldecode() but the effect was same. When upload it on web server, works fine...

A: 

What if you try sending a HTTP Content-type header, to indicate the browser which encoding / charset your page is generating ?

For instance, something like this might help :

header('Content-type: text/html; charset=UTF-8');
echo 'the word is / думата е '.$_GET['word'];

Of course, this is if you are generating HTML -- you probably are.


Considering there is a configuration setting at the server's level that defines which encoding is sent by default, maybe the default encoding on your server is OK -- while the one on your local server is not.

Sending such a header by yourself would solve the problem : it would make sure the encoding is always set properly.

Pascal MARTIN
I have tried with header() - same result. I think that is something with my local server configuration but I don't know where to look.I got:[Apache Environment]HTTP_ACCEPT_CHARSET UTF-8,* [HTTP Headers Information]Accept-Charset UTF-8,* Content-Type text/html; charset=utf-8 [PHP Variables]_SERVER["HTTP_ACCEPT_CHARSET"] UTF-8,*
T1000
A: 

I suppose you are using the Apache web server.

There is a common problem with Apache configuration - a line with "AddDefaultCharset" in the config should be commented out (add # in the begining of the line, or replace the line with "AddDefaultCharset off") because it "overrides any encoding given in the files in meta http-equiv or xml encoding tags".

In my current installation (Apache2 @ Ubuntu Linux) the line is found in "/etc/apache2/conf.d/charset" but in other (Linux/Unix) setups can be in "/etc/apache2/httpd.conf", or "/etc/apache/httpd.conf" (if you are using Apache 1). If you don't find it in these files you can search for it with "cd /etc/apache2 ; grep -r AddDefaultCharset *" (for Apache 2 @ Unix/Linux).

nupogodi
nupogodi 10x for your answer! You're tight about that I'm using apache (on Windows) but after searching the text "AddDefaultCharset" in all files *.* in apache dir I found only on 1 hit in file called "httpd.h" but it is already commented. I hope u have any other sugestions.
T1000
According to http://httpd.apache.org/docs/2.0/platform/windows.html#cust apache is configured by the files in the conf subdirectory of your installation. If there is no "AddDefaultCharset" there try to add a new line "AddDefaultCharset off" in the httpd.conf in this directory (if there is such file, or add the line to another file that you know is used by apache for configuration).
nupogodi
nupogodi I have add it into httpd.conf (I'm sure that apache get settings from there) restart it, but there was no effect.
T1000
Did you add the header from the other comment because it's also required?Also I suppose that the problem might be your browser sending windows-1251 encoded the word "проба" so try changing the default browser encoding or using the header with "windows-1251" instead of UTF-8.
nupogodi
Yes nupogodi, I have tried it with the header function.I don't think it is from my browser because the very same file works on the real server. Also when I manualy set browser's encoding to windows-1251 the text from GET var become readable but all other text become broken AND I don't want my site to be windows-1251.... I think It is something from apache or maybe php.ini configuration but I've tried almost everything but the problem is still here.
T1000
My god! It works ONLY on chrome. Firefox and IE7 and 8 failed . WTF
T1000
A: 

If You recieve $_GET from AJAX make sure that Your blablabla.js file in UTF-8 encode. Also You can use iconv("cp1251","utf8",$_GET['word']); to display your $_GET['word'] in UTF-8

dev reactive.by
A: 

Take a look at Changing the server encoding. An excellent read!

Cheers!

lunohodov
A: 

T1000, did you manage to solve the issue? I seem to be having the exact same problem. None of the proposed solutions on this page work for me as well.

mm55mm
Nope. I've decided to ignore it for now, the script works when I'm testing localy with Crome and works on every browser on the real host.I'll write if I find the problem, hope u do the same.
T1000
A: 

did anyone solve this, because i'm having same problems with apache2 on ubuntu ??

baltazar