title: Please help me trace how charsets are handled every step of the way
body: <p>We all know how <a href="http://www.joelonsoftware.com/articles/Unicode.html" rel="nofollow noreferrer">easy character sets are on the web</a>, yet every time you think you got it right, a foreign charset bites you in the butt. So I'd like to trace the steps of what happens in a fictional scenario I will describe below. I'm going to try and put down my understanding as well as possible but my question is for you folks to correct any mistakes I make and fill in any BLANKs.</p>
  
  <p>When reading this scenario, imagine that this is being done on a Mac by John, and on Windows by Jane, and add comments if one behaves differently than the other in any particular situation.</p>
  
  <hr>
  
  <p>Our hero (John/Jane) starts by writing a paragraph in Microsoft Word. Word's charset is BLANK1 (<a href="http://en.wikipedia.org/wiki/Windows-1252" rel="nofollow noreferrer">CP1252</a>?).</p>
  
  <p>S/he copies the paragraph, including smart quotes (e.g. “ ”). The act of copying is done by the BLANK2 (Operating system...Windows/Mac?) which BLANK3 (detects what charset the application is using and inherits the charset?). S/he then pastes the paragraph in a text box at StackOverflow. </p>
  
  <p>Let's assume StackOverflow is running on Apache/PHP and that their set up in httpd.conf does not specify <a href="http://httpd.apache.org/docs/2.2/mod/core.html#adddefaultcharset" rel="nofollow noreferrer">AddDefaultCharset utf-8</a> and their php.ini sets the <a href="http://us3.php.net/manual/en/ini.core.php" rel="nofollow noreferrer">default_charset</a> to ISO-8859-1. </p>
  
  <p>Yet neither charset above matters, because Stack Overflow's header contains this statement <a href="http://www.w3.org/TR/REC-html40/charset.html" rel="nofollow noreferrer">META http-equiv="Content-Type" content="text/html; charset=UTF-8"</a>, so even though when you clicked on "Ask Question" you might have seen a *RESPONSE header in <a href="http://getfirebug.com/" rel="nofollow noreferrer">firebug</a> of "Content-type text/html;" ... in fact, Firefox/IE/Opera/Other browsers BLANK4 (completely 100% ignore the server header and override it with the Meta Content-type declaration in the header? Although it must read the file before knowing the Content-type, since it doesn't have to do anything with the encoding until it displays the body, this makes no different to the browser?). </p>
  
  <p>Since the Meta Content-type of the page is UTF-8, the input form will convert any characters you type into the box, into UTF-8 characters. BLANK5 (If someone can go into excruciating detail about what the browser does in this step, it would be very helpful...here's my understanding...since the operating system controls the clipboard and display of the character in the form, it inserts the character in whatever charset it was copied from. And displays it in the form as that charset...OVERRIDING the UTF-8 in this example).</p>
  
  <p>Let's assume the form method=GET rather than post so we can play w/ the URL browser input.... Continuing our story, the form is submitted as UTF-8. The smart quotes which represent decimal code 147 &amp; 148, when the browser converts them to UTF-8, it gets transformed into BLANK6 characters.</p>
  
  <p>Let's assume that after submission, Stack Overflow found an error in the form, so rather than displaying the resulting question, it pops back up the input box with your question inside the form. In the php, the form variables are escaped with htmlspecialchars($var) in order for the data to be properly displayed, since this time it's the BLANK7 (browser controlling the display, rather than the operating system...therefore the quotes need to be represented as its UTF-8 equivalent or else you'd get the dreaded funny looking � question mark?)</p>
  
  <p>However, if you take the smart quotes, and insert them directly in the URL bar and hit enter....the htmlspecialchars will do BLANK8, messing up the form display and inserting question marks �� since querying a URL directly will just use the encoding in the url...or even a BLANK9 (mix of encodings?) if you have more than one in there...</p>
  
  <p>When the REQUEST is sent out, the browser lists acceptable charsets to the browser. The list of charsets comes from BLANK10.</p>
  
  <p>Now you might think our story ends there, but it doesn't. Because StackOverflow needs to save this data to a database. Fortunately, the people running this joint are smart. So when their MySQL client connects to the database, it makes sure the client and server are talking to each other UTF-8 by issuing the <a href="http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html" rel="nofollow noreferrer">SET NAMES UTF-8</a> command as soon as the connection is initiated. Additionally, the default character set for MySQL is set to UTF-8 and each field is set the same way.</p>
  
  <p>Therefore, Stack Overflow has completely secured their website from dB injections, CSRF forgeries and XSS site scripting issues...or at least those borne from charset game playing.</p>
  
  <p>*Note, this is an example, not the actual response by that page.</p>