Writing PHP - 3

PHP and HTTP: headers and variables.

PHP files can contain a mixture of code and you can insert php code anywhere in the document. Code sections begin with

<?php or <script language="php"

Some PHP installations will allow a shortened tag <? but this will clash with any XML or WML output so check your phpinfo output for "short_open_tag off" before trying XML and PHP. HTML, XML and WML code can be entered as a PHP variable using

$variable="my content"

If the content includes text quotes (e.g. tag attributes), use the backslash to "escape" the quotation marks - \" e.g.

$html_output="<html> <body> 
<a href=\"test.php\">test</a> </body> </html>";

The Header function can be used to output formats other than the default HTML. For WML use the PHP command:

Header("Content-type:text/vnd.wap.wml");

Only set the Header once for each possible non-HTML output of the PHP file. Duplicate Header assignments will generate PHP parsing errors. HTTP header information is always available in all PHP files - for a full list see the phpinfo output - so detecting the browser USER_AGENT setting is straighforward:

$agent = getenv("HTTP_USER_AGENT");

To check for a match with Internet Explorer 5, use a Perl type regular expression:

if (preg_match("/MSIE 5/i", "$agent")) {echo ("$ie5");exit;}

Detect WML browsers (like WAP phones) through the use of getenv("HTTP_ACCEPT"). WML browsers set this value to text/vnd.wap.wml - amongst other settings.



This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for copying conditions.