章 50. Using PHP

This section gathers most common errors you can face, while writing PHP scripts.

1. 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?
2. I need to convert all single-quotes (') to a backslash followed by a single-quote. How can I do this with a regular expression?
3. When I do the following, the output is printed in the wrong order:
function myfunc($argument)
{
    echo $argument + 10;
}
$variable = 10;
echo "myfunc($variable) = " . myfunc($variable);
what's going on?
4. Hey, what happened to my newlines?
 pre 
 ?php echo "This should be the first line."; ? 
 ?php echo "This should show up after the new line above."; ? 
 /pre 
5. I get the message 'Warning: Cannot send session cookie - headers already sent...' or 'Cannot add header information - headers already sent...'.
6. I need to access information in the request header directly. How can I do this?
7. When I try to use authentication with IIS I get 'No Input file specified'.
8. 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.
9. How am I supposed to mix XML and PHP? It complains about my ?xml tags!
10. How can I use PHP with FrontPage or some other HTML editor that insists on moving my code around?
11. Where can I find a complete list of pre-set variables available to me, and why are these not documented in the PHP documentation?
12. 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?

Make sure that the track_vars feature is enabled in your php.ini file. Since PHP 4.0.3, this feature is always on. When track_vars is on, it creates some associative arrays, the most important here is: $_POST (this used to be called $HTTP_POST_VARS in PHP versions prior 4.1.0). So, to write a generic script to handle POST method variables you would need something similar to the following:

foreach ($_POST as $var =  $value) {
    echo "$var = $value br \n";
}


The functions header(), setcookie() 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.

6. I need to access information in the request header directly. How can I do this?

The getallheaders() 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:

$headers = getallheaders();
foreach ($headers as $name =  $content) {
    echo "headers[$name] = $content br \n";
}


Netscape is more strict regarding html tags (such as tables) then IE. Running your html output through a html validator, such as validator.w3.org, might be helpful. For example, a missing /table might cause this.

Also, both IE and Lynx ignore any NULs (\0) 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. In *nix, pipe it through od -c and look for any \0 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.

9. How am I supposed to mix XML and PHP? It complains about my ?xml tags!

You need to turn off the short tags by setting short_tags to 0 in your php.ini file, or by using the appropriate Apache directive. You could even use a File section to do this selectively.

10. How can I use PHP with FrontPage or some other HTML editor that insists on moving my code around?

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 % and % 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 asp_tags php.ini variable, or use the appropriate Apache directive.