1

QUESTION: Hello stackoverflow! So this encoding stuff is getting on my last nerve. Not enough that it is difficult to figure out what the best combination of encodings needs to be when sending stuff forth and back using AJAX and PHP and SQL etc.. But it also causing problems with SESSION???!

So basically I already found a hot-fix solution no-thanks to google, partly the reason I'm writing this now. But I would also like to see if anyone of you actually have any more practical solution.

PROBLEM: For example if I want my PHP file to have UTF-8 encoding, it then adds hidden characters in the file which then can only be viewed and deleted in a hex-editor. For those that don't know, YES any extra characters that aren't commented out will cause problems with SESSION and give you header error. So when I delete them, and re-upload the file, it falls back to ANSI encoding. Maybe there are different editors that can encode files more properly into UTF-8? I don't know, I'm using Notepad++ at the moment and am perfectly happy with it and it is hard to believe it should cause problems with encoding. I have also tried to change my default encoding in .htaccess file and no difference for the index file anyways.

Vadim Cool
  • 129
  • 10
  • So, the question is what? What did you code, what output did you expected and what did you get? – Hille Aug 15 '15 at 21:14
  • What I coded doesn't matter since the code is not on trial here, but SESSION and encoding is. So this question is irrelevant really. As for what I expected is clear enough, to set SESSIONS in index.php that is UTF-8 encoded PHP file. And as for the error, you all know it: `session_start(): Cannot send session cache limiter - headers already sent ...` – Vadim Cool Aug 15 '15 at 21:20
  • But my scripting purposes are a bit harder to explain. Because I copy characters from a different website which are UTF-8 encoded. And then I'm passing them into ANSI encoded file. And that returns to me something entirely different. So for example this trademark `™` sign will become `â„¢` in a ANSI encoded php file. And that is how it will be shown on the page with weird characters instead of actual TM sign. And there are some signs more difficult to maintain and replace than others and don't have a back-up replacement for HTML such as `™` – Vadim Cool Aug 15 '15 at 21:41
  • I have though found out that you can use `rawurlencode();` to preserve the original characters and then you can still print them in whatever encoded file format and they will adjust themselves accordingly since url encoding is good at interpreting and preserving characters as it meets them. The problem may occur when those characters need encoding while still in JSON format. But for that I guess we can make a small white-list of characters that it should re-encode back again to their original form before PARSING. So basically, no real need for UTF-8 file format to display special characters. – Vadim Cool Aug 16 '15 at 19:20

3 Answers3

0

It seems, although we get WARNING: session_start(): Cannot send session cache limiter - headers already sent ... the sessions are still set perfectly fine and all we could do at this point is simply turning off warning errors by placing this on top of our php file: error_reporting(~E_NOTICE & ~E_WARNING); although this doesn't really solve our problem and simply hiding it from public eye.

Vadim Cool
  • 129
  • 10
0

Page open Notepad2 or Sublime Text -> Save with Encoding -> UTF-8

index.php

<?php 
 session_start();
 header('Content-type: text/html; charset=utf-8');

 echo 'Hello ÇÖİŞÜĞüğışçö'; // bla bla
?>
Limitless isa
  • 3,689
  • 36
  • 28
-1

SOLUTION: I had to therefore make a fix by making 2 separate files for one and the same index, just with different encoding. Like my main file is ANSI encoded and called

index.php

that will have session_start(); line in it and beneath it we include our main scripts that were originally supposed to be there, but instead now included with this include('index_.php'); ................ Also I found out that this problem will NOT occur on all hosting servers, but only some. So the real solution may be found trough somewhere in the server settings.

Vadim Cool
  • 129
  • 10
  • More practical solution was NOT found in here: http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Vadim Cool Aug 15 '15 at 22:41