Professional Documents
Culture Documents
PHP Full
PHP Full
Chapter 26 - PHP
Outline
26.1 Introduction
26.2 PHP
26.3 String Processing and Regular Expressions
26.4 Viewing Client/Server Environment Variables
26.5 Form Processing and Business Logic
26.6 Verifying a Username and Password
26.7 Connecting to a Database
26.8 Cookies
26.9 Dynamic Content in PHP
26.10 Operator Precedence
26.11 Web Resources
Objectives
26.1 Introduction
• PHP
– PHP: Hypertext Preprocessor
– Originally called “Personal Home Page Tools”
– Popular server-side scripting technology
– Open-source
• Anyone may view, modify and redistribute source code
• Supported freely by community
– Platform independent
26.2 PHP
• Basic application
– Scripting delimiters
• <? php ?>
• Must enclose all script code
– Variables preceded by $ symbol
• Case-sensitive
– End statements with semicolon
– Comments
• // for single line
• /* */ for multiline
– Filenames end with .php by convention
26.2 PHP
Fig. 26.1 Simple PHP program.
26.2 PHP
• Variables
– Can have different types at different times
– Variable names inside strings replaced by their value
– Type conversions
• settype function
• Type casting
– Concatenation operator
• . (period)
• Combine strings
26.2 PHP
26.2 PHP
Fig. 26.3 Type conversion.
26.2 PHP
• Arithmetic operators
– Assignment operators
• Syntactical shortcuts
• Before being assigned values, variables have value undef
• Constants
– Named values
– define function
26.2 PHP
Fig. 26.4 Using PHP’s arithmetic operators.
26.2 PHP
• Keywords
– Reserved for language features
– if…elseif…else
• Arrays
– Group of related data
• Elements
– Name plus braces and index
• Indices start at zero
– count function
– array function
26.2 PHP
• Arrays, cont.
– Built-in iterators
• Maintain pointer to element currently referenced
• reset
• key
• next
• foreach loops
26.2 PHP
PHP keywords
and do for include require true
break else foreach list return var
case elseif function new static virtual
class extends global not switch xor
continue false if or this while
default
Fig. 26.5 PHP keywords.
26.2 PHP
Fig. 26.6 Array manipulation.
Quantifier Matches
{n} Exactly n times.
{m,n} Between m and n times inclusive.
{n,} n or more times.
+ One or more times (same as {1,}).
* Zero or more times (same as {0,}).
? Zero or one time (same as {0,1}).
Fig. 26.9 Some PHP quantifiers.
• Form processing
– action property
• Where to send form data
– method property
• post
– Each element has unique name
• Business logic
– Confirm that valid information was entered
– extract function
• Creates variables corresponding to each key-value pair in array
• Easily retrieve all values sent to PHP page
– Regular expressions very helpful
– Do checks on client side where possible
• JavaScript
• Conserves server resources
• Ending a script
– die function
• Remember to close all HTML tags
• Private website
– Only accessible to certain individuals
– Encrypt username and password data when sending, storing
and retrieving for increased security
• Implementing password checking
– Login information stored in file
• fopen function
• Read, write, append modes
– Store data using fputs
• \n newline character
– Close files when done
• fclose function
• Databases
– Store and maintain data
– MySQL is a free database product
– PHP supports many database operations
• Access databases from Web pages
26.8 Cookies
• Cookies
– Store information on client computer
– Track preferences and other information
– Stored as text files on hard drive
– Never store sensitive information, such as credit card
numbers, in a cookie
• Security risk
• Cookies and PHP
– setcookie function
• Name
• Value
• Expiration date
26.8 Cookies
Fig. 26.20 Gathering data to be written as a cookie.
26.8 Cookies
Fig. 26.21 Writing a cookie to the client.
26.8 Cookies
• Reading cookies
– $_COOKIE environment variable
• Array
– foreach loop to access each element
• Split into key and value
26.8 Cookies
• Cookie storage
– Internet Explorer
• Stores cookies in Cookies directory
• Text file
26.8 Cookies
Fig. 26.22 Cookies directory before a cookie is written.
26.8 Cookies
Fig. 26.24 Displaying the cookie’s content.