1

I need to find out at runtime what charset is currently used on virtual host. E.g. it might be defined in .htaccess file (AddDefaultCharset directive) or in httpd.conf

heximal
  • 10,327
  • 5
  • 46
  • 69
  • I think this question belongs on serverfault. – krtek Mar 16 '11 at 13:29
  • why do you need to find this out? – ax. Mar 16 '11 at 13:35
  • Do you want to determinate charset or just set it up? – quantme Mar 16 '11 at 13:36
  • i want to determine. something like: if ($ServerCharset=='UTF8') doUTF8stuff(); – heximal Mar 16 '11 at 13:37
  • the charset might be different for different files, so what does a default charset help you? – ax. Mar 16 '11 at 13:39
  • ax: although the question is not about this, i'll answer. i need to know virtualhost charset because the php script must convert data retrieved from db to current charset on-the-fly. and finally user will see correct symbols. of course we may configure php script so it will permanenlty convert data from one charset to another, but my idea to implement universal method. so now i'm trying to realize, is it possible at all? – heximal Mar 16 '11 at 13:46
  • @heximal I don't know too much about .htaccess but I think is a good solution, try something like this http://stackoverflow.com/questions/1234919/can-i-do-an-if-then-else-in-htaccess/1239819#1239819 – quantme Mar 16 '11 at 13:48
  • 4
    @heximal i don't see the point. what has the virtualhost to do with serving data from the database to the client? a virtual host does not *have* a charset. [AddDefaultCharset](http://httpd.apache.org/docs/2.2/mod/core.html#adddefaultcharset) is only a fallback for content that cannot label their charset individually. a php script definitely can (`header('Content-type: text/html; charset=utf-8');`). – ax. Mar 16 '11 at 14:00
  • let's look at the situation. client has loaded web page that was generated by php script. this page was returned in utf8 encoding. from that page user submits ajax-request. on server side the script connects to db, selects some data in ISO 8859-1 charset and returns to client. what will user see? – heximal Mar 16 '11 at 14:13
  • i don't know - you tell me :) (*) my point is that this whole scenario is completely independent of any virtualhost setting. – ax. Mar 16 '11 at 14:34
  • (*) i would guess that if the client accepts ISO-8859-1, and if the ajax response comes with `Content-Type: text/html; charset=ISO-8859-1`, it would display just ok. if it doesn't, convert the result of the db query to utf8. or, much better, convert the whole database to utf8 and use this encoding for all transactions. – ax. Mar 16 '11 at 14:39
  • that is! the script does not know what default charset has its virtual host. so it does not know to which chaset to convert db result. ok, i think, the better solution is to give user script content in some known charset (e.g.) when first launch, and then convert all futher resonse to that charset. Header('Content-type'... will help me)) – heximal Mar 16 '11 at 14:46
  • please forget the virtual host - it is completely irrelevant! the database result has to be served in/converted to any of the encodings accepted by the client (request header [`Accept-Charset`](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3) might be a hint), and this choosen charset has to be advised in the response (`Content-type`, see above). that's it. – ax. Mar 16 '11 at 15:20

1 Answers1

0

I think you should bind to charset desired by client browser. Take a closer look at $_SERVER["HTTP_ACCEPT_CHARSET"].

Slava
  • 2,040
  • 15
  • 15
  • I don't think you'll be able to get value of that directive other way than by reading `.htaccess` file. However it is possible that PHP won't have permission to read it, depends on server configuration. – Slava Mar 16 '11 at 14:09