PHP
Bertrand Estellon
Aix-Marseille Universit´e
Formulaires Introduction
GET
Protocole HTTP
Client
Serveu
r
1. Je veux toto.php 2. Sortie de l'exécution de toto.htmlRequˆ
ete :
GET /toto.php
?n1=10&n2=15
HTTP/1.0
Host : example.com
Referer : http ://example2.com/
User-Agent : Mozilla/5.0 (X11 ; U ; Linux
x86 64 ; fr ; rv :1.9.0.4) Gecko/2008111217
Fedora/3.0.4-1.fc10 Firefox/3.0.4
R´
eponse :
HTTP/1.0 200 OK
Date : Fri, 31 Dec 1999 23 :59 :59 GMT
Server : Apache/0.8.4
Content-Type : text/html
Content-Length : 59
Expires : Sat, 01 Jan 2000 00 :59 :59 GMT
Last-modified : Fri, 09 Aug 1996
14 :21 :40 GMT
<TITLE>Exemple</TITLE>
<P>R´
esultat :
25
.</P>
Formulaires Introduction
POST
Protocole HTTP
Client
Serveu
r
1. Je veux toto.php 2. Sortie de l'exécution de toto.htmlRequˆ
ete :
POST /toto.php HTTP/1.0
Host : example.com
Referer : http ://example2.com/
User-Agent : Mozilla/5.0 (X11 ; U ; Linux
x86 64 ; fr ; rv :1.9.0.4) Gecko/2008111217
Fedora/3.0.4-1.fc10 Firefox/3.0.4
n1=10&n2=15
R´
eponse :
HTTP/1.0 200 OK
Date : Fri, 31 Dec 1999 23 :59 :59 GMT
Server : Apache/0.8.4
Content-Type : text/html
Content-Length : 59
Expires : Sat, 01 Jan 2000 00 :59 :59 GMT
Last-modified : Fri, 09 Aug 1996
14 :21 :40 GMT
<TITLE>Exemple</TITLE>
<P>R´
esultat :
25
.</P>
Formulaires Introduction
Les formulaires en HTML
<input type="reset"... /> <input type="file"... /> <input type="checkbox".../> <label>...</label> <input type="radio".../> <input type="text".../> <textarea>...</textarea> <input type="submit"... />Formulaires Introduction
Les formulaires en HTML
<h t m l> <b o d y> <f o r m a c t i o n =”p a g e . php ” method =”p o s t ”> < f i e l d s e t> <l e g e n d>Q u i e t e s −v o u s ?</ l e g e n d> < l a b e l>Nom :</ l a b e l><i n p u t t y p e=” t e x t ” name=”nom ” v a l u e=” v o t r e nom ”/> < l a b e l>Prenom :</ l a b e l>
<i n p u t t y p e=” t e x t ” name=”prenom ” v a l u e=” v o t r e prenom ”/> <i n p u t t y p e=” r a d i o ” name=” s e x e ” v a l u e=”homme ”/>Homme <i n p u t t y p e=” r a d i o ” name=” s e x e ” v a l u e=”femme ”/>Femme < l a b e l>Ph o to :</ l a b e l>
<i n p u t t y p e=” f i l e ” name=”p h o t o ” a c c e p t=”i m a g e / j p e g ” /> </ f i e l d s e t>
< f i e l d s e t>
<l e g e n d>V o t r e c o m m e n t a i r e</ l e g e n d>
< t e x t a r e a name=”c o m m e n t a i r e ”>v o t r e c o m m e n t a i r e</ t e x t a r e a> </ f i e l d s e t>
<c e n t e r>
<i n p u t t y p e=”c h e c k b o x ” name=” v a l i d ” v a l u e=” v a l i d ”/>J a c c e p t e . . . <i n p u t t y p e=” r e s e t ” v a l u e=” E f f a c e r ”>
<i n p u t t y p e=”s u b m i t ” v a l u e=”E n v o y e r ”> </ c e n t e r>
</ f o r m> </ b o d y> </ h t m l>
I
Envoi des donn´
ees au serveur par la m´
ethode POST sur la page
page.php lorsque l’utilisateur clique sur le bouton Envoyer.
Formulaires Variables $ POST et $ GET
Variables “super-globales”
I
Les variables super-globales sont accessibles dans tous les contextes
Fichier test.php
:
<?
f u n c t i o n
t o t o ( ) {
e c h o $ SERVER [ ”PHP SELF ” ] . ”\ n ” ;
1
}
t o t o ( ) ;
e c h o $ SERVER [ ”PHP SELF ” ] . ”\ n ” ;
1
?>
1
”test.php”
Formulaires Variables $ POST et $ GET
Variable $ POST
IFichier index.html
<h t m l>
<b o d y>
<f o r m method=
” p o s t ”
a c t i o n=
” t r a i t e m e n t . php ”
>
< l a b e l>Nom : </ l a b e l>
<i n p u t
t y p e=” t e x t ” name=”nom ”/>
<i n p u t
t y p e=”s u b m i t ” v a l u e=”E n v o y e r ”/>
</ f o r m>
</ b o d y>
</ h t m l>
IFichier
traitement.php
<h t m l>
<b o d y>
B o n j o u r <? e c h o
$ POST [ ’ nom ’ ]
;
?> .</ b r>
</ b o d y>
</ h t m l>
Requête : Réponse : POST traitement.php ... nom=Superman ... Bonjour Superman. ...Formulaires Variables $ POST et $ GET
Variable $ POST
GET index.php contenu de index.php POST traitement.php ... nom=Superman Bonjour Superman Client Serveur Saisie du nom Clic sur "Envoyer"Génération de la page par PHP
Formulaires Variables $ POST et $ GET
Variable $ GET
IFichier index.html
<h t m l>
<b o d y>
<f o r m method=
”g e t ”
a c t i o n=
” t r a i t e m e n t . php ”
>
< l a b e l>Nom : </ l a b e l>
<i n p u t
t y p e=” t e x t ” name=”nom ”/>
<i n p u t
t y p e=”s u b m i t ” v a l u e=”E n v o y e r ”/>
</ f o r m>
</ b o d y>
</ h t m l>
IFichier
traitement.php
<h t m l>
<b o d y>
B o n j o u r <? e c h o
$ GET [ ’ nom ’ ]
;
?> .</ b r>
</ b o d y>
</ h t m l>
Requête : Réponse : GET traitement.php?nom=superman ... ... Bonjour Superman. ...Formulaires Variables $ POST et $ GET
Variable $ GET
GET index.php contenu de index.php GET traitement.php ?nom=Superman ... Bonjour Superman Client Serveur Saisie du nom Clic sur "Envoyer"Génération de la page par PHP
Formulaires Applications
Bouton radio
IFichier index.html :
<h t m l> <b o d y> <f o r m method=” p o s t ” a c t i o n=” t r a i t e m e n t . php ”><i n p u t t y p e=” r a d i o ” name=” s e x e ” v a l u e=”homme ”>Homme</ b r> <i n p u t t y p e=” r a d i o ” name=” s e x e ” v a l u e=”femme ”>Femme</ b r> <i n p u t t y p e=”s u b m i t ” v a l u e=”E n v o y e r ”/>
</ f o r m> </ b o d y> </ h t m l> I
Fichier
traitement.php
:
<h t m l> <b o d y> <? i f ($ POST [ ’ s e x e ’]=== ’homme ’) { ?> B o n j o u r , v o u s e t e s un homme . <?} e l s e {?> B o n j o u r , v o u s e t e s une femme . <? } ?> </ b o d y> </ h t m l>Formulaires Applications
Avec un seul fichier
<h t m l> <b o d y>
<? i f (i s s e t ( $ POST [ ’ nom ’ ] )) {?> B o n j o u r <? e c h o $ POST [ ’ nom ’ ]; ?> <?} e l s e {?>
<f o r m method=” p o s t ” a c t i o n=”<? e c h o $ SERVER [ ”PHP SELF ”] ; ?>”> < l a b e l>Nom : </ l a b e l>
<i n p u t t y p e=” t e x t ” name=”nom ”/>
<i n p u t t y p e=”s u b m i t ” v a l u e=”E n v o y e r ”/> </ f o r m>
<? } ?> </ b o d y> </ h t m l>
Formulaires Applications
Valeurs multiples et checkboxes
I
Fichier index.html :
<h t m l> <b o d y>
<f o r m method=” p o s t ” a c t i o n=” t r a i t e m e n t . php ”>
<i n p u t t y p e=”c h e c k b o x ” name=” c h o i x [ ] ” v a l u e=”A ”>A</ b r> <i n p u t t y p e=”c h e c k b o x ” name=” c h o i x [ ] ” v a l u e=”B ”>B</ b r> <i n p u t t y p e=”c h e c k b o x ” name=” c h o i x [ ] ” v a l u e=”C ”>C</ b r> <i n p u t t y p e=”s u b m i t ” v a l u e=”E n v o y e r ”/>
</ f o r m> </ b o d y> </ h t m l> I
Fichier
traitement.php
:
<h t m l> <b o d y> Vous a v e z c o c h e : <? f o r e a c h ($ POST [ ’ c h o i x ’ ] a s $ v ) e c h o ”$ v ” ; ?> <b r /> </ b o d y> </ h t m l>Formulaires Applications
Maintient de l’´
etat du formulaire
<h t m l> <b o d y>
<f o r m method=” p o s t ” a c t i o n=”<? e c h o $ SERVER [ ”PHP SELF ” ] ; ?> ”> <i n p u t t y p e=” t e x t ” name=”a ” />
<i n p u t t y p e=” t e x t ” name=”b ” />
<i n p u t t y p e=”s u b m i t ” v a l u e=”E n v o y e r ”/> </ f o r m>
<? i f (i s s e t ( $ POST [ ’ a ’ ] ) && i s s e t ( $ POST [ ’ b ’ ] )) {?> R e s u l t a t : <? e c h o $ POST [ ’ a ’ ] + $ POST [ ’ b ’ ]; ?> <? } ?>
</ b o d y> </ h t m l>
Formulaires Applications
Maintient de l’´
etat du formulaire
<h t m l> <b o d y>
<f o r m method=” p o s t ” a c t i o n=”<? e c h o $ SERVER [ ”PHP SELF ” ] ; ?> ”> <i n p u t t y p e=” t e x t ” name=”a ” v a l u e=”<? e c h o $ POST [ ’ a ’ ] ; ? > ” /> <i n p u t t y p e=” t e x t ” name=”b ” v a l u e=”<? e c h o $ POST [ ’ b ’ ] ; ? > ” /> <i n p u t t y p e=”s u b m i t ” v a l u e=”E n v o y e r ”/>
</ f o r m>
<? i f ( i s s e t ( $ POST [ ’ a ’ ] ) && i s s e t ( $ POST [ ’ b ’ ] ) ) {?> R e s u l t a t : <? e c h o $ POST [ ’ a ’ ]+$ POST [ ’ b ’ ] ; ?> <? } ?>
</ b o d y> </ h t m l>
Formulaires Transfert de fichiers
Transfert de fichiers
<h t m l> <b o d y> <f o r m e n c t y p e =”m u l t i p a r t / form−d a t a ” method =”p o s t ” a c t i o n=”<? e c h o $ SERVER [ ”PHP SELF ” ] ; ?> ”> <i n p u t t y p e =” f i l e ” name=” f i c h i e r ” /><i n p u t t y p e=”s u b m i t ” v a l u e=”E n v o y e r ” /> </ f o r m> <? v a r d u m p ($ F I L E S) ; 1 ?> </ b o d y> </ h t m l>
1
array(1) {
[”fichier”]=> array(5) {
[”name”]]=> string(3) ”a.c”
[”type”]=> string(10) ”text/plain”
[”tmp name”]=> string(14) ”/tmp/phpi5JOt8”
[”error”]=> int(0)
[”size”]=> int(185)
}
Formulaires Transfert de fichiers
Transfert de fichiers
<h t m l> <b o d y>
<f o r m e n c t y p e=” m u l t i p a r t / form−d a t a ” method=” p o s t ” a c t i o n=”<? e c h o $ SERVER [ ”PHP SELF ” ] ; ?> ”> <i n p u t t y p e=” f i l e ” name=” f i c h i e r ” />
<i n p u t t y p e=”s u b m i t ” v a l u e=”E n v o y e r ” /> </ f o r m> <? i f (i s s e t ( $ F I L E S [ ’ f i c h i e r ’ ] )) { $tmpname = $ F I L E S [ ’ f i c h i e r ’ ] [ ’ tmp name ’ ]; $newname = ” f i c h i e r ”. $ F I L E S [ ’ f i c h i e r ’ ] [ ’ name ’ ]; e c h o $tmpname . ” ” . $newname . ”<b r /> ” ; $ r e s u l t = m o v e u p l o a d e d f i l e ( $tmpname , $newname ); i f ($ r e s u l t==TRUE) e c h o ” t r a n s f e r t ok !< b r /> ” ; e l s e e c h o ” e r r e u r : ” .$ F I L E S [ ’ f i c h i e r ’ ] [ ’ e r r o r ’ ]; } ?> </ b o d y> </ h t m l>
Formulaires Transfert de fichiers
Transfert de fichiers
Les erreurs possibles :
I
UPLOAD_ERR_OK (valeur 0 ) :
,→ pas d’erreur
I
UPLOAD_ERR_INI_SIZE (valeur 1 ) :
,→ la taille du fichier d´
epasse la valeur pr´
esente dans le fichier php.ini.
I
UPLOAD_ERR_FORM_SIZE (valeur 2 ) :
,→ la taille du fichier d´
epasse celle fix´
ee dans le formulaire
(champ cach´
e MAX_FILE_SIZE).
I
UPLOAD_ERR_PARTIAL (valeur 3 ) :
,→ le fichier a ´
et´
e partiellement t´
el´
echarg´
e.
I