More on Func+ons PHP Func+on names can be stored in - - PowerPoint PPT Presentation

more on func ons
SMART_READER_LITE
LIVE PREVIEW

More on Func+ons PHP Func+on names can be stored in - - PowerPoint PPT Presentation

More on Func+ons PHP Func+on names can be stored in variables and called through the variables This allows the func+on that is used to be


slide-1
SLIDE 1

1 ¡

More ¡on ¡Func+ons ¡

  • PHP ¡Func+on ¡names ¡can ¡be ¡stored ¡in ¡variables ¡

and ¡called ¡through ¡the ¡variables ¡

– This ¡allows ¡the ¡func+on ¡that ¡is ¡used ¡to ¡be ¡determined ¡ dynamically ¡(at ¡run ¡+me) ¡ – Can ¡be ¡handy ¡if ¡different ¡func+ons ¡need ¡to ¡be ¡called ¡ in ¡different ¡situa+ons ¡

  • PHP ¡func+ons ¡can ¡have ¡default ¡arguments ¡

– If ¡no ¡actual ¡parameter ¡is ¡supplied ¡the ¡default ¡value ¡is ¡ used ¡

  • See ¡ex11.php ¡
slide-2
SLIDE 2

2 ¡

PHP ¡Files ¡

  • Using ¡files ¡in ¡PHP ¡is ¡fairly ¡straighIorward ¡

– Can ¡open ¡a ¡file ¡for ¡reading, ¡wri+ng, ¡append, ¡and ¡ a ¡couple ¡varia+ons ¡of ¡reading+wri+ng ¡

  • Note ¡1: ¡Files ¡are ¡not ¡covered ¡in ¡the ¡Sebesta ¡text ¡
  • Note ¡2: ¡You ¡may ¡have ¡to ¡set ¡some ¡permissions ¡on ¡

your ¡file ¡system ¡to ¡allow ¡your ¡server ¡write ¡access ¡to ¡ files ¡

  • There ¡are ¡a ¡few ¡different ¡ways ¡to ¡access ¡files ¡

in ¡PHP ¡

– Many ¡C ¡file ¡func+ons ¡are ¡almost ¡iden+cal ¡in ¡PHP ¡

  • Ex: ¡fopen, ¡fseek, ¡fscanf, ¡fgetc, ¡fgets ¡
  • See ¡the ¡manual ¡for ¡complete ¡list ¡
slide-3
SLIDE 3

3 ¡

PHP ¡Files ¡

  • Opening ¡files ¡

– Typically ¡we ¡use ¡fopen() ¡to ¡open ¡a ¡file ¡for ¡either ¡ reading ¡or ¡wri+ng ¡

$fp ¡= ¡fopen(<filename>, ¡<mode>); ¡

  • Where ¡<filename> ¡is ¡the ¡path/name ¡of ¡a ¡file ¡that ¡is ¡

accessible ¡to ¡the ¡server ¡

  • Where ¡<mode> ¡specifies ¡how ¡the ¡file ¡will ¡be ¡accessed ¡

– Ex: ¡"r" ¡à ¡read ¡only ¡ ¡ ¡"r+" ¡à ¡read/write ¡with ¡pointer ¡at ¡beginning ¡ » The ¡above ¡modes ¡require ¡the ¡file ¡to ¡already ¡exist ¡ ¡

slide-4
SLIDE 4

4 ¡

PHP ¡Files ¡

¡ ¡ ¡ ¡"w" ¡à ¡write ¡only ¡

"w+" ¡à ¡write ¡/ ¡read, ¡trunca+ng ¡previous ¡file ¡length ¡to ¡0 ¡

  • For ¡the ¡above ¡modes, ¡the ¡server ¡will ¡a\empt ¡to ¡create ¡the ¡

file ¡if ¡it ¡does ¡not ¡exist. ¡ ¡

– Also ¡"a" ¡and ¡"a+" ¡for ¡append ¡modes ¡

  • Reading ¡from ¡files ¡

– For ¡text ¡files, ¡we ¡can ¡read ¡different ¡amounts ¡per ¡read ¡ depending ¡on ¡our ¡requirements ¡

  • Read ¡a ¡single ¡character ¡at ¡a ¡+me ¡– ¡fgetc() ¡
  • Read ¡a ¡string ¡(line ¡\n) ¡ ¡ ¡-­‑ ¡ ¡ ¡fgets() ¡
  • Opens/Read ¡the ¡en+re ¡file ¡into ¡a ¡single ¡string ¡– ¡readfile() ¡
  • Opens/Read ¡the ¡lines ¡of ¡the ¡file ¡into ¡an ¡array ¡of ¡strings ¡– ¡file() ¡

– Can ¡also ¡read ¡binary ¡data ¡if ¡necessary ¡

  • Ex: ¡images, ¡audio, ¡etc. ¡
slide-5
SLIDE 5

5 ¡

PHP ¡Files ¡

