home

articles

broadband

browsers

fraud001

javascript001

javascript002

javascript003

links

mysql01

mysql02

mysql03

mysql04

php001

php002

php003

problem001

spyware

virus01

virus02

web_programming

Sponsor001

 

Page created 11 April 2005, updated 13 June 2008.

PHP Programming

This article is about PHP programming, but in reality, it is my own personal notebook. So I will be adding more content, as I learn more about the language.

ERRORS

I recently uploaded a PHP file, which worked on mp3science.com and another web site, on my local host. To my horror, it displayed some errors, when it was uploaded to the web. Eventually, I checked the PHP information on all three servers. The problem was simple, the 'display_errors' was set to 'Off' on the local host. So the errors weren't displayed on the local host, but the web host was set up to display them.

PHP INI

I decided that it would be better to have display_errors set to 'On', on my local host. So I edited the php.ini file. The easy way to find the file is to click on 'Start', then 'Find' and enter 'php.ini' in the search field. Once located, double click on the file, and it should load into 'Note Pad'. Find the line which reads 'display_errors = Off' on its own and without a leading ' ; ' or anything else on that line (you will see it in the information section). Now change it to read 'display_errors = on'.

RESTART THE COMPUTER

Another puzzle, was to find that I had to restart the computer, before the change took place!

PHP INFO

To find all the information about PHP on the server, type the following line into a text editor:

<?php phpinfo(); ?>

Save the file as ' phpinfo.php ' on the local host, or upload it onto a web server. Enter the address into the browser URL field, for example, http://localhost/phpinfo.php and it will display the PHP information.

INCLUDE

The Include statement allows you to include other files into the web page. For example, at the top of this page, there is a navigation bar, and a list of Sponsors. Rather than keep rewriting the code for each page, I use two files, navigation.htm and sponsor.htm, then I call them from each web page, using the following code:

<?php

include( "navigation.htm" );

chdir("../");

include( "sponsor.htm" );

?>

CHANGE DIRECTORY

Notice the line in the above code, chdir("../"); This line tells the PHP parser to look in the directory, which is one level up.

WORKING WITH FILES

The code below opens the file navigation.htm, reads the content ("r"), and prints the contents onto the screen.

<?php

$filename = "navigation.htm";

$file = fopen( $filename, "r" );

$filesize = filesize( $filename );

$text = fread( $file, $filesize );

fclose( $file );

echo( "$text" );

?>

This is how I used to load the navigation bar, before I learned about the 'include' statement. It is of course, much easier to type include( "navigation.htm" ); but at least it has proved to be an example of working with files.

LAST MODIFIED

The code below, automatically prints the last modified date, and has been inserted at the top of this page, where it says, "Updated". Now, whenever I modify this page, the date will change accordingly.

<?php echo( date( "j F Y", filemtime( "computer/php001.php" ) ) ); ?>

The part "computer/php001.php" refers to this page, which is located in the 'computer' directory. So that part will have to be edited for each web page.

IMPROVEMENT

Programming is never finished! There is always an improvement to be made! The following script automatically finds the page URL, so it remains the same for any page.

<?php echo( date( "j F Y", filemtime( substr($_SERVER['PHP_SELF'] ,1 ) ) ) ); ?>

The part $_SERVER['PHP_SELF'] returns /computer/php001.php so it was necessary to remove the / by using the substr method.

CORRECT DIRECTORY

The problem with using other peoples code, is that when it goes wrong, it's very hard to find the problem. I used my own code (above) on another web site, and as you have guessed, it didn't work. The problem was that I had changed the directory in the code before it. So the solution is to find the correct directory:

chdir(../);

Will take you to the directory above, and:

chdir(../../);

Will take you 2 directories above. So it is important to find the correct directory, in order for the code to work.

ALTERNATIVE METHOD

The following code will give you the date modified, with the directory one level up:

echo( date( "j F Y", filemtime( ".." . $_SERVER['PHP_SELF'] ) ) );

This code will change to 2 directories above:

echo( date( "j F Y", filemtime( "../.." . $_SERVER['PHP_SELF'] ) ) );

It is obvious that you need to understand how the program works. Simply copying an existing code can be just as difficult as creating your own!