Page 1 of 1

[Help] Annoying problem relating session functions!

PostPosted: 16. February 2007 03:32
by lucky_clover
This is quite weird problem relating session function of PHP. I think it may be because of settings in php.ini. I'm using xampp 1.5.5 and I don't modify any settings in php.ini, just fresh installation.

I have 2 files as follows:

index_en.php
Code: Select all
<?php
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Title in English</title>
</head>
<body>
<?php
   echo "Session ID: " . session_id();
?>
</body>
</html>


index_vn.php
Code: Select all
<?php
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Tiêu đề bằng tiếng Việt</title>
</head>
<body>
<?php
   echo "ID của session: " . session_id();
?>
</body>
</html>


I wrote both files using notepad. The first I saved as ASCII encoding text file, the second saved as UTF-8 encoding text file. When loaded in browser, the index_en.php is fine, but the index_vn.php gave these warning:
Code: Select all
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\test\index_vn.php:1) in C:\xampp\htdocs\test\index_vn.php on line 2

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\test\index_vn.php:1) in C:\xampp\htdocs\test\index_vn.php on line 2


Also, the index_vn.php created different session ids every time I reload the page, that's odd.

I uploaded these two to a host and they run correctly. You can see these pages here:
http://troyvn.byethost31.com/temp/index_en.php
http://troyvn.byethost31.com/temp/index_vn.php

I think this problem may be caused by settings in php.ini, but I don't know how to correct it. Anyone has any idea?

PostPosted: 16. February 2007 09:32
by Wiedmann
the second saved as UTF-8 encoding text file
Code: Select all
<?php
session_start();
?>

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\xampp\htdocs\test\index_vn.php:1)

There is an utf-8 bom at the beginning of your file. (reconfigure your editor to: don't write a bom with utf-8 files).

PostPosted: 16. February 2007 11:11
by lucky_clover
Wiedmann wrote:There is an utf-8 bom at the beginning of your file. (reconfigure your editor to: don't write a bom with utf-8 files).


Thanks a lot! I use UltraEdit and configure it to not write UTF-8 bom, and the problem's gone! But I still wonder why this problem doesn't occur when I uploaded them to my host! Does Linux treat UTF-8 text files differently from Windows?

PostPosted: 16. February 2007 11:58
by Wiedmann
Does Linux treat UTF-8 text files differently from Windows?

No.

But I still wonder why this problem doesn't occur when I uploaded them to my host!

a) you transfer the files in text mode and the ftp client removes the bom (convert dos to unix line endings) (btw, I don't know if a client really remove the bom in this case)

b) or output buffering is enabled at your unix host.