– PHP ¡allows ¡all ¡of ¡these ¡with ¡various ¡func+ons ¡

  • Look ¡at ¡the ¡op+ons ¡in ¡the ¡manual ¡
  • See: ¡h\p://php.net/manual/en/ref.filesystem.php ¡ ¡
  • Wri+ng ¡to ¡files ¡

– Most ¡commonly ¡done ¡with ¡fwrite ¡ – Again ¡see ¡manual ¡for ¡details ¡ ¡

  • Very ¡Simple ¡Example: ¡

– See ¡readwrite.php ¡

slide-6
SLIDE 6

Debugging Note

  • Many ¡situa+ons ¡that ¡produce ¡compila+on ¡or ¡

run-­‑+me ¡errors ¡in ¡Java ¡will ¡not ¡do ¡so ¡in ¡PHP ¡

  • Ex: ¡Accessing ¡a ¡variable ¡that ¡has ¡no ¡value: ¡

$count ¡= ¡$count ¡+ ¡1; ¡

  • Ex: ¡Reading ¡a ¡file ¡that ¡does ¡not ¡exist: ¡

$data ¡= ¡file(“nonexistenIile.txt”); ¡

– However, ¡these ¡situa+ons ¡will ¡produce ¡warnings, ¡ which ¡we ¡can ¡elect ¡to ¡see ¡or ¡not ¡see ¡in ¡the ¡ returned ¡web ¡page ¡

  • We ¡can ¡determine ¡whether ¡these ¡warnings ¡(and ¡actual ¡

errors) ¡are ¡seen ¡or ¡not ¡via ¡.htaccess ¡files ¡

6

slide-7
SLIDE 7

Debugging Note

  • These ¡are ¡configura+on ¡files ¡that ¡allow ¡per ¡directory ¡

configura+on ¡op+ons ¡for ¡the ¡server ¡

  • For ¡example ¡the ¡sehngs: ¡

php_value display_errors 1 php_value display_startup_errors 1

– will ¡send ¡PHP ¡warnings ¡back ¡to ¡the ¡client ¡browser ¡

  • And ¡the ¡sehngs: ¡

php_value display_errors 0 php_value display_startup_errors 0

– will ¡hide ¡the ¡warnings ¡from ¡the ¡user ¡

  • Note: ¡In ¡some ¡installa?ons ¡these ¡cause ¡problems ¡for ¡

the ¡server ¡– ¡if ¡these ¡cause ¡an ¡error ¡in ¡your ¡server ¡ don’t ¡use ¡them ¡

7

slide-8
SLIDE 8

PHP Files

  • Flocking ¡files ¡
  • See ¡h\p://php.net/manual/en/func+on.flock.php ¡ ¡

– The ¡flock() ¡func+on ¡is ¡called ¡to ¡restrict ¡access ¡to ¡ files ¡(when ¡necessary) ¡to ¡one ¡“user” ¡at ¡a ¡+me ¡

  • If ¡each ¡“user” ¡calls ¡flock() ¡prior ¡to ¡accessing ¡a ¡file ¡

pointer ¡to ¡the ¡same ¡file, ¡only ¡one ¡will ¡be ¡allowed ¡to ¡ access ¡it ¡at ¡a ¡+me ¡

– Why ¡do ¡we ¡need ¡this? ¡

  • Mul+ple ¡users ¡frequently ¡access ¡the ¡same ¡server ¡
  • Server ¡typically ¡spawns ¡a ¡separate ¡process ¡for ¡each ¡

user ¡

8

slide-9
SLIDE 9

9 ¡

PHP ¡Files ¡

  • These ¡processes ¡can ¡execute ¡in ¡pseudo-­‑

parallel ¡or ¡in ¡actual ¡parallel ¡depending ¡on ¡how ¡ the ¡server ¡is ¡configured ¡ ¡

  • Consider ¡the ¡following ¡scenario ¡for ¡process ¡

P1: ¡

  • Read ¡a ¡file ¡into ¡an ¡array ¡
  • Update ¡a ¡value ¡in ¡the ¡array ¡
  • Write ¡the ¡array ¡back ¡to ¡the ¡file ¡

– What ¡if ¡process ¡P2 ¡writes ¡to ¡the ¡file ¡between ¡P1's ¡ reading ¡and ¡wri+ng? ¡ – If ¡used ¡correctly, ¡flock() ¡can ¡prevent ¡this ¡problem ¡

  • See ¡flock.php ¡
slide-10
SLIDE 10

PHP Files

– Note: ¡This ¡issue ¡is ¡difficult ¡to ¡test ¡with ¡localhost ¡ server ¡access ¡

  • Prac+cally ¡speaking ¡we ¡can ¡only ¡submit ¡one ¡request ¡at ¡

a ¡+me ¡

  • However ¡an ¡online ¡server ¡can ¡be ¡accessed ¡via ¡many ¡

clients, ¡with ¡many ¡coinciding ¡processes ¡

  • See ¡demo ¡of ¡flock.php ¡on ¡collab ¡site ¡

10

slide-11
SLIDE 11

11 ¡

