Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > 0afeee9cca140e167a996902b9a677c5 > files > 48

php-manual-en-4.3.0-2mdk.noarch.rpm

<HTML
><HEAD
><TITLE
>Using PHP</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="index.html"><LINK
REL="UP"
TITLE="FAQ: Frequently Asked Questions"
HREF="faq.html"><LINK
REL="PREVIOUS"
TITLE="Build Problems"
HREF="faq.build.html"><LINK
REL="NEXT"
TITLE="PHP and HTML"
HREF="faq.html.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="chapter"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="faq.build.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="faq.html.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="chapter"
><H1
><A
NAME="faq.using"
>Chapter 50. Using PHP</A
></H1
><P
>&#13;   This section gathers most common errors you can
   face, while writing PHP scripts.
  </P
><DIV
CLASS="qandaset"
><DL
><DT
>1. <A
HREF="faq.using.html#faq.using.anyform"
>&#13;      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?
     </A
></DT
><DT
>2. <A
HREF="faq.using.html#faq.using.singlequotes"
>&#13;      I need to convert all single-quotes (') to a backslash 
      followed by a single-quote. How can I do this with a 
      regular expression?
     </A
></DT
><DT
>3. <A
HREF="faq.using.html#faq.using.wrong-order"
>&#13;      When I do the following, the output is printed in 
      the wrong order: 
     <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>function myfunc($argument)
{
    echo $argument + 10;
}
$variable = 10;
echo "myfunc($variable) = " . myfunc($variable);</PRE
></TD
></TR
></TABLE
>
     what's going on?
     </A
></DT
><DT
>4. <A
HREF="faq.using.html#faq.using.newlines"
>&#13;      Hey, what happened to my newlines?
      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;pre&#62;
&#60;?php echo "This should be the first line."; ?&#62;
&#60;?php echo "This should show up after the new line above."; ?&#62;
&#60;/pre&#62;</PRE
></TD
></TR
></TABLE
>
     </A
></DT
><DT
>5. <A
HREF="faq.using.html#faq.using.headers-sent"
>&#13;      I get the message 'Warning: Cannot send session cookie - headers already
      sent...' or 'Cannot add header information - headers already sent...'.
     </A
></DT
><DT
>6. <A
HREF="faq.using.html#faq.using.header"
>&#13;      I need to access information in the request header directly. 
      How can I do this?
     </A
></DT
><DT
>7. <A
HREF="faq.using.html#faq.using.authentication"
>&#13;      When I try to use authentication with IIS I get 'No Input file specified'.
     </A
></DT
><DT
>8. <A
HREF="faq.using.html#faq.using.netscape"
>&#13;      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.
     </A
></DT
><DT
>9. <A
HREF="faq.using.html#faq.using.mixml"
>&#13;      How am I supposed to mix XML and PHP? It complains 
      about my &#60;?xml&#62; tags!
     </A
></DT
><DT
>10. <A
HREF="faq.using.html#faq.using.editor"
>&#13;      How can I use PHP with FrontPage or some other HTML editor
      that insists on moving my code around?
     </A
></DT
><DT
>11. <A
HREF="faq.using.html#faq.using.variables"
>&#13;      Where can I find a complete list of pre-set variables available 
      to me, and why are these not documented in the PHP documentation?
     </A
></DT
><DT
>12. <A
HREF="faq.using.html#faq.using.cgi-vars"
>&#13;      I'm trying to access one of the standard CGI
      variables (such as $DOCUMENT_ROOT or $HTTP_REFERER) in a user-defined
      function, and it can't seem to find it. What's wrong?
     </A
></DT
></DL
><DIV
CLASS="qandaentry"
><B
><DIV
CLASS="question"
><P
><A
NAME="faq.using.anyform"
></A
><B
>1. </B
>
      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?
     </P
></DIV
></B
><DIV
CLASS="answer"
><P
><B
> </B
>
      Make sure that the <A
HREF="configuration.directives.html#ini.track-vars"
>track_vars</A
>
      feature is enabled in your <TT
CLASS="filename"
>php.ini</TT
>
      file. Since PHP 4.0.3, this feature is always on. When
      <TT
CLASS="literal"
>track_vars</TT
> is on, it creates some
      associative arrays, the most important here is:
      <TT
CLASS="literal"
>$_POST</TT
> (this used to be called
      <TT
CLASS="literal"
>$HTTP_POST_VARS</TT
> in <TT
CLASS="literal"
>PHP</TT
> versions
      prior 4.1.0). So, to write a generic
      script to handle POST method variables you would
      need something similar to the following:
      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>foreach ($_POST as $var =&#62; $value) {
    echo "$var = $value&#60;br&#62;\n";
}</PRE
></TD
></TR
></TABLE
>
     </P
