more on func ons
play

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


  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 ¡ 1 ¡

  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 ¡ 2 ¡

  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 ¡ ¡ 3 ¡

  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. ¡ 4 ¡

  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 ¡ 5 ¡

  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

  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

  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

  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 ¡ 9 ¡

  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

  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 ¡ 11 ¡

  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 ¡ only ¡1 ¡request ¡ – See: ¡ h\p://www.w3.org/Protocols/rfc2616/rfc2616-­‑ sec9.html ¡ ¡ 12 ¡

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend