Professional Documents
Culture Documents
2
Chapter 9 from text book
1
Outline
Form Handling
Files
Cookies
Session Tracking
Database Accesses using PHP and MySQL
(13.5)
Content management system
2
Submitting data to a web server
Though browsers mostly retrieve data,
sometimes you want to submit data to a
server
◦ Hotmail: Send a message
◦ Flickr: Upload a photo
◦ Google Calendar: Create an appointment
The data is sent in HTTP requests to the
server
◦ with HTML forms
The data is placed into the request as
parameters
3
"Superglobal" Arrays
Array Description
4
Using $_GET
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
• If the user enters his information then clicks the "Submit"
button, the URL sent to the server could look something like
this:
-http://~~~/welcome.php?fname=Deema&age=3
6
Files
PHP can:
◦ Deal with any files on the server
◦ Deal with any files on the Internet, using either http or ftp
PHP associates a variable with a file, called the file variable (for
program reference)
A file has a file pointer (where to read or write)
◦ $fptr = fopen(filename, use_indicator)
Use indicators:
r read only, from the beginning
r+ read and write, from the beginning
w write only, from the beginning (also creates the file, if necessary)
w+ read and write, from the beginning (also creates the file, if necessary)
a write only, at the end, if it exists (creates the file, if necessary)
a+ read and write, read at the beginning, write at the end
Reading files
1) Read all or part of the file into a string variable
$str = fread(file_var, #bytes)
To read the whole file, use filesize(file_name) as the second
parameter
13
Example 1: Writing Cookies
(cookies.html)
<html xmlns = "http://www.w3.org/1999/xhtml">
<head> <title>Writing a cookie to the client computer</title> </head>
<body style = "font-family: arial, sans-serif; background-color: #99CCFF">
<h2>Click Write Cookie to save your cookie data.</h2>
<form method = "post" action = "cookies.php“>
<strong>Name:</strong><br />
<input type = "text" name = "NAME" /><br />
<strong>Height:</strong><br />
<input type = "text" name = "HEIGHT" /><br />
<strong>Favorite Color:</strong><br />
<input type = "text" name = "COLOR" /><br />
<input type = "submit" value = "Write Cookie"
style = "background-color: #F0E86C; color: navy; font-weight: bold"
/></p>
</form> </body></html> 14
Example 1: Writing Cookies
(cookies.php)
<?php
extract( $_POST );
// write each form field’s value to a cookie and set the cookie’s expiration date
setcookie( "Name", $NAME, time() + 60 * 60 * 24 * 5 );
setcookie( "Height", $HEIGHT, time() + 60 * 60 * 24 * 5 );
setcookie( "Color", $COLOR, time() + 60 * 60 * 24 * 5 );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head> <title>Cookie Saved</title> </head>
<body style = "font-family: arial, sans-serif">
<p>The cookie has been set with the following data:</p>
<br /><span style = "color: blue">Name:</span>
<?php print( $NAME ) ?><br />
<span style = "color: blue">Height:</span>
<?php print( $HEIGHT ) ?><br />
<span style = "color: blue">Favorite Color:</span>
<span style = "color: <?php print( "$COLOR\">$COLOR" ) ?> </span><br />
<p>Click <a href = "readCookies.php">here</a> to read the saved cookie.</p>
</body> </html> 15
Example 2: Reading Cookies
(readcookies.php)
<html xmlns = "http://www.w3.org/1999/xhtml">
<head><title>Read Cookies</title></head>
<body style = "font-family: arial, sans-serif">
<p>
<strong> The following data is saved in a cookie on your computer. </strong>
</p>
<table border = "5" cellspacing = "0" cellpadding = "10">
<?php
// iterate through array $_COOKIE and print name and value of each cookie
foreach ( $_COOKIE as $key => $value )
print( "<tr>
<td bgcolor=\"#F0E68C\">$key</td>
<td bgcolor=\"#FFA500\">$value</td>
</tr>" );
?>
</table> </body> </html>
16
Sessions
Some applications need to keep track of a session
Sessions are represented internally in PHP with a session id
◦ A session consists of key/value pairs
A session can be initialized or retrieved by using the
session_start function
◦ This function retrieves $_SESSION, an array containing the
key/value pairs for each session in the current request
Example:
<?php
// this starts the session
session_start();
// this sets variables in the session
$_SESSION['test']='testing';
print "Done";
?>
17
Sessions
Example
Set a session variable using $_SESSION
array.
Checkpoint 2
What is the difference between Sessions
and Cookies?
Can we use Sessions and Cookies in the
same PHP program?
Database Accesses using PHP and
MySQL
To access data stored in a MySQL database:
1. Create a Connection to a MySQL DBMS
$con = mysql_connect(servername,username,password);
25
Extra Topic
CONTENT
MANAGEMENT
SYSTEMS (CMS)
What is CMS?
A Web Content
Management System
(WCMS) is a software system
which provides website
authoring, collaboration and
administration tools designed to
allow users with little
knowledge of web programming
languages or markup languages
to create and manage the site's
content with relative
ease.(Wikipedia, 2010)
What functionalities do CMSs
provide?
WYSIWYG editor for
content
Easily editable content
Multilingual front end
Multi-
Template/Themes.
Seamless Database
connectivity
Scalable feature sets
…. And much more!!
How do I choose a CMS?
Understanding Your Web Content
Determining and documenting "business"
requirements
OS that runs on
Type of Database
Extendibility
User Friendly
Provides templating that is completely
customizable and Supports XHTML and
CSS templates
Example (Joomla)
Example (Drupal)
THE END