Home > Support > HOWTO List > Web > PHP

Web howtos

Using PHP: PHP Hello World, Common Problems and Solutions

PHP is a popular language used to create dynamic web pages.  Using PHP can be as simple as putting the following code in a /var/www/html/helloworld.php file.  The presumes your Apache DocumentRoot is /var/www/html.  The code:

	<title>PHP Test</title>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>

	<h1>PHP Test</h1>
		<p><b>An Example of PHP in Action</b></p>
		<?php echo "The Current Date and Time is: <br />"; 
		echo date("g:i A l, F j Y.");?> </p>
	<h2>PHP Information</h2> 
		<p> <?php phpinfo(); ?> </p> 

If you then browse to http://yourserver/hellowworld.php it will output information about your Apache and PHP setup.


The following code snippet demonstrates how to get a MySQL connection:

    $link = mysql_connect("localhost", "mysql_user", "mysql_password")
        or die("Could not connect: " . mysql_error());
    print ("Connected successfully");

Resolving: mysql_connect Problem

Are you getting an error like: Problem: mysql_connect(): Client does not support authentication protocol requested by server?

This can happen, for example, when connecting to a MySQL 4.1 database server.  Try the 'old' password format:

update user set password=old_password('yourpasswordhere') where User='yourusernamehere';
flush privileges;

Resolving: File For Download

If instead of executing the PHP your web browser offers you the file for download, check you haven't got a 'IncludesNoExec' directive in play in your httpd.conf file.  E.g.

<Directory /home/*/public_html>
AllowOverride FileInfo AuthConfig Limit
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec

(If you do, just remove the IncludesNoExec option, or override it on the virtual host)

Resolving: PHP Doesn't Execute

Is your test PHP script not working?  E.g. if you view the source of the page do you see the <?php ... > tags?

Make sure your <Directory> block has an Options line that includes ExecCGI, eg...

Options Indexes FollowSymLinks ExecCGI

Make sure that your Apache configuration has correctly enabled php for your setup, eg for FPM enable the proxy_fcgi module, for mod_php make sure the matching libapache package is installed for the php version you are using.

If PHP code works for your 'main' server but not for a virtual host on the same server, check the relevant Directory and VirtualHost directives.  And make sure they also have the ExecCGI directive.

Resolving: URL/Form Parameters Not Present

Are your PHP scripts failing to see URL or Form parameters when you refer to the parameters like $VariableName?

/etc/php.ini will be set with register_globals = Off ($VariableName is how you'd reference a global variable).  You could turn on globals, but the prefered way of referencing the variables is by using syntax such as $HTTP_POST_VARS['VariableName'] - for form POSTs - or $HTTP_GET_VARS['VariableName'] - for URL parameters.

For more information see the w3schools page.

Dont forget to carefully sanitise user inputs, or use a good framework that does the heavy lifting for you.

Resolving: "the document contains no data" on file upload

This error can occur when a user submits a form with more data than PHP is configured to allow. For example, when uploading a file.

To resolve the problem change the LimitRequestBody (the default is about 500KB) in the main apache configuration and restart apache.