Title: LIS651 lecture 1 PHP basics
1LIS651 lecture 3taming PHP with custom
functions, sessions and includes
Thomas Krichel 2009-04-05
2functions
- The PHP function reference is available on its
web site http//php.net/quickref.php. It shows
the impressive array of functions within PHP. - But one of the strengths of PHP is that you can
create your own functions as you please. - If you recreate one of the built-in functions,
your own function will have no effect.
3simplest function
function beer_print print
"beer\n" beer_print() // prints beer and
newline
4A more practical example
- Stephanie Rubino was an English teacher and
objects to sentences like - You have ordered 1 bottles of Grosswald Pils.
- Let us define a function rubino_print(). It will
take three arguments - a number to check for plural or singular
- a word for the singular
- a word for the plural
5a function and its arguments
- declare the arguments to the function in
parenthesis - function rubino_print (number,
singular,plural) - if(number 1)
- print "one singular"
-
- else
- print "number plural"
-
-
- rubino_print(3,'woman','women') // prints 3
women
6default arguments
- Sometimes you want to allow a function to be
called without giving all its arguments. You can
do this by declaring a default value, using an
equal sign in the function list - function thomas_need(thing'beer')
- print "Thomas needs thing.\n"
-
- thomas_need() // prints Thomas needs beer.
- thomas_need('vodka') // prints Thomas needs
vodka.
7rubino_print using common plurals
function rubino_print (num, sing,plur1)
if(num 1) print "one sing"
elseif(plur 1) print "num
sing"."s" else print "num
plur" rubino_print(6,'beer') //
prints 6 beers
8return value
- Up until now we have just looked at the side
effect of a function. - "return" is a special command that returns a
value. - It takes the return value as a parameter
- return result
- When return is used, the function is left.
Example - function good_beer ()
- return 'Festbock'
-
- beergood_beer()
- print beer // prints Festbock.
9rubino_print with return
function rubino_print (number,
singular,plural) if(number 1)
return "one singular" return "number
plural" orderrubino_print(2,"beer","beers")
print "you ordered order\n" // prints you
ordered 2 beers.
10visibility of variables
- Variables used inside a function are not visible
from the outside. Example - beer"Karlsberg"
- function yankeefy (name'Sam Adams')
- beername
-
- yankeefy()
- print beer // prints Karlsberg
- The variable inside the function is something
different than the variables outside.
11accessing global variables.
- There are two ways to change a global variable,
i.e. one that is defined in the main script. - One is just to call it as GLOBAL'name' where
name is the name of the global variable. - function yankeefy (name"Sam Adams")
- GLOBALS'beer'name
-
- The other is to change it outside a function
definition. - Example in brewer_quiz.php
12working with many source files
- Many times it is useful to split a PHP script
into several files. - PHP has two mechanisms.
- require(file) requires the file file to be
included. If the file is not there, PHP exits
with an error. - include(file) includes the file. If the file is
not there no error is reported.
13require() and include()?
- Both assume that you leave PHP. Thus within your
included file you can write simple HTML. - If you want to include PHP in your included file,
you have to surround it by lt?php and ?gt, just
like in a PHP script. - Here is an example to use include to build the
basic web page. - Included files are customarily give the extension
.inc.
14top.inc
lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN" "http//www.w3.org/TR/xhtml1/DTD/xhtml
1-strict.dtd"gt lthtmlgt ltheadgtlttitlegttitlelt/titlegt
ltmeta http-equiv"content-type"
content"text/html charsetUTF-8"/gt ltlink
rel"stylesheet" type"text/css"
href"main.css"/gt lt/headgt ltbodygt
15bottom.inc
ltp id"validator"gt lta href"http//validator.w3.or
g/check?urireferer"gtltimg style"border
0pt" src"http//wotan.liu.edu/valid-xhtml10.p
ng" alt"Valid XHTML 1.0!" height"31"
width"88" /gtlt/agt lt/pgt lt/bodygt lt/htmlgt
16trouble
- title in the top.html is not understood as the
title. It reads as title, which means "idiot"
for your web user. - Even if you replace title with
- lt?php title ?gt
- title is empty. The definition from the
outer file is not seen in the included file. - So you have to split into three files, and print
the title in the main file. I leave that to you
to figure out.
17login.php create_account.php
- Both require a database that has three fields
- id which is an auto_increment int acting as a
handle - username is the username of the account. it must
be unique and this is enforced by mySQL - password is a varchar(41) because the sha1 of the
password is stored. This is 40 chars long.
18sessions
- You will recall that HTTP is a stateless
protocol. Each request/response is
self-contained. - Statefulness is crucial in Web applications.
Otherwise users have to authenticate every time
they access a new page. - Traditionally, one way to create statefulness is
to use cookies. - PHP uses cookies to create a concept of its own,
sessions, that makes it all very easy.
19cookies
- A cookie is a piece of attribute/value data. A
server can send cookies as value of a HTTP header
Set-Cookie. Multiple headers may be sent. - When the client visits the web site again, it
will send the cookie back to the server with a
HTTP header Cookie
20Set-Cookie
- Set-Cookie namevalue expires date
pathpath domaindomain secure - where indicates optional components and
- name is the variable name set in the cookie
- value is the variable's value
- date is a date when the cookie expires
- path restricts the cookie to be sent only when
requests to a path starting with path are made - domain restricts the sending of the cookie to a
certain domain - secure restricts transmission to https
21Cookies
- The browser compares the request it wants to make
with the URL and the domain that sent the cookie. - If the path is not set the cookie will only be
sent to a request with the originating URL. - If the cookie matches the request a request
header of the form - Cookie name1value1 name2value2
- is sent.
22sessions
- Sessions are a feature of PHP. PHP remembers a
session through a special cookie PHPSESSID. - To activate the sessions, include
session_start() at the beginning of your script,
before any printing has been done. - One a session is active, you have a special
super-global variable _SESSION. Session data is
stored in special files on wotan.
23_SESSION
- This is an array where you can read and set
variables that you want to keep during the
session. - if(_SESSION'user_name)
- print "welcome " . _SESSION'user_name
-
- else
- // show users login form
- print login_form()
24ending sessions
- At 9 and 39 past each hour, wotan deletes all
session files that have not been changed for 24
minutes or more. - If you want to remove a session yourself, you can
call session_destroy() in your script. - An example is in visit.php.
25http//openlib.org/home/krichel
Thank you for your attention! Please switch off
machines b4 leaving!