PHP
Bertrand Estellon
Aix-Marseille Université
13 mars 2012
1 / 214
Protocole HTTP
1. Je veux
Client Serveur
2. Sortie de l'exécution de
Requête :
GET ?n1=10&n2=15 HTTP/1.0
Host :
Referer : http 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éponse :
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ésultat : 25.</P>
POST Protocole HTTP 1. Je veux Client Serveur 2. Sortie de l'exécution de |
Requête :
POST HTTP/1.0
Host :
Referer : http 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éponse :
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ésultat : 25.</P>
<input type="reset"... /> <input type="submit"... />
<html>
<body>
<form action =”page . php ” method=”post ”>
<f i e l d s e t>
<legend>Qui etes?vous ?</ legend>
<l a b e l>Nom :</ l a b e l>
<input type=”text ” name=”nom” value=”votre nom”/>
<l a b e l>Prenom :</ l a b e l>
<input type=”text ” name=”prenom ” value=”votre prenom ”/>
<input type=”radio ” name=”sexe ” value=”homme”/>Homme <input type=”radio ” name=”sexe ” value=”femme ”/>Femme
<l a b e l>Photo :</ l a b e l>
<input type=” f i l e ” name=”photo ” accept=”image/ jpeg ” />
</ f i e l d s e t>
<f i e l d s e t>
<legend>Votre commentaire</ legend>
<textarea name=”commentaire ”>votre commentaire</ textarea>
</ f i e l d s e t>
<center>
<input type=”checkbox ” name=”v a l i d ” value=”v a l i d ”/>J accepte . . .
<input type=”r e s e t ” value=”Effacer ”>
<input type=”submit ” value=”Envoyer ”>
</ center>
</form>
</body>
</html>
I Envoi des données au serveur par la méthode POST sur la page lorsque l’utilisateur clique sur le bouton Envoyer.
I Les variables super-globales sont accessibles dans tous les contextes
Fichier :
<? function toto () {
echo $ SERVER [ ”PHP SELF”] . ”\n ”; 1
} toto ( ) ;
echo $ SERVER [ ”PHP SELF”] . ”\n ”; 1
?>
1 ””
I Fichier
<html>
<body>
<form method=”post ” action=”traitement . php ”>
<l a b e l>Nom : </ l a b e l>
<input type=”text ” name=”nom”/>
<input type=”submit ” value=”Envoyer ”/>
</form>
</body>
</html>
I Fichier
<html>
<body>
Bonjour <? echo $ POST [ ’nom ’] ; ?>.</ br>
</body>
</html>
Saisie du nom
Clic sur "Envoyer"
Génération de la page par PHP
I Fichier
<html>
<body>
<form method=”get ” action=”traitement . php ”>
<l a b e l>Nom : </ l a b e l>
<input type=”text ” name=”nom”/>
<input type=”submit ” value=”Envoyer ”/>
</form>
</body>
</html>
I Fichier
<html>
<body>
Bonjour <? echo $ GET [ ’nom ’] ; ?>.</ br>
</body>
</html>
Saisie du nom
Clic sur "Envoyer"
Génération de la page par PHP
I Fichier :
<html>
<body>
<form method=”post ” action=”traitement . php ”>
<input type=”radio ” name=”sexe ” value=”homme”>Homme</ br>
<input type=”radio ” name=”sexe ” value=”femme ”>Femme</ br>
<input type=”submit ” value=”Envoyer ”/>
</form>
</body>
</html>
I Fichier :
<html>
<body>
<? i f ($ POST [ ’ sexe ’]===’homme ’ ) { ?> Bonjour , vous etes un homme.
<?} e l s e {?>
Bonjour , vous etes une femme .
<? } ?>
</body>
</html>
<html>
<body>
<? i f ( i s s e t ($ POST [ ’nom ’ ] ) ) {?>
Bonjour <? echo $ POST [ ’nom ’] ; ?>
<?} e l s e {?>
<form method=”post ” action=”<? echo $ SERVER [ ”PHP SELF ”] ; ?>” >
<l a b e l>Nom : </ l a b e l>
<input type=”text ” name=”nom”/>
<input type=”submit ” value=”Envoyer ”/>
</form>
<?}?>
</body>
</html>
Bonjour Superman
I Fichier :
<html>
<body>
<form method=”post ” action=”traitement . php ”>
<input type=”checkbox ” name=”choix [ ] ” value=”A”>A</ br>
<input type=”checkbox ” name=”choix [ ] ” value=”B”>B</ br>
<input type=”checkbox ” name=”choix [ ] ” value=”C”>C</ br>
<input type=”submit ” value=”Envoyer ”/>
</form>
</body>
</html>
I Fichier :Vous avez coché : A C
<html>
<body>
Vous avez coche :
<? foreach ($ POST [ ’ choix ’] as $v ) echo ”$v ”; ?>
<br />
</body>
</html>
<html>
<body>
<form method=”post ” action=”<? echo $ SERVER [ ”PHP SELF”] ; ?>”>
<input type=”text ” name=”a ” />
<input type=”text ” name=”b ” />
<input type=”submit ” value=”Envoyer ”/>
</form>
<? i f ( i s s e t ($ POST [ ’ a ’ ] ) && i s s e t ($ POST [ ’ b ’ ] ) ) {?>
Resultat : <? echo $ POST [ ’ a ’]+$ POST [ ’ b ’] ; ?> <?}?>
</body>
</html>
<html>
<body>
<form method=”post ” action=”<? echo $ SERVER [ ”PHP SELF”] ;?>”>
<input type=”text ” name=”a ” value=”<? echo $ POST [ ’ a ’];? >” /> <input type=”text ” name=”b ” value=”<? echo $ POST [ ’ b ’];? >” />
<input type=”submit ” value=”Envoyer ”/>
</form>
<? i f ( i s s e t ($ POST [ ’ a ’ ] ) && i s s e t ($ POST [ ’b ’ ] ) ) {?>
Resultat : <? echo $ POST [ ’ a ’]+$ POST [ ’b ’ ] ; ?> <?}?>
</body>
</html>
<html>
<body>
<form enctype=”multipart /form?data ” method=”post ”
? array(1) {
??????????? [”fichier”[[”name””type”]=]=]]=>>>array(5)string(10) ”text/plain”string(3) ”a.c”{
1 [”tmp name”]=> string(14) ”/tmp/phpi5JOt8”
[”error”]=> int(0)
???????? } } [”size”]=> int(185)
<html>
<body>
<form enctype=”multipart /form?data ” method=”post ” action=”<? echo $ SERVER [ ”PHP SELF”] ; ?>”>
<input type=” f i l e ” name=” f i c h i e r ” />
<input type=”submit ” value=”Envoyer ” />
</form>
<? i f ( i s s e t ( $ FILES [ ’ f i c h i e r ’ ] ) ) {
$tmpname = $ FILES [ ’ f i c h i e r ’ ] [ ’ tmp name ’] ; $newname = ” f i c h i e r ”. $ FILES [ ’ f i c h i e r ’ ] [ ’ name ’] ; echo $tmpname . ” ”. $newname . ”<br/>”;
$ r e s u l t = move uploaded file ($tmpname , $newname) ; i f ( $ r e s u l t==TRUE) echo ”t r a n s f e r t ok !<br/>”;
e l s e echo ”e r r e u r : ”. $ FILES [ ’ f i c h i e r ’ ] [ ’ e r r o r ’] ;
}
?>
</body>
</html>
Les erreurs possibles :
I UPLOAD_ERR_OK (valeur 0) :
,? pas d’erreur
I UPLOAD_ERR_INI_SIZE (valeur 1) :
,? la taille du fichier dépasse la valeur présente dans le fichier .
I UPLOAD_ERR_FORM_SIZE (valeur 2) :
,? la taille du fichier dépasse celle fixée dans le formulaire (champ caché MAX_FILE_SIZE).
I UPLOAD_ERR_PARTIAL (valeur 3) :
,? le fichier a été partiellement téléchargé.
I UPLOAD_ERR_NO_FILE (valeur 4) :
,? aucun fichier n’a été téléchargé.