PHP Scripts on the Departmental Web server(s)
To process web pages via the PHP engine you must give them a .php extension.
More information about PHP can be found on the PHP web site.
Another very good starting point (allthough in Dutch only) is the web page about the KU Leuven course on dynamic web sites ... certainly well worth visiting when you plan to write you own dynamic pages.
When writing php pages/scripts do not forget to check the web server log files regularly for any errors/warnings generated by them. You can find the log files in /cw/wwwserver/extern/logs for the external web server and in /cw/wwwserver/intern/logs for the internal web server ... check both the web-service and php log files.
phpinfo()
To get started with PHP, it can be very informative to put a call to the phpinfo() function at the beginning or end of your script. This will output the current configuration and values of all PHP variables and arrays. In this way, you can easily spot which variable holds the information you are looking for.Register Globals
Theregister_globals configuration variable is set to off.The cookies, get, post and other PHP variables are thus not automatically available as global variables ... you can find them in the auto-global arrays. (e.g. $_POST['myvariable'] instead of $myvariable)
More information about register_globals can be found in the PHP documentation about it.
Safe Mode
Safe mode used to be a PHP security setting but is deprecated (and/or even removed) from recent PHP versions. Our PHP has safe mode set to off.
Because security is important, we now use suexec - ask helpdesk for more information if needed.
Sending E-Mail
Sending E-Mail from within a php script requires some special attention: the standard php function mail() does not always set the real/envelope From address correctly. Consequently any and all mail errors will be sent to the web server account (who probably doesn't know what to do with them ;-)
Luckily newer php versions (including the one on our web server) offer support to make sure both normal e-mail communication and possible errors are handled as they should using the 4th (additional_headers) and the 5th (additional_parameters) arguments of the php mail function:
$to = ...; $subject = ...; $message = ...; $name = 'Your Name'; $from = 'Your.Address@example.com'; $headers = "From: $name <$from>\r\n" . "Reply-To: $name <$from>\r\n" . "Return-Path: $from"; mail( $to, $subject, $message, $headers, "-f $from" );
Make sure to end all additional header lines with \r\n (so both carriage return and line feed) and feel free to add additional headers to your liking/needs.
If you adapt the above code, please do make sure to always include a meaningfull return address in both the Return-Path header and the 5th argument of the mail function !
Please also make sure that no spam or otherwise unwanted mail can be sent using your php script !
MySQL
From within a PHP script, you can access a MySQL database using themysql_connect et al. commands. You must give the database server, the database name and mysql account and password to connect with.
You should never put passwords, or even user and database names, on web pages or PHP scripts ... you should put them in a separate file that is only accessible by you and the web server and that is not served by the web server as a regular web page.
So you should not put such a file in either of:
- /cw/www/...
- /cw/wwwserver/extern/public_html/...
- /cw/wwwserver/extern/users/login-name/public_html/...
- /cw/wwwserver/extern/groups/group-name/public_html/...
You can put such a file safely in:
- /cw/wwwserver/extern/users/login-name/something-else-than-public_html
- /cw/wwwserver/extern/groups/group-name/something-else-than-public_html
include command.The file itself must only be readable by the web server and yourself or a limited group of people. This can easily be accomplished in 2 ways:
- either give it www-data as group (and yourself as owner)
- or give it www-data as owner and the group in question as group
A third possibility, in case you have several of such files, is to create a directory in the way described above and put the permissions on that directory as desired (thus not accessible to others, only by the web server and yourself or a limited group). In such a directory, you can create files without having to bother with the exact owner, group and permissions on the files themselves (because access is already restricted by the permissions on the directory).
Creating a file or directory with www-data as owner or www-data as group, can only be done by root, so ask pvw to do this for you.
Please make sure you put all sensitive information (passwords, user names, database names, database servers etc.) in a directory the web server does not serve web pages from. Security is everybody's concern !
You can find more information about the use of MySQL databases here.

