Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

Writing PHP

By Neil Williams
2007-11-11


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.



Tutorial Pages:
» What can PHP do?
» PHP output
» PHP and HTTP: headers and variables
» No need for CGI access
» Preventing and solving bugs in PHP
» Checking user input for dubious or erroneous values
» Read remote files, read and write local files on the server
» Common PHP functions
» Building, sorting, reversing and flipping arrays
» Current time, date calculations, timestamp formats
» PHP string functions


Copyright © Neil Williams


 | Bookmark
Related Tutorials:
» Port Scanning and Service Status Checking in PHP
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources