Documente Academic
Documente Profesional
Documente Cultură
PHPAdvancedPartI
M.R.M.Rosman|http://rahimi.uitm.edu.my
byM.R.M.Rosman
FreePowerpointTemplates
FreePowerpointTemplates
Page1
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
Page2
Introduction
M.R.M.Rosman|http://rahimi.uitm.edu.my
Thischapterwillcover:
FreePowerpointTemplates
Page3
M.R.M.Rosman|http://rahimi.uitm.edu.my
CHAPTEREIGHT
DATE()FUNCTION
FreePowerpointTemplates
Page4
PHPDate()Function
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
Page5
PHPDateFormattheDate
The first parameter in the date() function
specifieshowtoformatthedate/time.
It uses letters to represent date and time
formats:
M.R.M.Rosman|http://rahimi.uitm.edu.my
dThedayofthemonth(0131)
mThecurrentmonth,asanumber(0112)
YThecurrentyearinfourdigits
<html>
<head><title>Example1</title></head>
<body>
<?php
echodate("d/m/Y")
?>
</body></html>
FreePowerpointTemplates
Page6
M.R.M.Rosman|http://rahimi.uitm.edu.my
Parameter
Description
Thedayofthemonth(from01to31)
Atextualrepresentationofaday(three
letters)
Thedayofthemonthwithoutleadingzeros
(1to31)
Afulltextualrepresentationofaday
Thedayoftheyear(from0through365)
TheISO8601weeknumberofyear
(weeksstartingonMonday)
Afulltextualrepresentationofamonth
(JanuarythroughDecember)
Anumericrepresentationofamonth(from
01to12)
Ashorttextualrepresentationofamonth
(threeletters)
Anumericrepresentationofamonth,
withoutleadingzeros(1to12)
FreePowerpointTemplates
Page7
M.R.M.Rosman|http://rahimi.uitm.edu.my
Parameter
Description
Thenumberofdaysinthegivenmonth
Whetherit'saleapyear(1ifitisaleap
year,0otherwise)
Afourdigitrepresentationofayear
Atwodigitrepresentationofayear
Lowercaseamorpm
UppercaseAMorPM
12hourformatofanhour(1to12)
24hourformatofanhour(0to23)
12hourformatofanhour(01to12)
24hourformatofanhour(00to23)
Minuteswithleadingzeros(00to59)
Seconds,withleadingzeros(00to59)
FreePowerpointTemplates
Page8
DateDefaultTimezone
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
Page9
M.R.M.Rosman|http://rahimi.uitm.edu.my
Example
<html>
<head><title>Example1</title></head>
<body>
<?php
date_default_timezone_set('Asia/Kuala_Lumpur')
echodate("d/m/Y")
echo"<br>".date("D")
echo"<br>".date("H:s")
?>
</body></html>
FreePowerpointTemplates
Page10
M.R.M.Rosman|http://rahimi.uitm.edu.my
Source:http://php.net/manual/en/function.date.php
FreePowerpointTemplates
Page11
M.R.M.Rosman|http://rahimi.uitm.edu.my
CHAPTEREIGHT
SERVERSIDEINCLUDES
(SSI)
FreePowerpointTemplates
Page12
M.R.M.Rosman|http://rahimi.uitm.edu.my
ServerSideIncludes
The include() and require() function
can be use to insert banner, menu,
specific code, functions, footers, or
anycontentoffileintoPHP(similarto
theconceptofexternalCSS)
Thedifferencesbetweenthosetwois
thewaytheyhandleerrors.Include()
generate warning but continue
executing the codes while require()
generatesafatalerror.
FreePowerpointTemplates
Page13
M.R.M.Rosman|http://rahimi.uitm.edu.my
WhySSI?
Save the developer a considerable
amountoftime.
Allow you to create standard design
tobereusedwithinyourproject.
Updating is easier because can just
updatingasinglefile.
FreePowerpointTemplates
Page14
M.R.M.Rosman|http://rahimi.uitm.edu.my
CHAPTEREIGHT
INCLUDE()FUNCTION
FreePowerpointTemplates
Page15
include()function
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
Page16
Example
M.R.M.Rosman|http://rahimi.uitm.edu.my
test.php
menu.php
Create2newPHPFiles
FreePowerpointTemplates
Page17
M.R.M.Rosman|http://rahimi.uitm.edu.my
test.php
<html>
<head><title>Example1</title></head>
<body>
<?php
include("menu.php")
echo"<br>WelcometoMyPersonalSystem"
?>
</body></html>
FreePowerpointTemplates
Page18
menu.php
M.R.M.Rosman|http://rahimi.uitm.edu.my
<ahref="index.php">Home</a>|
<ahref="biodata.php">Biodata</a>|
<ahref="education.php">Education</a>|
FreePowerpointTemplates
Page19
M.R.M.Rosman|http://rahimi.uitm.edu.my
CHAPTEREIGHT
REQUIRE()FUNCTION
FreePowerpointTemplates
Page20
M.R.M.Rosman|http://rahimi.uitm.edu.my
Therequire()Function
The require() function is similar
to include(), they only handle
errorsdifferently.
The require() function generates
a fatal error and the script
execution will stop after the
error.
FreePowerpointTemplates
Page21
M.R.M.Rosman|http://rahimi.uitm.edu.my
Comparison
FreePowerpointTemplates
Page22
M.R.M.Rosman|http://rahimi.uitm.edu.my
CHAPTEREIGHT
FILEUPLOAD
FreePowerpointTemplates
Page23
M.R.M.Rosman|http://rahimi.uitm.edu.my
UploadingFile
Uploading file is possible with
PHP.
Restrictions on upload is
compulsory as uploading files is
considerasasecurityrisk.
Allowonlytrusteduserstodoso
and keep track the type of files
thattheycanupload
FreePowerpointTemplates
Page24
Example
M.R.M.Rosman|http://rahimi.uitm.edu.my
Refer to Introduction to
PHP
Projects:
A
beginners handbook,
pages2930.
FreePowerpointTemplates
Page25
M.R.M.Rosman|http://rahimi.uitm.edu.my
UploadingFile
The enctype is declared within
form tag to specify the type of
contentwhenyousubmittheform
"multipart/formdata" is used when
a form requires binary data, like
the contents of a file, to be
uploaded
The type="file" attribute of the
<input>tagspecifiesthattheinput
shouldbepossessedasafile.
When viewed in a browser, there
willbeabrowsebuttonnexttothe
inputfield
FreePowerpointTemplates
Page26
PHP$_FILES
Global PHP $_FILES array can be used to upload
filesintoserver.
Thefirstparameteristheform'sinputnameandthe
second index can be either "name", "type", "size",
"tmp_name"or"error.
M.R.M.Rosman|http://rahimi.uitm.edu.my
Parameter
Description
$_FILES["file"]["name"]
$_FILES["file"]["type"]
$_FILES["file"]["size"]
$_FILES["file"]["tmp_name"]
$_FILES["file"]["error"]
FreePowerpointTemplates
Page27
Example2
M.R.M.Rosman|http://rahimi.uitm.edu.my
gallery.php
<html>
<head><title>FileUpload</title></head>
<body>
<formname="form1"method="post"action="upload.php"enctype="multipart/formdata">
<tablewidth="291"border="0"align="center">
<tr><tdwidth="61">Name</td><tdwidth="12">:</td>
<tdwidth="204"><inputname="stuName"type="text"id="stuName"></td></tr>
<tr>
<td>Picture</td><td>:</td><td><inputtype="file"name="gambar"></td></tr>
<tr><tdcolspan="3"><divalign="center"><inputtype="submit"name="Submit"
value="Submit"></div></td>
</tr>
</table>
</form>
</body></html>
FreePowerpointTemplates
Page28
Example2(cont)
M.R.M.Rosman|http://rahimi.uitm.edu.my
upload.php
<?php
$stuName=$_POST["stuName"]
move_uploaded_file($_FILES["gambar"]["tmp_name"],"upload/".$_FIL
ES["gambar"]["name"])
echo"<imgsrc='upload/".$_FILES["gambar"]["name"]."'width='100'
height='100'><br>"
echo"StudentName$stuName"
echo"<br>Upload:".$_FILES["gambar"]["name"]
echo"<br>Size:".$_FILES["gambar"]["size"]
echo"<br>Type:".$_FILES["gambar"]["type"]
echo"<br>DataUploadSuccessfully"
?>
FreePowerpointTemplates
Page29
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
Page30
M.R.M.Rosman|http://rahimi.uitm.edu.my
RestrictionsonUpload
Allowing file upload without
restriction is a risky activities
unless you are the administrator
ofthesite.
Restriction can be made upon
file size, file type, or any other
criteria's.
FreePowerpointTemplates
Page31
M.R.M.Rosman|http://rahimi.uitm.edu.my
Restrictionbasedonfiletype
<?php
$stuName=$_POST["stuName"]
if($_FILES["gambar"]["type"]=="image/pjpeg")
{
move_uploaded_file($_FILES["gambar"]["tmp_name"],"upload/".$_FILES["gambar"]["name"])
echo"<imgsrc='upload/".$_FILES["gambar"]["name"]."'width='100'height='100'><br>"
echo"StudentName$stuName"
echo"<br>Upload:".$_FILES["gambar"]["name"]
echo"<br>Size:".$_FILES["gambar"]["size"]
echo"<br>Type:".$_FILES["gambar"]["type"]
echo"<br>DataUploadSuccessfully"
}
else
{
echo"FileuploadonlysupportJPEG!"
}
?>
FreePowerpointTemplates
Page32
M.R.M.Rosman|http://rahimi.uitm.edu.my
Restrictionbasedonfilesize
<?php
$stuName=$_POST["stuName"]
if($_FILES["gambar"]["size"]>"20000")
{
move_uploaded_file($_FILES["gambar"]["tmp_name"],"upload/".$_FILES["gambar"]["name"])
echo"<imgsrc='upload/".$_FILES["gambar"]["name"]."'width='100'height='100'><br>"
echo"StudentName$stuName"
echo"<br>Upload:".$_FILES["gambar"]["name"]
echo"<br>Size:".$_FILES["gambar"]["size"]
echo"<br>Type:".$_FILES["gambar"]["type"]
echo"<br>DataUploadSuccessfully"
}
else
{
echo"Fileuploadexceed20000bytes!"
}
?>
FreePowerpointTemplates
Page33
M.R.M.Rosman|http://rahimi.uitm.edu.my
ByteConversionChart
FreePowerpointTemplates
Page34
M.R.M.Rosman|http://rahimi.uitm.edu.my
MIMEFileTypePg.1
FreePowerpointTemplates
Page35
M.R.M.Rosman|http://rahimi.uitm.edu.my
MIMEFileTypePg.2
FreePowerpointTemplates
Page36
M.R.M.Rosman|http://rahimi.uitm.edu.my
MIMEFileTypePg.3
FreePowerpointTemplates
Page37
M.R.M.Rosman|http://rahimi.uitm.edu.my
MIMEFileTypePg.4
FreePowerpointTemplates
Page38
M.R.M.Rosman|http://rahimi.uitm.edu.my
CHAPTEREIGHT
USINGCOOKIES
FreePowerpointTemplates
Page39
M.R.M.Rosman|http://rahimi.uitm.edu.my
PHPCookie
Cookie is used to identify the
personwhologonintoasystem.
Its used to kept a unique
identifier so that it can be used
many times until the person log
outfromthesystem.
FreePowerpointTemplates
Page40
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
Page41
M.R.M.Rosman|http://rahimi.uitm.edu.my
Higuys!!Doyou
knowwhat
happenedafteryou
pressthelogin
button?
FreePowerpointTemplates
Page42
M.R.M.Rosman|http://rahimi.uitm.edu.my
Whenyoupress
thebutton,your
usernameand
passwordwillbe
senttoascript
FreePowerpointTemplates
Page43
M.R.M.Rosman|http://rahimi.uitm.edu.my
Authenticateduser
willbepromptinto
mainpageafter
successfullogin
whilebadlogin
usuallyprompted
backintologinpage
FreePowerpointTemplates
Page44
Step1
M.R.M.Rosman|http://rahimi.uitm.edu.my
Then,howshouldI
recognizetheperson
whoentermy
system?Hmm,lets
mecheckhim/her
first.
FreePowerpointTemplates
Page45
Step2
M.R.M.Rosman|http://rahimi.uitm.edu.my
Hi,howareyou?
Fine,thanksfor
asking.
FreePowerpointTemplates
Page46
Step3
M.R.M.Rosman|http://rahimi.uitm.edu.my
Canyougivemeyou
username?
Sure.My
usernameis
siti2010
FreePowerpointTemplates
Page47
Step5
M.R.M.Rosman|http://rahimi.uitm.edu.my
Ok.Ifoundyour
username.Nowgive
meyoupasswordso
thatIknowthatIm
talkingwiththeright
person.
Mypasswordis
eureka90210
FreePowerpointTemplates
Page48
Step6
M.R.M.Rosman|http://rahimi.uitm.edu.my
Ok.Imatchedyou
passwordwithyou
username.Your
nameisSiti Aminah
BtAbdullah.Yourid
is3.Iwillstoredyour
idforreferences.
Ok.Thanksalot!
Wait,howdoyou
storedmylogin
credentials?
FreePowerpointTemplates
Page49
Step7
M.R.M.Rosman|http://rahimi.uitm.edu.my
Wehavetwooption.
Firstbyusingcookie
COOKIE
andanotherbyusing
session.Wewill
SESSION
storedyoureference
idthatisunique.
Oh,Isee.Thank
youforyourtime!
FreePowerpointTemplates
Page50
Step8
Hi,mynameisSiti.
Nicetomeetyou!
M.R.M.Rosman|http://rahimi.uitm.edu.my
Okguys.Thisgirl
isSitiwithID3
fromtbl_user.
FreePowerpointTemplates
Page51
M.R.M.Rosman|http://rahimi.uitm.edu.my
CHAPTEREIGHT
USINGCOOKIE
FreePowerpointTemplates
Page52
COOKIE
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
Page53
COOKIE
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
Page54
M.R.M.Rosman|http://rahimi.uitm.edu.my
Example
<?php
setcookie("ujian","MohamadRahimi",time()+3600)
?>
<html><head><title>UsingCookie</title></head>
<body>
<?phpecho"MyNameis".$_COOKIE["ujian"]
?>
</body>
</html>
FreePowerpointTemplates
Page55
M.R.M.Rosman|http://rahimi.uitm.edu.my
CHAPTEREIGHT
COOKIETUTORIAL
FreePowerpointTemplates
Page56
INSTRUCTION
M.R.M.Rosman|http://rahimi.uitm.edu.my
Createafoldernamelogin1and
fourPHPfiles:
index.php
login.php
main.php
logout.php
FreePowerpointTemplates
Page57
M.R.M.Rosman|http://rahimi.uitm.edu.my
index.php
FreePowerpointTemplates
Page58
M.R.M.Rosman|http://rahimi.uitm.edu.my
login.php
FreePowerpointTemplates
Page59
M.R.M.Rosman|http://rahimi.uitm.edu.my
main.php
FreePowerpointTemplates
Page60
M.R.M.Rosman|http://rahimi.uitm.edu.my
logout.php
FreePowerpointTemplates
Page61
LOGIN
M.R.M.Rosman|http://rahimi.uitm.edu.my
LOGOUT
FreePowerpointTemplates
Page62
UnsupportedBrowsers
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
Page63
M.R.M.Rosman|http://rahimi.uitm.edu.my
CHAPTEREIGHT
USINGSESSIONS
FreePowerpointTemplates
Page64
M.R.M.Rosman|http://rahimi.uitm.edu.my
PHPSessionVariables
Session variables allow you to store
data temporarily, just as cookies
does.
The variables or data will be deleted
whenyouleftyourbrowser.
Sessionscreateauniqueid(UID)for
each visitor and store variables
basedonthisUID.
The UID is either stored in a cookie
orispropagatedintheURL.
FreePowerpointTemplates
Page65
M.R.M.Rosman|http://rahimi.uitm.edu.my
StartingaPHPSession
Before you can store user
informationinyourPHPsession,
you must first start up the
session.
The session_start() function
must appear BEFORE the
<html>tag:
FreePowerpointTemplates
Page66
StoringaSessionVariable
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
Page67
DisplayingaSessionVariable
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
using
Page68
FreeingSessionVariable
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
Page69
DestroyingaSession
M.R.M.Rosman|http://rahimi.uitm.edu.my
session_destroy()willdestroyorreset
your session and all stored
informationwilllose.
FreePowerpointTemplates
Page70
M.R.M.Rosman|http://rahimi.uitm.edu.my
CHAPTEREIGHT
SESSIONTUTORIAL
FreePowerpointTemplates
Page71
INSTRUCTION
M.R.M.Rosman|http://rahimi.uitm.edu.my
Createafoldernamelogin2and
fourPHPfiles:
index.php
login.php
main.php
logout.php
FreePowerpointTemplates
Page72
M.R.M.Rosman|http://rahimi.uitm.edu.my
index.php
FreePowerpointTemplates
Page73
M.R.M.Rosman|http://rahimi.uitm.edu.my
login.php
FreePowerpointTemplates
Page74
M.R.M.Rosman|http://rahimi.uitm.edu.my
main.php
FreePowerpointTemplates
Page75
M.R.M.Rosman|http://rahimi.uitm.edu.my
logout.php
FreePowerpointTemplates
Page76
LOGIN
M.R.M.Rosman|http://rahimi.uitm.edu.my
LOGOUT
FreePowerpointTemplates
Page77
M.R.M.Rosman|http://rahimi.uitm.edu.my
CHAPTEREIGHT
MAILFUNCTION
FreePowerpointTemplates
Page78
ThePHPmail()Function
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
Page79
M.R.M.Rosman|http://rahimi.uitm.edu.my
Parameters
Syntax
Description
to
Required.Specifiesthereceiver/receiversoftheemail
Subjects
Required.Specifiesthesubjectoftheemail
message
Required.Specifiesmessagetobesent.
headers
Optional.SpecifiesheaderslikeFrom,Cc,andBcc
parameters
Optional.Specifiesadditionalparametertosendmail
program.
FreePowerpointTemplates
Page80
M.R.M.Rosman|http://rahimi.uitm.edu.my
PHPSimpleEMail
The simplest way to send an
emailwithPHPistosendatext
email.
Intheexamplebelowwedeclare
the variables ($to, $subject,
$message, $from, $headers),
thenweusethevariablesinthe
mail()functiontosendanemail:
FreePowerpointTemplates
Page81
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
Page82
Exercise
M.R.M.Rosman|http://rahimi.uitm.edu.my
FreePowerpointTemplates
Page83