CGI ¡and ¡Scripts ¡

  • CGI ¡-­‑ ¡Common ¡Gateway ¡Interface ¡
  • h\p://en.wikipedia.org/wiki/Common_Gateway_Interface ¡
  • h\p://tools.ieI.org/html/rfc3875 ¡ ¡ ¡

– Interface ¡for ¡Web ¡servers ¡that ¡interact ¡with ¡ browsers, ¡u+lizing ¡scrip+ng ¡languages ¡and ¡the ¡ HTTP ¡(HyperText ¡Transfer ¡Protocol) ¡ – Used ¡to ¡allow ¡data ¡interac+on ¡between ¡user ¡and ¡ server ¡scripts ¡

  • Ex. ¡Extrac+ng ¡data ¡sent ¡via ¡HTTP ¡requests ¡and ¡passing ¡

to ¡scripts ¡

slide-12
SLIDE 12

12 ¡

CGI ¡and ¡Scripts ¡

  • Two ¡best ¡known ¡HTTP ¡methods: ¡GET ¡and ¡POST ¡

– GET ¡

  • Appends ¡user ¡input ¡to ¡URL ¡and ¡requests ¡

corresponding ¡document ¡

  • Server ¡parses ¡URL ¡– ¡first ¡part ¡is ¡a ¡program ¡that ¡it ¡

¡ ¡ ¡ ¡invokes, ¡second ¡is ¡parameter ¡passed ¡along ¡

Recommended ¡usage ¡for ¡safe ¡and ¡idempotent ¡

requests ¡

– Safe: ¡

  • For ¡retrieval ¡only ¡– ¡has ¡no ¡side ¡effects ¡on ¡the ¡server ¡

– Idempotent: ¡

  • Making ¡N ¡> ¡1 ¡iden+cal ¡requests ¡has ¡the ¡same ¡effect ¡as ¡making ¡
  • nly ¡1 ¡request ¡

– See: ¡ h\p://www.w3.org/Protocols/rfc2616/rfc2616-­‑ sec9.html ¡ ¡

slide-13
SLIDE 13

GET and POST

  • POST ¡

– sends ¡data ¡as ¡a ¡stream ¡to ¡script ¡program ¡ – more ¡suitable ¡for ¡large ¡amounts ¡of ¡data ¡ – arguments ¡are ¡not ¡shown ¡in ¡address ¡but ¡are ¡s+ll ¡ extracted ¡and ¡processed ¡by ¡server ¡ – Used ¡for ¡requests ¡that ¡may ¡alter ¡/ ¡update ¡the ¡ server ¡

  • i.e. ¡NOT ¡safe ¡and ¡NOT ¡idempotent ¡
  • Ex: ¡update ¡a ¡database ¡

– Ex: ¡submit ¡a ¡payment ¡

13

slide-14
SLIDE 14

14 ¡

CGI ¡and ¡Scripts ¡

  • GET ¡and ¡POST ¡are ¡ouen ¡used ¡within ¡HTML ¡

forms ¡

– User ¡enters ¡data ¡into ¡form ¡and ¡then ¡SUBMITS ¡it ¡ – Browser ¡processes ¡form ¡and ¡passes ¡choices ¡and ¡ informa+on ¡to ¡the ¡url ¡specified ¡ – Server ¡invokes ¡appropriate ¡script ¡u+lizing ¡ requested ¡method, ¡extrac+ng ¡submi\ed ¡data ¡

  • Most ¡scrip+ng ¡languages ¡(including ¡PHP) ¡have ¡

predefined ¡ways ¡to ¡easily ¡extract ¡this ¡data ¡

  • This ¡data ¡is ¡used ¡as ¡input ¡to ¡the ¡script ¡
slide-15
SLIDE 15

15 ¡

CGI ¡and ¡Scripts ¡

– Results ¡are ¡sent ¡back ¡to ¡browser ¡and ¡displayed ¡in ¡ the ¡Web ¡browser ¡ – See ¡getpost.html ¡and ¡getpost.php ¡

  • Since ¡scripts ¡are ¡executed ¡by ¡the ¡server ¡and ¡

can ¡access ¡files ¡on ¡the ¡server ¡

– It ¡is ¡prudent ¡for ¡a ¡webmaster ¡to ¡be ¡cau+ous ¡about ¡ the ¡scripts ¡placed ¡onto ¡the ¡server ¡ – For ¡example ¡many ¡servers ¡will ¡only ¡execute ¡ “approved” ¡scripts ¡placed ¡into ¡an ¡approved ¡ directory ¡

slide-16
SLIDE 16

Exercise ¡2 ¡

  • Using ¡your ¡Web ¡server, ¡create ¡a ¡primi+ve ¡

Web ¡Site ¡called ¡"CS ¡4501 ¡Exercise ¡2". ¡ ¡

  • Use ¡a ¡simple ¡file ¡and ¡a ¡PHP ¡script ¡to ¡keep ¡

track ¡of ¡the ¡number ¡of ¡+mes ¡the ¡site ¡has ¡been ¡ accessed ¡(or ¡refreshed), ¡and ¡show ¡the ¡count ¡

  • n ¡the ¡page. ¡ ¡