2

I'm having issues with the encoding of the the '£' character. (Issue: When POSTing '£' from a form field and doing an insert, nothing is inserted in the MySQL table). I've checked everything wrt to UTF-8 support on my PHP code headers, server, collation/char set on MySQL etc.

I'm using MAMP as my dev environment (PHP 5.3.5).

Everything works fine on my production server (commercial host) (PHP 5.2.6) so I've ruled out any issues with my code

However, I think I have tracked down the culprit: When comparing both environments, this line is missing from my dev server:

_ENV["HTTP_ACCEPT_CHARSET"] ISO-8859-1,utf-8;q=0.7,*;q=0.3

However, there is nothing in php.ini I can see to change it. Any ideas, or am I barking up the wrong tree?

Cheers

Roland

RolandFlyBoy
  • 193
  • 3
  • 14
  • Re: "Everything works fine on my production server [...] so I've ruled out any issues with my code": This is **wrong**! You might have a wrong dependency on some environment setting that you luckily got "right" on the production system. – Joachim Sauer Apr 26 '11 at 15:34
  • The HTTP `Accept-Charset` header comes from the client. It is possible that something with your browser is not sending that header during communication and it is ignoring your `Charset: UTF-8` header from the server (you are sending one right?) and the browser is sending as whatever it wants. – Kevin Peno Apr 26 '11 at 15:35
  • Side question, is it necessary to make the person type the pound '£' symbol? Could you not just have it hard coded or an option from a drop down? Then you could use `£` – Kevin Peno Apr 26 '11 at 15:39

1 Answers1

0

I'd write a simple test to check out where things are going wrong.

  • echo() out the value from $_POST in PHP and verify whether the browser is sending the data correctly and that it's being parsed into PHP correctly. WHen you do this test make sure that the browser has correctly detected the character set.
  • If that works then you're likely to have mis-configured something with the database. If you have both the table collation and the connection encoding as "UTF-8" then you should have no problems saving the data into MySQL (if both SET NAMES and the table collation are the same no translation will go on so it'll be stored correctly in the table).

You didn't mention the MySQL connection anywhere in your question (just "etc"). Just in case there's anything you've missed have a good look over this article How to Avoid Character Encoding Problems in PHP

James C
  • 14,047
  • 1
  • 34
  • 43
  • Both connection and collation are set to UTF-8 as is my Codeigniter config VAR (which I assumed would overide any issues). I'm using Firebug to view the XHR POST data and £ is posted fine (although it has encoded it as %C%A20 if I look at the raw data) Nobody addressed the difference in the phpinfo() dump of: _ENV["HTTP_ACCEPT_CHARSET"] ISO-8859-1,utf-8;q=0.7,*;q=0.3 – RolandFlyBoy Apr 26 '11 at 15:59
  • Please use PHP's primitive `echo()` to return the data from the `$_POST` variable as per my suggestion. – James C Apr 26 '11 at 16:27
  • @Roland, I did in my comment to your question :P – Kevin Peno Apr 26 '11 at 16:31
  • @James C Using Firebug the parameter list shows £ (the raw serialised html shows %C2%A3) @Kevin, that's taken from the PHPINFO() dump. Am I missing something? – RolandFlyBoy Apr 26 '11 at 17:39
  • trying really hard to help you but you're not making it easy! Firebug will show you what the browser is sending the server and nothing about PHP variable space. I can't help you any more unless you `echo()` – James C Apr 26 '11 at 18:04
  • that's v.strange. Could you try setting up a single standalone page that just contains the single echo $_POST (don't require in any other libraries, run any code, etc, etc) – James C Apr 26 '11 at 18:25
  • @James C, appreciate it. Echo of $_POST var gives blank for £ (works for other 'normal chars'....hmmm Here are the headers: Response Headers ... Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language en-us,en;q=0.5 Accept-Encoding gzip, deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive 115 Connection keep-alive X-Requested-With XMLHttpRequest Content-Type application/x-www-form-urlencoded; charset=UTF-8 Referer http://127.0.0.1/ Content-Length 114 – RolandFlyBoy Apr 26 '11 at 18:25
  • OK set up a two page sandbox. 1st file index.php contained a simple form, second server.php, echoes the $_POST var. Confirm £ is echoed correctly. So must be something with Codeigniter?? – RolandFlyBoy Apr 26 '11 at 18:39
  • Is this the same problem? http://stackoverflow.com/questions/5784207/post-empty-on-utf-8-characters – James C Apr 26 '11 at 18:41
  • Could be? Will post a CI question. Thanks for your help – RolandFlyBoy Apr 26 '11 at 19:07
  • This appears to only affect users using the latest version of MAMP and when using PHP 5.3.5. The current workaround is to go to the MAMP control panel and use the earlier version of PHP 5.2.17 For more details see: https://bitbucket.org/ellislab/codeigniter-reactor/issue/214/problem-when-inserting-special-characters – RolandFlyBoy Apr 26 '11 at 21:18
  • @Roland, you should post that as an answer and accept. Good find. I figured it was a server issue. – Kevin Peno Apr 26 '11 at 21:35