PHP - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

PHP

Description:

PHP Cookie Variables ... Restrict cookie access to current domain e.g. .massey.ac.nz. Secure ... Built in PHP variables. Processing Forms. Cookies. Sessions ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 19
Provided by: chme3
Category:
Tags: php | cookie

less

Transcript and Presenter's Notes

Title: PHP


1
PHP
  • Language Structure and Programming Techniques

2
PHP
  • Serverside programming language
  • Designed for quick development of HTML based
    dynamic web pages
  • Server side scripts embedded in HTML page
  • lt?php . ?gt
  • Elements of C, Java and Perl

3
PHP HelloWorld
  • Use echo to output information
  • echo Hello World
  • Either single or double quotes
  • lthtmlgtltheadgt
  • lttitlegtPHP Hello Worldlt/titlegt
  • lt/headgt
  • ltbodygt
  • lt?php echo 'lth2gtHello Worldlt/h2gt' ?gt
  • lt/bodygt
  • lt/htmlgt

4
PHP Variables
  • Variables in PHP are dynamically typed
  • Variable names
  • begin with a sign.
  • next character not a number
  • E.g.
  • value 1
  • value2 hello
  • Reference variable assigned using
  • value3 value2

5
PHP Operators
  • ,,,-,/,,,,!,,--,,/,,gt,lt,gt,
    lt,,,,
  • All similar to C
  • Additionally
  • , !
  • Identical, as well as having equal value they are
    the same type
  • _at_
  • Error suppression command
  • Back tick operator to execute shell comands,
    similar to shel_exec(). Watch out for variations
    on different platforms, windows vs unix
  • .
  • String concatenation operator
  • Fullname firstname. .lastname

6
PHP Arrays
  • Arrays in PHP are associative arrays
  • E.g.
  • details array ( namegt Chris, deptgt
    IIMS, citygt Auckland)
  • Access array elements using keyword
  • echo detailsname
  • Associate index can be numbers or strings
  • E.g. Auto indexing
  • myArray array(1, 2, 3, 4, 5, 10gt1, 5gt2, 20,
    3 gt 25)
  • print_r(myArray) gives
  • Array ( 0 gt 1 1 gt 2 2 gt 3 3 gt 25 4
    gt  5 10 gt 1 5 gt 2 11 gt 20 )

7
PHP Functions
  • Functions declared using function keyword
  • function myHello() echo hello
  • Functions can take parameters (passed as values)
  • function myHello2(x) echo hello x
  • Functions can return values
  • function myAdd(x,y) return xy
  • Functions can take parameters by reference
  • function myRef(x) xHello

8
More PHP Functions
  • Functions can return references
  • function myReturnRef()
  • global hello return hello
  • Functions can have default parameter values
  • function myHello3(xChris)
  • echo hello x

9
PHP Classes
  • Object Oriented Programming in PHP
  • Declare classes using class keyword
  • class details
  • var name var city
  • function details(n,c)
  • namencityc
  • Instansiate classes using new keyword
  • myDetails new details(Chris, Auckland)

10
PHP Structured Programming Constructs
  • if, else, switch case , break, continue
  • Similar to C
  • elseif
  • Different syntax to C, same semantics
  • while .., do .. while, for ..
  • All similar to C
  • foreach ( array as val) .
  • foreach ( array as keygtval) .
  • Iterates through named array
  • Note acts on a copy of the array

11
Additional PHP Constructs
  • require(.)
  • Includes file specified, terminates on errors
  • include()
  • Includes file specified, gives warning on errors
  • require_once(.)
  • Includes file specified only if it has not
    already been included, terminates on errors
  • include_once(.)
  • Includes file specified only if it has not
    already been included, gives warning on errors

12
PHP Built in Variables
  • GLOBALS
  • _SERVER
  • _GET
  • _POST
  • _COOKIE
  • _FILES
  • _ENV
  • _REQUEST
  • _SESSION
  • NOTE old versions specify HTTP_XXXXX_VARS

13
PHP Processing Form Variables
  • Extract submitted form variables from
  • _GET
  • _POST
  • Depending on the submission method
  • _REQUEST also contains variables but may
    violate security by using wrong method compared
    to the application design
  • Submitted files can be extracted from
  • _FILES

14
PHP Cookie Variables
  • setcookie(string name, string value, int expire,
    string path, string domain, int secure)
  • Should be the first line of a HTML file (inside
    lt?php. ?gt
  • Needs to be sent as part of HTTP header so cant
    be after any text (even blank spaces)
  • Expiry time
  • time()3600
  • In one hour etc
  • Path
  • Default is /, add more to be specific to
    application /myApp/
  • Domain
  • Restrict cookie access to current domain e.g.
    .massey.ac.nz
  • Secure
  • Whether the cookie is sent over a secure
    connection

15
PHP Cookies cont.
  • if(!isset(COOKIE_SET))
  • setcookie("test", newCookie")
  • header("Location _SERVER'SCRIPT_NAME
    '?COOKIE_SET1")
  • exit
  • Need to make sure cookie is set before reading a
    cookie. If the cookie has only just been set, the
    page must be reloaded before the cookie can be
    read
  • setcookie("test)
  • Delete a cookie by calling set cookie with just
    the name
  • Read the cookie values from
  • _COOKIE variable

16
PHP Sessions
  • Sessions in PHP can be used even if cookies are
    disabled
  • session_start()
  • Called before starting to use any session
    functions, note use before any content is sent
  • header("Cache-control private")
  • Used to enable correct performance when returning
    to submission forms
  • _SESSION'name' name
  • Specify a value for a session variable

17
PHP Sessions cont
  • unset(_SESSION'name')
  • Remove a session variable
  • session_destroy()
  • Terminate a session, not normally done, leave to
    timeout.

18
Summary
Main points to remember PHP Language
Constructs Built in PHP variables Processing
Forms Cookies Sessions
Write a Comment
User Comments (0)
About PowerShow.com