Laboratory 2 Introduction to PHP Programming
Objective : Make use of PHP4 for building web site with dynamic content
/etc/init.d
./httpd status
./httpd start
1 The first PHP Program
"first.php"
<html>
<head><title>First PHP</title></head>
<body>
<?php
// Shell script style
echo "Shell script style printing: Hello World<p>\n";
?>
<H2> <?php
/* String operator */
print "More ". "Hello ". 'World';
?> </H2>
C style printing: an integer
<strong> <?php
printf("%d", 123456);
?> </strong>
a floating point number
<i> <?php
printf("%03.20f!!", M_PI);
?> </i>
<br>
</body>
</html>
2 Redirect client browser to one of the 3 URLs Randomly
"redir2.php"
<?php // only 3 URLs
$myurl=array();
$myurl[0]="http://www.polyu.edu.hk";
$myurl[1]="http://www.eie.polyu.edu.hk";
$myurl[2]="http://www.yahoo.com";
function ur() {
return rand(0,2); // return random integer 0-2.
};
// To redirect the user to the 3 web sites randomly
header("Location: ". $myurl[ur()]);
// Step 3
exit;
?>
Question:
The function rand(min,max) gives a random number between min and max. Try to execute redir2.php 20 times. Give the number of times that each web site is redirected to.
ANS:
$myurl[0]="http://www.polyu.edu.hk"-----------------------5
$myurl[1]="http://www.eie.polyu.edu.hk"------------------10
$myurl[2]="http://www.yahoo.com"-------------------------5
You may notice that indeed the function rand() is not that random. Another way to give a more random output is to refer to the system time.
"redir2.php"
<?php // only 3 URLs
$myurl=array();
$myurl[0]="http://www.polyu.edu.hk";
$myurl[1]="http://www.eie.polyu.edu.hk";
$myurl[2]="http://www.yahoo.com";
function ur() {
return time() % 3 ; // % is the modulo operator.
};
// To redirect the user to the 3 web sites randomly
header("Location: ". $myurl[ur()]);
// Step 3
exit;
?>
ANS:
$myurl[0]="http://www.polyu.edu.hk"-----------------------10
$myurl[1]="http://www.eie.polyu.edu.hk"------------------2
$myurl[2]="http://www.yahoo.com"-------------------------7
3 Basic Form data processing and file I/O
"write_time.php"
<?php
$datafile = "/tmp/datafile";
$data = time();
$fp = fopen ($datafile, "a");
if ($fp == false)
die ("Can't open data file '$datafile'.\n");
fwrite ($fp, $data . "\n");
fclose ($fp);
?>
<html>
<head><title>Write something to file</title></head>
<body>
Write <?php print $data; ?> into <?php print $datafile; ?>.
</body>
</html>
Question:
Implement the PHP script "write_time.php" as shown above. What is the content of the file? Does it look like the usual form we used to represent time? if not, why?
Content: write 1079153780 into /tmp/datafile.
No,
Modify the script so that it writes the data in the form: "Month day, year, hh:mm: am/pm, eg "January 24, 2003, 8:14 pm"
"write_time.php"
<?php
$datafile = "/tmp/datafile";
$data = date("Fj,Y,g:i a");
$fp = fopen ($datafile, "a");
if ($fp == false)
die ("Can't open data file '$datafile'.\n");
fwrite ($fp, $data . "\n");
fclose ($fp);
?>
<html>
<head><title>Write something to file</title></head>
<body>
Write <?php print $data; ?> into <?php print $datafile; ?>.
</body>
</html>
¡@
4 Apache Web server
"fvs.html"
<html><body>
<form action="fvs.php" method="post">
<input type="text" name="name1" size=30 /><br>
<input type="submit" name="ms" value="Submit" />
<p>Multiple select:
<select multiple name="multiselect[]" value=1>
<option value="opt1">Opt1</option>
<option value="opt2">Opt2</option>
<option value="opt3">Opt3</option>
</select></p>
</form>
</body></html>
"fvs.php"
<?php
print $_POST['name1']. "<br>";
print "Number of item: " . count($_POST['multiselect']) . "<br>";
foreach ( $_POST['multiselect'] as $s) {
print $s . "<br>";
};?>
Question
Put fvs.html and fvs.php to the DocumentRoot of an Apache Web server. Use the client PC to retrieve the Web page fvs.html. Grab the screen outputs before and after the submit buttom is pressed and save them to floppy disk.