[solved] Sessions and UTF8!?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

[solved] Sessions and UTF8!?

Post by Sindarin »

In every document I create nowadays, it's in utf-8. Problem is, it doesn't work with session_start().
A file encoded with ANSI will work fine, but a UTF8 file will spit out the error that it cannot send headers.
Any help?
Last edited by Sindarin on Sat Aug 30, 2008 7:56 pm, edited 1 time in total.
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Sessions and UTF8!?

Post by marcth »

I have saved files UTF-8 and have not had problems using PHP sessions.

Why do you want to save your PHP files in UTF-8 anyway? Won't ANSi work?

The PHP code you write may deal with foreign characters, but the code that executes it can be saved under the ANSI character set. How PHP deals with with foreign characters is a different story--maybe http://www.php.net has an entry on i18n?

Edit: This was under linux with the appropriate UTF-8 character set installed.

Re-edit: Come to think of it, I've done it under windows too.
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Sessions and UTF8!?

Post by Sindarin »

Nope ANSI won't maintain the characters and I'll have to use html chars like &sigma &omicron etc. UTF8 gives the comfort to use any character, so the script remains actually readable.

I found the problem is the BOM signature. It seems the signature leaves some unwanted invisible characters at the start of the file (I saw them with a hex editor) and when php reads the file it also accounts these characters so it supposes there is data before session_start() and throws the error. So, I did use UTF8 without BOM and it works perfectly now.

Why they won't settle down their standards and save us some headache? :drunk:
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: [solved] Sessions and UTF8!?

Post by marcth »

Damn those BOM signatures! :D
jing007
Forum Newbie
Posts: 1
Joined: Sun Oct 05, 2008 5:07 pm

Re: [solved] Sessions and UTF8!?

Post by jing007 »

What is "BOM signature"?

Thanks.
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: [solved] Sessions and UTF8!?

Post by Sindarin »

http://en.wikipedia.org/wiki/Byte_Order_Mark

Also found,
Many Windows programs (including Windows Notepad) add BOM's to UTF-8 files. However in Unix-like systems (which make heavy use of text files for file formats as well as for inter-process communication) this practice is not recommended, as it will interfere with correct processing of important codes such as the hash-bang at the start of an interpreted script. It may also interfere with source for programming languages that don't recognise it. For example, gcc reports stray characters at the beginning of a source file, and in PHP, if output buffering is disabled, it has the subtle effect of causing the page to start being sent to the browser, preventing custom headers from being specified by the PHP script.
Post Reply