
|
Last
Digit of your Student ID % 5
|
Designated PHP-MySQL for Web Programming Exercises |
|
0
|
sanluis
|
|
1
|
blanca
|
|
2
|
crestone
|
|
3
|
shavano
|
|
4
|
wetterhorn
|
|
Type |
Description |
|---|---|
|
Integer |
integer number |
|
Double |
floating point number |
|
bool1 |
Boolean (true or false), available from PHP 4.0 |
|
Array |
hybrid of ordered array and associative array |
|
object2 |
an object with properties and methods (not discussed in this article) |
Indexed from 0
Indexed by strings (Using the same [ ] to specify
the index, as opposed to { } in Perl)
Indexed with non-continuous numbers
Indexed by a mix of numbers and strings
<?php
$array1 = array(2, 3, 5, 7, 11);
$array2 = array("one" => 1, "two" => 2, "three" => 3);
$array3 = array(5 => "five", "six", "seven");
printf("7: %d, 1: %d, 'six': %s\n", $array1[3], $array2["one"], $array3[6]);
?> PHP includes if and elseif conditionals, as well
as while and for loops, all with syntax similar
to C. The example below introduces these four constructs:
<?php
// Conditionals
if ($a) {
print "a is true<BR>\n";
} elseif ($b) {
print "b is true<BR>\n";
} else {
print "neither a or b is true<BR>\n";
}
// Loops
do {
$c = test_something();
} while ($c);
while ($d) {
print "ok<BR>\n";
$d = test_something();
}
for ($i = 0; $i < 10; $i++) {
print "i=$i<BR>\n";
}
?>
$i can appear in side print string. Variable substitution similar to that
of Perl is performed. One of PHP's oldest features is the ability to make HTML form and cookie data available directly to the programmer. By default, any form entry creates a global PHP variable of the same name.
In the following example, a user name is retrieved and assigned to a variable. The name is then printed by the sub-routine "submit.php":
<FORM METHOD="GET" ACTION="submit.php">
What's your name? <INPUT NAME="myname" SIZE=3>
</FORM>
<?php
print "Hello, $myname!";
?>
From the above example, note that variables can also be referenced from within
double quoted strings.
The following checkbox.php file will only print out "sizes=2, 2, o"
if all checkboxes are selected. The reason is the browser will submit a request
with body contains "sizes=12oz&sizes=16oz&sizes=22oz" and
the $sizes will receive "22oz" last data as value, and therefore
print out the first 3 characters of "22oz". It misses the other
data input.
<h1>chow's test php web page on xampp </h1>
<?php
//#extract($_REQUEST); We can use extract to assign all submited values to
the variables
// using the same name attribute in the original form as variable name.
$sizes=$_REQUEST['sizes']; // we can also use $_REQUEST array to retrieve
specific
//
name value pair of the input.
print "sizes=$sizes[0], $sizes[1], $sizes[2]<br>";
?>
<form method=POST action="test.php">
Select Beer Can sizes:
<input type=checkbox name=sizes value="12oz"> 12 oz.<br>
<input type=checkbox name=sizes value="16oz"> 16 oz.<br>
<input type=checkbox name=sizes value="22oz"> 22 oz.<br>
<br><br>
<input type=submit>
</form>
To fix the problem we need to declare the name of checkbox input as an array.
The correct checkbox.php is as follows. The name is changed to sizes[ ] from
sizes.
<h1>chow's test php web page on xampp </h1>
<?php
//#extract($_REQUEST);
$sizes=$_REQUEST['sizes'];
print "sizes=$sizes[0], $sizes[1], $sizes[2]<br>";
?>
<form method=POST action="test.php">
Select Beer Can sizes:
<input type=checkbox name=sizes[] value="12oz"> 12 oz.<br>
<input type=checkbox name=sizes[] value="16oz"> 16 oz.<br>
<input type=checkbox name=sizes[] value="22oz"> 22 oz.<br>
<br><br>
<input type=submit>
</form>
See the comparison in checkboxWrong.php
and checkbox.php.
You can set cookies in the browser from PHP using the setcookie()
function. setcookie() adds headers to the HTTP response. Since
headers must be inserted before the body, you will need to finish all of your
setcookie() calls before any body output (usually HTML) is printed.
The following example uses a cookie to store a form value until your browser is terminated.
<?php
if (!$myname) {
print "What is your name? ";
print "<FORM ACTION=\"$PHP_SELF\" METHOD=\"GET\">\n";
print "<INPUT NAME=\"myname\" SIZE=20>\n";
print "</FORM>";
exit;
}
setcookie("myname", $myname);
?>
PHP has a number of built-in variables that give you access to your Web server's CGI environment, form/cookie data and PHP internals. Here are some of the most useful variables:
The $GLOBALS and $PHP_SELF variables shown in the table below are specific to PHP:
| Variable Name | Description |
|---|---|
| $GLOBALS | An associative array of all global variables. This is the only variable in PHP that is available regardless of scope. You can access it anywhere without declaring it as a global first. |
| $PHP_SELF | This is the current
script, for example /~ssb/phpinfo.php3. |
The variables listed below are derived from CGI protocols.
Note that the $HTTP_*_VARS variables are available only when the "track_vars" directive is enabled. You can enable the directive by default when installing PHP with the "enable-track-vars" switch set to configure. Alternatively, you can set it in php.ini, or from your Web server's configuration.
|
Variable Name |
Description |
|---|---|
|
$DOCUMENT_ROOT |
Your Web server's base directory with user-visible files. |
|
$REQUEST_METHOD |
The HTTP method used to access this page, for example GET or POST. |
|
$REQUEST_URI |
Full local part of the request URL, including parameters. |
|
$HTTP_GET_VARS |
An associative array with the GET parameters passed to PHP, if any. |
|
$HTTP_POST_VARS |
An associative array with the POST parameters passed to PHP, if any. |
|
$HTTP_COOKIE _VARS |
An associative array with the cookies passed by the browser, if any. |
|
$SCRIPT_FILENAME |
File name of the top-level page being executed. |
|
$SCRIPT_NAME |
Local URI part of the page being executed. |
|
$SERVER_ADMIN |
Server administrator's email address. |
|
$SERVER_NAME |
Domain name for the server. |
|
$SERVER_PORT |
TCP port number the server runs on. |
|
$SERVER_PROTOCOL |
Protocol used to access the page, for example "HTTP/1.1". |
HTTP Request Variables are derived from their corresponding HTTP headers. For example, $HTTP_USER_AGENT is derived from the User-Agent header, presented in the table below:
|
$HTTP_HOST |
Host name in the browser's "location" field. |
|
$HTTP_USER_AGENT |
User agent (browser) being used. |
|
$HTTP_REFERER |
URL of the referring page. |
echo '<pre>';
if (!$name) {
print "Name not provided<br>Try again";
} elseif (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{
// the file is first upload to /tmp directory directory
and then move to our specified directory
echo "File is valid, and was successfully uploaded.\n";
echo 'Here is the link to the saved file:';
print "<a href=\"$uploadfile\">
$uploadfile</a>";
} else {
echo "Possible file upload attack!\n";
}
print "</pre>";
?>