original - Kansas State University

advertisement
Lecture 18 of 42
Lab 3: PHP Tutorial
Discussion: Server-Side Programming
Friday, 10 October 2008
William H. Hsu
Department of Computing and Information Sciences, KSU
KSOL course page: http://snipurl.com/v9v3
Course web site: http://www.kddresearch.org/Courses/Fall-2008/CIS560
Instructor home page: http://www.cis.ksu.edu/~bhsu
Reading for Next Class:
PHP Tutorial, K. Brown (http://www.keithjbrown.co.uk/vworks/php/)
W3Schools PHP Tutorial (http://www.w3schools.com/php/)
Database Journal PHP Tutorial (http://snurl.com/48typ)
Computing & Information Sciences
Kansas State University
Introduction
 Based on PHP and MySQL Web Development, Third
Edition (Available as CS eBook from Dal Library)
 Other eBooks from Dal Library
 Learning PHP 5
 PHP Cookbook
 For Online resources, google “PHP”
© 2006 J. Paek, Dalhousie University
CS 4173 Web-centric Computing, http://users.cs.dal.ca/~jamie/course/CS/4173/
Computing & Information Sciences
Kansas State University
Table of Contents






Embedding PHP
Variables
Operators and Control Structures
Array
Function
Session Control (Using Cookie)
© 2006 J. Paek, Dalhousie University
CS 4173 Web-centric Computing, http://users.cs.dal.ca/~jamie/course/CS/4173/
Computing & Information Sciences
Kansas State University
Embedding PHP in HTML
 Insert PHP tag inside HTML file (with .php extension
 XML Style
<?php PHP statement; ?>
 Short Style (Need to be enabled)
<? PHP statement; ?>
 Script Style
<SCRIPT LANGUAGE='php'> PHP statement;
</SCRIPT>
 ASP Style (Need to be enabled)
<% PHP statement; %>
 Dynamic Content
function('argument');
 Note: argument is in string
© 2006 J. Paek, Dalhousie University
CS 4173 Web-centric Computing, http://users.cs.dal.ca/~jamie/course/CS/4173/
Computing & Information Sciences
Kansas State University
Variables
 Do not require to declare variable type
 Variable variables
$varname = 'tireqty';
$$varname = 5;
 Constants
define('TIREPRICE', 100);
 Accessing form variables (field=tireqty)
 Short style (requires register_globals)
$tieryqty
 Medium style
$_POST['tireqty'] or $_GET['tireqty']
 Long style
$HTTP_POST_VARS['tireqty']
© 2006 J. Paek, Dalhousie University
CS 4173 Web-centric Computing, http://users.cs.dal.ca/~jamie/course/CS/4173/
Computing & Information Sciences
Kansas State University
Operators and Control Structures
 Pretty much same as in other programming languages
(C, Java, etc.)
 Break statements are also same (continue, break),
except it provides exit statement to break out of the script
 Alternative control structure syntex
if( $totalqty == 0):
echo 'You did not order anything on the previous page!<br
/>';
exit;
endif;
© 2006 J. Paek, Dalhousie University
CS 4173 Web-centric Computing, http://users.cs.dal.ca/~jamie/course/CS/4173/
Computing & Information Sciences
Kansas State University
Array
 Create an array
$products = array ('Tires', 'Oil', 'Engine');
 Automatically generate sequnces of number, character
$numbers = range (1,10,2); //last parameter optional(Indicate
step)
 Accessing element
$products[0]
 Array with different indices
$prices = array( 'Tires'=>100, 'Oil'=>10, 'Spark Plugs'=>4 );
 Assign key and value to variables
list( $product, $price ) = each( $prices );
© 2006 J. Paek, Dalhousie University
CS 4173 Web-centric Computing, http://users.cs.dal.ca/~jamie/course/CS/4173/
Computing & Information Sciences
Kansas State University
Array (Cont'd)
 Multidimensional Array ($products[row][column]
$products = array( array( 'Code' => 'TIR',
'Description' => 'Tires',
'Price' => 100
),
array( 'Code' => 'OIL',
'Description' => 'Oil',
'Price' => 10
),
array( 'Code' => 'SPK',
'Description' => 'Spark Plugs',
'Price' =>4
)
);
© 2006 J. Paek, Dalhousie University
CS 4173 Web-centric Computing, http://users.cs.dal.ca/~jamie/course/CS/4173/
Computing & Information Sciences
Kansas State University
Function
 New function
function my_function()
{
echo 'My function was called';
}
 Calling function
my_function();
© 2006 J. Paek, Dalhousie University
CS 4173 Web-centric Computing, http://users.cs.dal.ca/~jamie/course/CS/4173/
Computing & Information Sciences
Kansas State University
Function (Cont'd)
 Using argument
 Should reset the argument if it is an array
 Next command gets next element of arg
 Current command gets current element
 Example
function create_table2( $data, $border = 1, $cellpadding = 4,
$cellspacing = 4 )
{
echo "<table border = $border cellpadding = $cellpadding"
." cellspacing = $cellspacing>";
reset($data);
$value = current($data);
while ($value)
{
echo "<tr><td>$value</td></tr>\n";
$value = next($data);
}
echo '</table>';
}
© 2006 J. Paek, Dalhousie University
CS 4173 Web-centric Computing, http://users.cs.dal.ca/~jamie/course/CS/4173/
Computing & Information Sciences
Kansas State University
Session Control (Using Cookie)
 Manually setting Cookie in PHP
bool setcookie (string name [, string value [, int expire [, string
path
[, string domain [, int secure]]]]])
Ex. setcookie ('mycookie', 'value');
 Using Cookie with Sessions
 Get session cookie parameters
session_get_cookie_params()
 Set session cookie parameters
session_set_cookie_params($lifetime, $path, $domain [,
$secure]);
© 2006 J. Paek, Dalhousie University
CS 4173 Web-centric Computing, http://users.cs.dal.ca/~jamie/course/CS/4173/
Computing & Information Sciences
Kansas State University
Session Control (Cont'd)
 Starting Session (Must be declared at the beginning of
the file)
session_start();
 Registering Session variables
$_SESSION['myvar'] = 5;
 Unsetting variables
 Single variable
unset($_SESSION['myvar']);
 All variables
$_SESSION=array();
 Destroying session
session_destroy();
© 2006 J. Paek, Dalhousie University
CS 4173 Web-centric Computing, http://users.cs.dal.ca/~jamie/course/CS/4173/
Computing & Information Sciences
Kansas State University
Session Control Example [1]
 Begin session
<?php
session_start();
$_SESSION['sess_var'] = "Hello world!";
echo 'The content of $_SESSION[\'sess_var\'] is '
.$_SESSION['sess_var'].'<br />';
?>
<a href="page2.php">Next page</a>
© 2006 J. Paek, Dalhousie University
CS 4173 Web-centric Computing, http://users.cs.dal.ca/~jamie/course/CS/4173/
Computing & Information Sciences
Kansas State University
Session Control Example [2]
 Get the variable and unset it
<?php
session_start();
echo 'The content of $_SESSION[\'sess_var\'] is '
.$_SESSION['sess_var'].'<br />';
unset($_SESSION['sess_var']);
?>
<a href="page3.php">Next page</a>
© 2006 J. Paek, Dalhousie University
CS 4173 Web-centric Computing, http://users.cs.dal.ca/~jamie/course/CS/4173/
Computing & Information Sciences
Kansas State University
Session Control Example [3]
 End session
<?php
session_start();
echo 'The content of $_SESSION[\'sess_var\'] is '
.$_SESSION['sess_var'].'<br />';
session_destroy();
?>
© 2006 J. Paek, Dalhousie University
CS 4173 Web-centric Computing, http://users.cs.dal.ca/~jamie/course/CS/4173/
Computing & Information Sciences
Kansas State University
Download