php-doc-en/faq/using.xml

377 lines
13 KiB
XML
Executable file

<?xml encoding="iso-8859-1"?>
<!-- $Revision: 1.5 $ -->
<chapter id="faq.using">
<title>Using PHP</title>
<titleabbrev>Using PHP</titleabbrev>
<para>
This section gathers most common errors that occur at
build time.
</para>
<qandaset>
<qandaentry id="faq.using.anyform">
<question>
<para>
I would like to write a generic PHP script that can handle data coming
from any form. How do I know which POST method variables are available?
</para>
</question>
<answer>
<para>
Make sure that the track_vars feature is
enabled in your php3.ini file. If you compiled PHP with
&quot;--enable-track-vars&quot; it will be on by default.
Alternatively you can enable it at run-time on a per-script
basis by putting &lt;?php_track_vars?&gt; at the top of your file.
When track_vars is on, it creates three associative arrays.
$HTTP_GET_VARS, $HTTP_POST_VARS and $HTTP_COOKIE_VARS. So, to
write a generic script to handle POST method variables you would
need something similar to the following:
<programlisting>
while (list($var, $value) = each($HTTP_POST_VARS)) {
echo "$var = $value&lt;br&gt;\n";
}
</programlisting>
</para>
</answer>
</qandaentry>
<qandaentry id="faq.using.singlequotes">
<question>
<para>
I need to convert all single-quotes (') to a backslash
followed by a single-quote. How can I do this with a
regular expression?
</para>
</question>
<answer>
<para>
First off, take a look at the <function>addslashes()</function>
function. It will do exactly what you want. You should also have
a look at the <link linkend="ini.magic-quotes-gpc">magic_quotes_gpc</link>
directive in your <filename>php.ini</filename> file.
</para>
</answer>
</qandaentry>
<qandaentry id="faq.using.wrong_order">
<question>
<para>
When I do the following, the output is printed in
the wrong order:
<programlisting>
function myfunc($argument) {
echo $argument + 10;
}
$variable = 10;
echo "myfunc($variable) = " . myfunc($variable);
</programlisting>
what's going on?
</para>
</question>
<answer>
<para>
To be able to use the results of your function in an expression (such
as concatenating it with other strings in the example above), you need
to <emphasis>return</emphasis> the value, not <function>echo</function>
it.
</para>
</answer>
</qandaentry>
<qandaentry id="faq.using.newlines">
<question>
<para>
Hey, what happened to my newlines?
<programlisting>
&lt;PRE&gt;
1 &lt;?echo $result[1];?&gt;
2 &lt;?echo $result[2];?&gt;
</programlisting>
</para>
</question>
<answer>
<para>
In PHP, the ending for a block of code is either "?&gt;" or
"?&gt;\n" (where \n means a newline). This means that you need to
insert an extra newline after each block of PHP code in the above
example.
</para>
<para>
Why does PHP do this? Because when formatting normal HTML, this
usually makes your life easier because you don't want that newline,
but you'd have to create extremely long lines or otherwise make the
raw page source unreadable to achieve that effect.
</para>
</answer>
</qandaentry>
<qandaentry id="faq.using.header">
<question>
<para>
I need to access information in the request header directly.
How can I do this?
</para>
</question>
<answer>
<para>
The <function>getallheaders</function> function will do this if
you are running PHP as a module. So, the following bit of code
will show you all the request headers:
<programlisting>
$headers = getallheaders();
for(reset($headers); $key = key($headers); next($headers)) {
echo "headers[$key] = ".$headers[$key]."&lt;br&gt;\n";
}
</programlisting>
</para>
</answer>
</qandaentry>
<qandaentry id="faq.using.authentication">
<question>
<para>
When I try to use authentication with IIS I get 'No Input file specified'.
</para>
</question>
<answer>
<para>
The security model of IIS is at fault here. This is a problem
common to all CGI programs running under IIS. A workaround is
to create a plain HTML file (not parsed by php) as the entry page
into an authenticated directory. Then use a META tag to redirect
to the PHP page, or have a link to the PHP page. PHP will
then recognize the authentication correctly. When the ISAPI
module is ready, this will no longer be a problem. This should
not effect other NT web servers. For more information, see:
<ulink url="&faqurl.iis;">&faqurl.iis;</ulink>.
</para>
</answer>
</qandaentry>
<qandaentry id="faq.using.PHP-IIS">
<question>
<para>
I've followed all the instructions, but still can't
get PHP and IIS to work together!
</para>
</question>
<answer>
<para>
Make sure any user who needs to run a PHP script has the rights
to run <filename>php.exe</filename>! IIS uses an anonymous user which is added at the
time IIS is installed. This user needs rights to <filename>php.exe</filename>.
Also, any authenticated user will also need rights to execute
<filename>php.exe</filename>. And for IIS4 you need to tell it
that PHP is a script engine.
</para>
</answer>
</qandaentry>
<qandaentry id="faq.using.netscape">
<question>
<para>
My PHP script works on IE and Lynx, but on Netscape some of
my output is missing. When I do a "View Source" I see the
content in IE but not in Netscape.
</para>
</question>
<answer>
<para>
Very good question! ;) This is a tricky little issue and it has come up
twice in the past month as of this writing. Both times I ended up
spending a good 20 minutes trying to figure out what the heck was going
on. The answer is that both IE and Lynx ignore any NULs
(<literal>\0</literal>) in the HTML stream. Netscape does not.
The best way to check for this is to compile the command-line version
of PHP (also known as the CGI version) and run your script from the
command line and pipe it through 'od -c' and look for any
<literal>\0</literal> characters. (If you are on Windows you need to
find an editor or some other program that lets you look at binary files)
When Netscape sees a NUL in a file it will typically not output
anything else on that line whereas both IE and Lynx will. If this
issue has bitten you, congratulations! You are not alone.
</para>
</answer>
</qandaentry>
<qandaentry id="faq.using.mixml">
<question>
<para>
How am I supposed to mix XML and PHP? It complains
about my &lt;?xml&gt; tags!
</para>
</question>
<answer>
<para>
You need to turn off the short tags by setting short_tags to 0 in your
<filename>php.ini</filename> file, or by using the php3_short_tags Apache
directive. (You could even use a &lt;File&gt; section to do this
selectively.) You can also disable and re-enable the short tags
in your script using the short_tags() function.
</para>
</answer>
</qandaentry>
<qandaentry id="faq.using.editor">
<question>
<para>
How can I use PHP with FrontPage or Dreamweaver or some
other HTML editor that insists on moving my code around?
</para>
</question>
<answer>
<para>
One of the easiest things to do is to enable using ASP tags in your
PHP code. This allows you to use the ASP-style &lt;% and %&gt; code
delimiters. Most of the popular HTML editors handle those more
intelligently (for now). To enable the ASP-style tags, you need
to set the asp_tags <filename>php.ini</filename> variable, or use the
<link linkend="ini.asp-tags">asp_tags</link> Apache directive.
</para>
</answer>
</qandaentry>
<qandaentry id="faq.using.variables">
<question>
<para>
Where can I find a complete list of pre-set variables available
to me, and why are these not documented in the PHP documentation?
</para>
</question>
<answer>
<para>
The best way is to stick a &lt;?phpinfo()?&gt; tag on a page and
load it up. This will show you all sorts of information about your
PHP setup, including a list of both environment variables and also
special variables set by your web server. This list can't really be
documented in the PHP documentation because it will change from one
server to another.
</para>
</answer>
</qandaentry>
<qandaentry id="faq.using.mysql">
<question>
<para>
Why do I get an error that looks something like this:
"Warning: 0 is not a MySQL result index in &lt;file&gt;
on line &lt;x&gt;" or "Warning: Supplied argument is not
a valid MySQL result resource in &lt;file&gt; on line &lt;x&gt;?
</para>
</question>
<answer>
<para>
You are trying to use a result identifier that is 0. The 0 indicates
that your query failed for some reason. You need to check for errors
after submitting a query and before you attempt to use the returned
result identifier. The proper way to do this is with code similar
to the following:
<programlisting>
$result = mysql_query("select * from tables_priv");
if(!$result) {
echo mysql_error();
exit;
}
</programlisting>
or
<programlisting>
$result = mysql_query("select * from tables_priv")
or die("Bad query: ".mysql_error());
</programlisting>
</para>
</answer>
</qandaentry>
<qandaentry id="faq.using.form-image">
<question>
<para>
I'm trying to use an &lt;input type="image"&gt; tag, but
the $foo.x and $foo.y variables aren't available.
Where are they?
</para>
</question>
<answer>
<para>
When submitting a form, it is possible to use an image instead of
the standard submit button with a tag like:
<programlisting>
&lt;input type="image" SRC="image.gif" NAME="foo"&gt;
</programlisting>
When the user clicks somewhere on the image, the accompanying form
will be transmitted to the server with two additional variables:
foo.x and foo.y.
</para>
<para>
Because $foo.x and $foo.y are invalid variable names in PHP, they are
automagically converted to $foo_x and $foo_y. That is, the periods
are replaced with underscores.
</para>
</answer>
</qandaentry>
<qandaentry id="faq.using.select-multiple">
<question>
<para>
How do I get all the results from a SELECT MULTIPLE HTML tag?
</para>
</question>
<answer>
<para>
The SELECT MULTIPLE tag in an HTML construct allows users to select multiple
items from a list. These items are then passed to the action handler for the
form. The problem is that they are all passed with the same widget name. ie.
<programlisting>
&lt;SELECT NAME="var" MULTIPLE&gt;
</programlisting>
Each selected option will arrive at the action handler as:
<programlisting>
var=option1
var=option2
var=option3
</programlisting>
Each option will overwrite the contents of the previous $var variable. The
solution is to use PHP's
non-indexed array feature. The following should be used:
<programlisting>
&lt;SELECT NAME="var[]" MULTIPLE&gt;
</programlisting>
This tells PHP to treat <varname>var</varname> as an array and each assignment of a
value to var[] adds an item to the array. The first item becomes
<varname>$var[0]</varname>, the next <varname>$var[0]</varname>, etc.
The <function>count</function> function can be used to determine how
many options were selected, and the <function>sort</function> function can be
used to sort the option array if necessary.
</para>
<para>
Note that if you are using JavaScript the <literal>[]</literal> on the element name
might cause you problems when you try to refer to the element by name.
Use it's numerical form element id instead, or enclose the
variable name in single quotes and use that as the index to the
elements array, for example:
<programlisting>
variable = documents.forms[0].elements['var[]'];
</programlisting>
</para>
</answer>
</qandaentry>
</qandaset>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
-->