83
Chapter Eight PHP Advanced Part I by M.R.M. Rosman Free Powerpoint Templates Page 1 M.R.M. Rosman | http://rahimi.uitm.edu.my Free Powerpoint Templates

Chapter 8A - PHP Pre-Built Function

Embed Size (px)

DESCRIPTION

php built function

Citation preview

  • ChapterEightPHPAdvancedPartI

    byM.R.M.Rosman

    FreePowerpointTemplates Page1

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    FreePowerpointTemplates

  • Copyright 2012M.R.M. Rosman. All right reserved torespective owner. No part of this publication may bereproducedordistributed inany formorbyanymeans,including,butnotlimitedto,theprocessofscanninganddigitization, or stored in a databaseor retrieval system,withoutthepriorwrittenpermissionoftheowner.

    FreePowerpointTemplates Page2

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    withoutthepriorwrittenpermissionoftheowner.

  • Introduction Thischapterwillcover:

    FreePowerpointTemplates Page3

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • FreePowerpointTemplates Page4

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    DATE()FUNCTIONCHAPTEREIGHT

  • PHPDate()Function

    The PHP date() function formats a timestamp to amorereadabledateandtime.

    AtimestampisthenumberofsecondssinceJanuary1,1970at00:00:00GMT.This isalsoknownastheUnixTimestamp

    Syntax:

    FreePowerpointTemplates Page5

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • PHPDateFormattheDateThe first parameter in the date() functionspecifieshowtoformatthedate/time.It uses letters to represent date and timeformats: dThedayofthemonth(0131)mThecurrentmonth,asanumber(0112) YThecurrentyearinfourdigits

    FreePowerpointTemplates Page6

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    Example1

  • Parameter Description

    d Thedayofthemonth(from01to31)

    D Atextualrepresentationofaday(threeletters)

    j Thedayofthemonthwithoutleadingzeros(1to31)

    I Afulltextualrepresentationofaday

    z Thedayoftheyear(from0through365)

    W TheISO8601weeknumberofyear

    FreePowerpointTemplates Page7

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    W TheISO8601weeknumberofyear(weeksstartingonMonday)

    F Afulltextualrepresentationofamonth(JanuarythroughDecember)

    m Anumericrepresentationofamonth(from01to12)

    M Ashorttextualrepresentationofamonth(threeletters)

    n Anumericrepresentationofamonth,withoutleadingzeros(1to12)

  • Parameter Description

    t Thenumberofdaysinthegivenmonth

    L Whetherit'saleapyear(1ifitisaleapyear,0otherwise)

    Y Afourdigitrepresentationofayear

    y Atwodigitrepresentationofayear

    a Lowercaseamorpm

    A UppercaseAMorPM

    g 12hourformatofanhour(1to12)

    FreePowerpointTemplates Page8

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    g 12hourformatofanhour(1to12)

    G 24hourformatofanhour(0to23)

    h 12hourformatofanhour(01to12)

    H 24hourformatofanhour(00to23)

    i Minuteswithleadingzeros(00to59)

    s Seconds,withleadingzeros(00to59)

  • DateDefaultTimezone

    Sometimes its necessary todeclaredefaulttimezone.

    FreePowerpointTemplates Page9

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • Example

    Example1

  • FreePowerpointTemplates Page11

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    Source:http://php.net/manual/en/function.date.php

  • FreePowerpointTemplates Page12

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    SERVERSIDEINCLUDES(SSI)

    CHAPTEREIGHT

  • ServerSideIncludes The include() and require() functioncan be use to insert banner, menu,specific code, functions, footers, oranycontentoffileintoPHP(similartotheconceptofexternalCSS)

    Thedifferencesbetweenthosetwoisthewaytheyhandleerrors. Include()

    FreePowerpointTemplates Page13

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    thewaytheyhandleerrors. Include()generate warning but continueexecuting the codes while require()generatesafatalerror.

  • WhySSI?

    Save the developer a considerableamountoftime.

    Allow you to create standard designtobereusedwithinyourproject.

    FreePowerpointTemplates Page14

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    tobereusedwithinyourproject. Updating is easier because can justupdatingasinglefile.

  • FreePowerpointTemplates Page15

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    INCLUDE()FUNCTIONCHAPTEREIGHT

  • include()function

    The include() function takes allthe text in a specified file andcopies it into the file that usestheincludefunction.

    FreePowerpointTemplates Page16

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    theincludefunction.

  • Example

    FreePowerpointTemplates Page17

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    Create2newPHPFiles

    test.php menu.php

  • test.php

    Example1

  • menu.php

    Home|Biodata|Education|

    2

    FreePowerpointTemplates Page19

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • FreePowerpointTemplates Page20

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    REQUIRE()FUNCTIONCHAPTEREIGHT

  • Therequire()Function The require() function is similarto include(), they only handleerrorsdifferently.

    The require() functiongeneratesa fatal error and the script

    FreePowerpointTemplates Page21

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    a fatal error and the scriptexecution will stop after theerror.

  • Comparison

    FreePowerpointTemplates Page22

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • FreePowerpointTemplates Page23

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    FILEUPLOADCHAPTEREIGHT

  • UploadingFile

    Uploading file is possible withPHP.

    Restrictions on upload iscompulsoryasuploading files is

    FreePowerpointTemplates Page24

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    compulsoryasuploading files isconsiderasasecurityrisk.

    Allowonlytrusteduserstodosoand keep track the type of filesthattheycanupload

  • Example Refer to Introduction toPHP Projects: Abeginners handbook,pages2930.

    FreePowerpointTemplates Page25

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • UploadingFileThe enctype is declared withinform tag to specify the type ofcontentwhenyousubmittheform"multipart/formdata" isusedwhena form requires binary data, likethe contents of a file, to beuploaded

    FreePowerpointTemplates Page26

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    the contents of a file, to beuploadedThe type="file" attribute of thetagspecifiesthattheinputshouldbepossessedasafile.When viewed in a browser, therewillbeabrowsebuttonnexttotheinputfield

  • PHP$_FILES

    Global PHP $_FILES array can be used to uploadfilesintoserver.

    Thefirstparameteristheform'sinputnameandthesecond index can be either "name", "type", "size","tmp_name"or"error.

    Parameter Description

    FreePowerpointTemplates Page27

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    Parameter Description

    $_FILES["file"]["name"] The nameoftheupload file

    $_FILES["file"]["type"] The type of the file upload (Such asJPEG,GIF)

    $_FILES["file"]["size"] The sizeinbytesoftheuploaded file

    $_FILES["file"]["tmp_name"] The name of the temporary copy ofthefilestoredontheserver

    $_FILES["file"]["error"] Theerrorcode resulting from the fileupload

  • Example2

    FileUploadName:

    gallery.php

    FreePowerpointTemplates Page28

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    Picture:

  • Example2(cont)

  • FreePowerpointTemplates Page30

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • RestrictionsonUpload

    Allowing file upload withoutrestriction is a risky activitiesunlessyouare theadministratorofthesite.

    FreePowerpointTemplates Page31

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    ofthesite. Restriction can be made uponfile size, file type, or any othercriteria's.

  • ByteConversionChart

    FreePowerpointTemplates Page34

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • MIMEFileTypePg.1

    FreePowerpointTemplates Page35

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • MIMEFileTypePg.2

    FreePowerpointTemplates Page36

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • MIMEFileTypePg.3

    FreePowerpointTemplates Page37

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • MIMEFileTypePg.4

    FreePowerpointTemplates Page38

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • FreePowerpointTemplates Page39

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    USINGCOOKIESCHAPTEREIGHT

  • PHPCookie Cookie is used to identify thepersonwhologonintoasystem.

    Its used to kept a uniqueidentifier so that it can be usedmany times until the person log

    FreePowerpointTemplates Page40

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    many times until the person logoutfromthesystem.

  • FreePowerpointTemplates Page41

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • FreePowerpointTemplates Page42

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my Higuys!!Doyou

    knowwhathappenedafteryoupressthelogin

    button?

  • FreePowerpointTemplates Page43

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my Whenyoupress

    thebutton,yourusernameandpasswordwillbesenttoascript

  • FreePowerpointTemplates Page44

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my Authenticateduser

    willbepromptintomainpageaftersuccessfulloginwhilebadloginusuallyprompted

    backintologinpage

  • Then,howshouldIrecognizetheperson

    whoentermysystem?Hmm,letsmecheckhim/her

    first.

    Step1

    FreePowerpointTemplates Page45

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • Hi,howareyou?

    Step2

    FreePowerpointTemplates Page46

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    Fine,thanksforasking.

  • Canyougivemeyouusername?

    Step3

    FreePowerpointTemplates Page47

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    Sure.Myusernameissiti2010

  • Ok.Ifoundyourusername.NowgivemeyoupasswordsothatIknowthatImtalkingwiththeright

    person.

    Step5

    FreePowerpointTemplates Page48

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    Mypasswordiseureka90210

  • Ok.Imatchedyoupasswordwithyouusername.Your

    nameisSiti AminahBtAbdullah.Youridis3.Iwillstoredyouridforreferences.

    Step6For wrong passwordrepeatstep3

    FreePowerpointTemplates Page49

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    Ok.Thanksalot!Wait,howdoyoustoredmylogincredentials?

  • Wehavetwooption.Firstbyusingcookieandanotherbyusingsession.Wewill

    storedyoureferenceidthatisunique.

    Step7

    COOKIE

    SESSION

    FreePowerpointTemplates Page50

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    Oh,Isee.Thankyouforyourtime!

    )

  • Hi,mynameisSiti.Nicetomeetyou!

    COOKIE identifies the person whocommunicatewiththeinformationsystem

    Step8

    FreePowerpointTemplates Page51

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    Okguys.ThisgirlisSitiwithID3fromtbl_user.

  • FreePowerpointTemplates Page52

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    USINGCOOKIECHAPTEREIGHT

  • COOKIE

    Cookie isused tostoreduniqueidentifierofaperson.

    The itemstoredmustbeuniqueandnoduplication.

    FreePowerpointTemplates Page53

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    andnoduplication.

  • COOKIE

    ThePHP$_COOKIEvariable isusedtoretrieveacookievalue.

  • Example

    UsingCookie

    FreePowerpointTemplates Page55

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • FreePowerpointTemplates Page56

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    COOKIETUTORIALCHAPTEREIGHT

  • INSTRUCTION

    Createafoldernamelogin1andfourPHPfiles:

    FreePowerpointTemplates Page57

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    index.php login.php

    main.php logout.php

  • index.php

    FreePowerpointTemplates Page58

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • login.php

    FreePowerpointTemplates Page59

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • main.php

    FreePowerpointTemplates Page60

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • logout.php

    FreePowerpointTemplates Page61

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • LOGIN

    FreePowerpointTemplates Page62

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    LOGOUT

  • UnsupportedBrowsers

    There are some browser thatdoesntsupportcookies.Todealwith this matter there are twopossiblesolutions:Useformtomovedatafromapagetoanother.

    FreePowerpointTemplates Page63

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    toanother.Usesessioncookies

  • FreePowerpointTemplates Page64

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    USINGSESSIONSCHAPTEREIGHT

  • PHPSessionVariables Session variables allow you to storedata temporarily, just as cookiesdoes.

    Thevariablesordatawillbedeletedwhenyouleftyourbrowser.

    Sessionscreateauniqueid(UID)for

    FreePowerpointTemplates Page65

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    Sessionscreateauniqueid(UID)foreach visitor and store variablesbasedonthisUID.

    TheUID is either stored in a cookieorispropagatedintheURL.

  • StartingaPHPSession Before you can store userinformationinyourPHPsession,you must first start up thesession.

    The session_start() function

    FreePowerpointTemplates Page66

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    The session_start() functionmust appear BEFORE thetag:

  • StoringaSessionVariable You can store value using$_SESSIONvariables:

    FreePowerpointTemplates Page67

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • DisplayingaSessionVariable You dispay value using$_SESSIONvariables:

    FreePowerpointTemplates Page68

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • FreeingSessionVariable

    The unset() function is used to free thespecifiedsessionvariable:

    FreePowerpointTemplates Page69

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • DestroyingaSessionsession_destroy()willdestroyorresetyour session and all storedinformationwilllose.

    FreePowerpointTemplates Page70

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • FreePowerpointTemplates Page71

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    SESSIONTUTORIALCHAPTEREIGHT

  • INSTRUCTION

    Createafoldernamelogin2andfourPHPfiles:

    FreePowerpointTemplates Page72

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    index.php login.php

    main.php logout.php

  • index.php

    FreePowerpointTemplates Page73

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • login.php

    FreePowerpointTemplates Page74

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • main.php

    FreePowerpointTemplates Page75

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • logout.php

    FreePowerpointTemplates Page76

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • LOGIN

    FreePowerpointTemplates Page77

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    LOGOUT

  • FreePowerpointTemplates Page78

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    MAILFUNCTIONCHAPTEREIGHT

  • ThePHPmail()Function

    ThePHPmail() function canbeused tosendemails from insideascript.

    FreePowerpointTemplates Page79

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • Parameters

    Syntax Description

    to Required.Specifiesthereceiver/receiversoftheemail

    Subjects Required.Specifiesthesubjectoftheemail

    message Required.Specifiesmessagetobesent.

    FreePowerpointTemplates Page80

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    headers Optional.SpecifiesheaderslikeFrom,Cc,andBcc

    parameters Optional.Specifiesadditionalparametertosendmail

    program.

  • PHPSimpleEMail

    The simplest way to send anemailwithPHPistosendatextemail.

    Intheexamplebelowwedeclare

    FreePowerpointTemplates Page81

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

    Intheexamplebelowwedeclarethe variables ($to, $subject,$message, $from, $headers),thenweusethevariables in themail()functiontosendanemail:

  • FreePowerpointTemplates Page82

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my

  • Exercise

    Create a simple upload form touploadimageintotherootfolder.

    FreePowerpointTemplates Page83

    M.R.M.R

    osman|http://rahimi.uitm

    .edu.my