></DIV
></DIV
><DIV
CLASS="qandaentry"
><B
><DIV
CLASS="question"
><P
><A
NAME="faq.using.singlequotes"
></A
><B
>2. </B
>
      I need to convert all single-quotes (') to a backslash 
      followed by a single-quote. How can I do this with a 
      regular expression?
     </P
></DIV
></B
><DIV
CLASS="answer"
><P
><B
> </B
>
      First off, take a look at the <A
HREF="function.addslashes.html"
><B
CLASS="function"
>addslashes()</B
></A
>
      function. It will do exactly what you want. You should also have
      a look at the <A
HREF="ref.info.html#ini.magic-quotes-gpc"
>magic_quotes_gpc</A
> 
      directive in your <TT
CLASS="filename"
>php.ini</TT
> file.
     </P
></DIV
></DIV
><DIV
CLASS="qandaentry"
><B
><DIV
CLASS="question"
><P
><A
NAME="faq.using.wrong-order"
></A
><B
>3. </B
>
      When I do the following, the output is printed in 
      the wrong order: 
     <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>function myfunc($argument)
{
    echo $argument + 10;
}
$variable = 10;
echo "myfunc($variable) = " . myfunc($variable);</PRE
></TD
></TR
></TABLE
>
     what's going on?
     </P
></DIV
></B
><DIV
CLASS="answer"
><P
><B
> </B
>
      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 <A
HREF="function.return.html"
><B
CLASS="function"
>return()</B
></A
> the value, not <A
HREF="function.echo.html"
><B
CLASS="function"
>echo()</B
></A
> 
      it.
     </P
></DIV
></DIV
><DIV
CLASS="qandaentry"
><B
><DIV
CLASS="question"
><P
><A
NAME="faq.using.newlines"
></A
><B
>4. </B
>
      Hey, what happened to my newlines?
      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;pre&#62;
&#60;?php echo "This should be the first line."; ?&#62;
&#60;?php echo "This should show up after the new line above."; ?&#62;
&#60;/pre&#62;</PRE
></TD
></TR
></TABLE
>
     </P
></DIV
></B
><DIV
CLASS="answer"
><P
><B
> </B
>
      In PHP, the ending for a block of code is either "?&#62;" or
      "?&#62;\n" (where \n means a newline). So in the example above,
      the echoed sentences will be on one line, because PHP omits
      the newlines after the block ending. This means that you need to
      insert an extra newline after each block of PHP code to make
      it print out one newline.
     </P
><P
>&#13;      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.
     </P
></DIV
></DIV
><DIV
CLASS="qandaentry"
><B
><DIV
CLASS="question"
><P
><A
NAME="faq.using.headers-sent"
></A
><B
>5. </B
>
      I get the message 'Warning: Cannot send session cookie - headers already
      sent...' or 'Cannot add header information - headers already sent...'.
     </P
></DIV
></B
><DIV
CLASS="answer"
><P
><B
> </B
>
      The functions <A
HREF="function.header.html"
><B
CLASS="function"
>header()</B
></A
>, 
      <A
HREF="function.setcookie.html"
><B
CLASS="function"
>setcookie()</B
></A
> and the session functions need to add
      headers to the output stream. But headers can only be sent before all other
      content, check if your script is sending headers after having already sent 
      content.
     </P
></DIV
></DIV
><DIV
CLASS="qandaentry"
><B
><DIV
CLASS="question"
><P
><A
NAME="faq.using.header"
></A
><B
>6. </B
>
      I need to access information in the request header directly. 
      How can I do this?
     </P
></DIV
></B
><DIV
CLASS="answer"
><P
><B
> </B
>
      The <A
HREF="function.getallheaders.html"
><B
CLASS="function"
>getallheaders()</B
></A
> function will do this if 
      you are running PHP as an Apache module. So, the following bit
      of code will show you all the request headers:
      <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>$headers = getallheaders();
foreach ($headers as $name =&#62; $content) {
    echo "headers[$name] = $content&#60;br&#62;\n";
}</PRE
></TD
></TR
></TABLE
>
     </P
></DIV
></DIV
><DIV
CLASS="qandaentry"
><B
><DIV
CLASS="question"
><P
><A
NAME="faq.using.authentication"
></A
><B
>7. </B
>
      When I try to use authentication with IIS I get 'No Input file specified'.
     </P
></DIV
></B
><DIV
CLASS="answer"
><P
><B
> </B
>
      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. With the ISAPI
      module, this is not a problem. This should not effect other
      NT web servers. For more information, see: 
      <A
HREF="http://support.microsoft.com/support/kb/articles/q160/4/22.asp"
TARGET="_top"
>http://support.microsoft.com/support/kb/articles/q160/4/22.asp</A
>.
     </P
></DIV
></DIV
><DIV
CLASS="qandaentry"
><B
><DIV
CLASS="question"
><P
><A
NAME="faq.using.netscape"
></A
><B
>8. </B
>
      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.
     </P
></DIV
></B
><DIV
CLASS="answer"
><P
><B
> </B
>
      Netscape is more strict regarding html tags (such as tables) then 
      IE.  Running your html output through a html validator, such as 
      <A
HREF="http://validator.w3.org/"
TARGET="_top"
>validator.w3.org</A
>, might 
      be helpful.  For example, a missing &#60;/table&#62; might cause this.
     </P
><P
>&#13;      Also, both IE and Lynx ignore any NULs (<TT
CLASS="literal"
>\0</TT
>) in 
      the HTML stream, Netscape does not.  The best way to check for this is 
      to compile the <A
