Programming in PHP
PHP is an excellent script language that provides numerous tools to web programmers. Code can be embedded in an HTML document to provide dynamic content.
In order for the PHP code to be processed, the HTML server must have PHP installed, the HTML/PHP file must end with the extension .php instead of the usual .htm or .html, AND the embedded code must be delimited by the paired delimiters <?PHP and ?>. As many code segments as necessary may be interlaced with static HTML code in the HTML body.
Any dynamic HTML code generated by PHP will be alternated with the static HTML code. An excellent application would be to generate user requested content extracted from databases.
1. A very simple example
This example will check the day of the month, and print the statement:
"It is an even day", or "It is an odd day" accordingly.
<HTML><HEAD></HEAD><BODY>
<?PHP
$today=getdate();
if($today["mday"]%2==0)echo "It is an even day.";
else echo "It is an odd day.";
?>
</BODY></HTML>
2. Hit-Log Application using PHP and MySql
Instead of simply a hit-counter, it would be nice to have a record of the time of the day your site has been visited, and by whom. This example will do just that.
It is a three part procedure that consists of
1. Make and execute a file containing the procedure to create a hit-log database at a location designated by your service provider,
2. Insert PHP code at the end of your HTML pages, but rename them to .php instead of the usual .htm or .html extension.
3. Make a procedure file that displays the report of the hit-log database.
Here are the details.
1. Make a procedure file in your home directory to create a table in a database
You will have to find out from your service provider the following information:
- the name of the MySql database server, or computer (name_of_server)
- the name of the account (account_name) and password (password)
- and the name of the database (database_name)
Insert following code in a file called MakeDB.php in your site's home directory to create a new table called "log", or any other name of your choice. If you are using someone else's account, make sure that he/she does not already have a table called "log". Execute the procedure once. In fact, you could execute it as many times as you wish, it will wipe out all the data already stored in the table, if there was any.
<?PHP
function ConnectDB(){ // connects to database
$conn = mysql_connect('name_of_server', 'account_name', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('database_name') or die('Could not select database');
return $conn;
}
function MakeDB(){
$conn=ConnectDB();
$query="drop table log;"; $results=mysql_query($query);
$query="create table log (l_index int auto_increment primary key,l_date date, l_time char(8), l_remoteIP varchar(15));";
$results=mysql_query($query);
$conn->close;
}
$conn=ConnectDB();
MakeDB();
$conn->close;
?>
2. Insert the following code at the end of your HTML files where you want the hits recorded, not forgetting to rename your file's extension htm or .html to .php:
<?PHP
function ConnectDB(){ // connects to database
$conn = mysql_connect('name_of_server', 'account_name', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('database_name') or die('Could not select database');
return $conn;
}
function WriteLog(){
$remoteIP=$_ENV["REMOTE_ADDR"];
$query="INSERT INTO log SET l_date=CURDATE(), l_time=CURTIME(), l_remoteIP=\"$remoteIP\" ";
$results=mysql_query($query);
}
$conn=ConnectDB();
WriteLog();
$conn->close;
?>
3. Getting a report of the hits.
It would be nice to view your hit-log, execute the following code that you put in a file called Report.php:
<HTML><HEAD></HEAD><BODY>
<CENTER><H1>REPORT OF HITS</H1></CENTER>
<?PHP
function ConnectDB(){ // connects to database
$conn = mysql_connect('name_of_server', 'account_name', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('database_name') or die('Could not select database');
return $conn;
}
function ShowBrief(){
echo "<TABLE border=1><THEAD><TR><TH>ID</TH><TH>DATE</TH><TH>TIME</TH><TH>IP</THEAD><TBODY>";
$query='select * from log';
$results=mysql_query($query);
while ($row = mysql_fetch_array($results)) {
echo "<TR><TD>$row[0]</TD> <TD>$row[1]</TD><TD>$row[2]</TD><TD>$row[3]</TD></TR>";
}
echo "</TBODY>";
}
$conn=ConnectDB();
ShowBrief();
$conn->close;
?>
</BODY></HTML>
Example Output
REPORT OF HITS
| ID | DATE | TIME | IP |
| 60 | 2005-04-17 | 18:19:22 | 124.33.141.72 |
| 61 | 2005-04-17 | 18:23:29 | 74.82.136.12 |
| 62 | 2005-04-17 | 18:36:30 | 53.66.136.64 |
| 63 | 2005-04-17 | 18:53:40 | 134.57.35.53 |
| 64 | 2005-04-17 | 18:59:11 | 134.132.58.39 |
| 65 | 2005-04-17 | 19:09:12 | 134.22.51.57 |
| 66 | 2005-04-17 | 19:13:35 | 204.51.33.82 |
| 67 | 2005-04-17 | 19:24:17 | 103.46.25.67 |
| 68 | 2005-04-17 | 19:27:53 | 44.122.63.75 |
| 69 | 2005-04-17 | 19:33:22 | 62.27.62.98 |
| 70 | 2005-04-17 | 20:36:54 | 107.34.142.57 |
| 71 | 2005-04-17 | 20:47:32 | 142.66.24.32 |
| 72 | 2005-04-17 | 20:54:15 | 25.82.13.192 |
| 73 | 2005-04-17 | 20:56:31 | 31.36.133.51 |
| 74 | 2005-04-17 | 20:58:56 | 74.80.166.35 |
Here it is, a simple but useful code example using PHP and MySql to record your hits.
updated 2005-04-17