note 78833 modified in function.setcookie by danbrown

From: Date: Sun, 17 May 2009 17:48:13 +0000
Subject: note 78833 modified in function.setcookie by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155025@lists.php.net to get a copy of this message
Problem with setcookie() and UTF-8 recognizing by browser: - If you want to use UTF-8 characters in your php file, some editors insert special bytes in the very beginning of the file. This prevents setcookie() from working, because these special bytes are sent to the browser BEFORE the header, and you get "Header already sent" error; - If you delete these bytes (with a hex editor), setcookie() will work fine, but the browser will STOP recognizing UTF-8 encoding automatically! The user will need to set the encoding to UTF-8 manually to see your page correctly. Here's how to get out of this: Instead of: <?php setcookie("aaa", "bbb"); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> Write this: <?php header("Content-Type: text/html; charset=utf-8"); setcookie("aaa", "bbb"); ?><html> <head> ... (make sure you have no special bytes before "<?") --was-- Problem with setcookie() and UTF-8 recognizing by browser: - If you want to use UTF-8 characters in your php file, some editors insert special bytes in the very beginning of the file. This prevents setcookie() from working, because these special bytes are sent to the browser BEFORE the header, and you get "Header already sent" error; - If you delete these bytes (with a hex editor), setcookie() will work fine, but the browser will STOP recognizing UTF-8 encoding automatically! The user will need to set the encoding to UTF-8 manually to see your page correctly. Here's how to get out of this: Instead of: <? setcookie("aaa", "bbb"); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> Write this: <? header("Content-Type: text/html; charset=utf-8"); setcookie("aaa", "bbb"); ?> <html> <head> ... (make sure you have no special bytes before "<?") http://php.net/manual/en/function.setcookie.php

« previous php.notes (#155025) next »