cvs: phpdoc /en/faq html.xml

From: Date: Tue, 06 Nov 2001 20:51:27 +0000
Subject: cvs: phpdoc /en/faq html.xml
Groups: php.doc 
Request: Send a blank email to phpdoc+get-969338522@lists.php.net to get a copy of this message
jeroen Tue Nov 6 15:51:27 2001 EDT Modified files: /phpdoc/en/faq html.xml Log: Add a FAQ-entry about encoding for forms and urls. Should probably be moved, and definitely be reviewed. Feel free to do so. Index: phpdoc/en/faq/html.xml diff -u phpdoc/en/faq/html.xml:1.7 phpdoc/en/faq/html.xml:1.8 --- phpdoc/en/faq/html.xml:1.7 Wed Oct 31 03:05:51 2001 +++ phpdoc/en/faq/html.xml Tue Nov 6 15:51:27 2001 @@ -1,15 +1,103 @@ <?xml encoding="iso-8859-1"?> -<!-- $Revision: 1.7 $ --> +<!-- $Revision: 1.8 $ --> <chapter id="faq.html"> <title>PHP and HTML</title> <titleabbrev>PHP and HTML</titleabbrev> <para> - PHP and HTML interact a lot : PHP generate HTML, and HTML + PHP and HTML interact a lot: PHP generate HTML, and HTML has informations that will be sent to PHP. </para> <qandaset> + <qandaentry id="faq.html.encoding"> + <question> + <para> + What encodings/decodings do I need when I pass a value on via a form? + And via an URL? + </para> + </question> + <answer> + <para> + There are several stages for which encoding is important. Assuming that + you have a <type>string</type> <varname>$data</varname>, which contains + the string you want to pass on in a non-encoded way, these are the + relevant stages: + <itemizedlist> + <listitem> + <para> + HTML interpretation. In order to specify a random string, you + <emphasis>must</emphasis> include it in double quotes, and htmlescape + the the whole value. + </para> + </listitem> + <listitem> + <para> + URL: A URL consists of several parts. If you want your data to be + interpreted as one item, you <emphasis>must</emphasis> encode it with + <function>urlencode</function>. + </para> + </listitem> + </itemizedlist> + </para> + <para> + <example> + <title>A hidden HTML form element</title> + <programlisting role="php"> +<![CDATA[ +<?php + echo "<input type=hidden value=\"" . htmlescape($data) . "\">\n"; +?> +]]> + </programlisting> + </example> + <note> + <simpara> + It is wrong to <function>urlencode</function> + <varname>$data</varname>, because it's the browsers reposability to + <function>urlencode</function> the data. All popular browsers do that + correctly. Note that this will happen regardless of the method (i.e., + GET or POST). You'll only notice this in case of GET request though, + because POST requests are usually hidden. + </simpara> + </note> + <example> + <title>In an URL</title> + <programlisting role="php"> +<![CDATA[ +<?php + echo "<a href=\"" . htmlescape("/nexpage.php?stage=23&data=" . + urlencode($data)) . "\">\n"; +?> +]]> + </programlisting> + </example> + <note> + <simpara> + In fact you are faking a HTML GET request, therefore it's necessary to + manually <function>urlencode</function> the data. + </simpara> + </note> + <note> + <simpara> + You need to <function>htmlescape</function> the whole URL, because the + URL occurs as value of an HTML-attribute. In this case, the browser + will first un-<function>htmlescape</function> the value, and then pass + the URL on. PHP will understand the URL correcly, because you + <function>urlencoded</function> the data. + </simpara> + <simpara> + You'll notice that the <literal>&amp;</literal> in the URL is replaced + by <literal>&amp;amp;</literal>. Although most browsers will recover + if you forget this, this isn't always possible. So even if your URL is + not dynamic, you <emphasis>need</emphasis> to + <function>htmlescape</function> the URL. + </simpara> + </note> + </para> + </answer> + </qandaentry> + <qandaentry id="faq.html.form-image"> <question> <para>

« previous php.doc (#969338522) next »