HREF="features.commandline.html"
>command line</A
> version of 
      PHP (also known as the CGI version) and run your script from the
      command line.  In *nix, pipe it through <TT
CLASS="literal"
>od -c</TT
> and look 
      for any <TT
CLASS="literal"
>\0</TT
> 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. 
     </P
></DIV
></DIV
><DIV
CLASS="qandaentry"
><B
><DIV
CLASS="question"
><P
><A
NAME="faq.using.mixml"
></A
><B
>9. </B
>
      How am I supposed to mix XML and PHP? It complains 
      about my &#60;?xml&#62; tags!
     </P
></DIV
></B
><DIV
CLASS="answer"
><P
><B
> </B
>
      You need to turn off the short tags by setting
      <A
HREF="configuration.directives.html#ini.short-open-tag"
>short_tags</A
> to 0 in your
      <TT
CLASS="filename"
>php.ini</TT
> file, or by using the appropriate
      Apache directive. You could even use a &#60;File&#62; section to
      do this selectively.
     </P
></DIV
></DIV
><DIV
CLASS="qandaentry"
><B
><DIV
CLASS="question"
><P
><A
NAME="faq.using.editor"
></A
><B
>10. </B
>
      How can I use PHP with FrontPage or some other HTML editor
      that insists on moving my code around?
     </P
></DIV
></B
><DIV
CLASS="answer"
><P
><B
> </B
>
      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 &#60;% and %&#62; code
      delimiters. Some of the popular HTML editors handle those more
      intelligently (for now). To enable the ASP-style tags, you need
      to set the <A
HREF="configuration.directives.html#ini.asp-tags"
>asp_tags</A
>
      <TT
CLASS="filename"
>php.ini</TT
> variable, or use the 
      appropriate Apache directive.
     </P
></DIV
></DIV
><DIV
CLASS="qandaentry"
><B
><DIV
CLASS="question"
><P
><A
NAME="faq.using.variables"
></A
><B
>11. </B
>
      Where can I find a complete list of pre-set variables available 
      to me, and why are these not documented in the PHP documentation?
     </P
></DIV
></B
><DIV
CLASS="answer"
><P
><B
> </B
>
      The best way is to stick a <TT
CLASS="literal"
>&#60;?php phpinfo(); ?&#62;</TT
>
      part 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.
     </P
></DIV
></DIV
><DIV
CLASS="qandaentry"
><B
><DIV
CLASS="question"
><P
><A
NAME="faq.using.cgi-vars"
></A
><B
>12. </B
>
      I'm trying to access one of the standard CGI
      variables (such as $DOCUMENT_ROOT or $HTTP_REFERER) in a user-defined
      function, and it can't seem to find it. What's wrong?
     </P
></DIV
></B
><DIV
CLASS="answer"
><P
><B
> </B
>
      Environment variables are normal global variables, so you must
      either declare them as global variables in your function (by using
      "<TT
CLASS="literal"
>global $DOCUMENT_ROOT;</TT
>", for example) or by using
      the global variable array (ie, "<TT
CLASS="literal"
>$GLOBALS["DOCUMENT_ROOT"]</TT
>").
     </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
       Since PHP 4.1.0 you can also use the superglobal array
       <TT
CLASS="literal"
>$_SERVER</TT
> which is available in every function.
       For example, you can now use <TT
CLASS="literal"
>$_SERVER["DOCUMENT_ROOT"]</TT
>
       instead of <TT
CLASS="literal"
>$DOCUMENT_ROOT</TT
>.
      </P
></BLOCKQUOTE
></DIV
></DIV
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="faq.build.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="faq.html.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Build Problems</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="faq.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>PHP and HTML</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>