TDM06 de Technologies Web: PHP
ASI4 - INSA Rouen
correction
1 Compteur PHP (1)
L’objectif de l’exercice est de développer le code nécessaire à la création d’un compteur sur une page web.
Ce compteur s’incrémentera à chaque chargement de la page (i.e. depuis plusieurs postes différents).
Correction
compteur.inc.php
<?php
f u n c t i o n c o m p t e u r($ n o m d u f i c h i e r) { if(f i l e _ e x i s t s($ n o m d u f i c h i e r) ) {
$ f i c h i e r = f o p e n($ n o m d u f i c h i e r,’ r + ’) ; f l o c k($ f i c h i e r, L O C K _ E X) ;
$ c o m p t e u r = f g e t s($ f i c h i e r, 1 0 0 ) ; if (e m p t y($ c o m p t e u r) )
$ c o m p t e u r = 0;
e l s e
s e t t y p e($ c o m p t e u r, " i n t e g e r ") ;
$ c o m p t e u r++;
} e l s e {
$ f i c h i e r = f o p e n($ n o m d u f i c h i e r,’ c ’) ; f l o c k($ f i c h i e r, L O C K _ E X) ;
$ c o m p t e u r = 1;
}
f s e e k($ f i c h i e r, 0) ;
f p u t s($ f i c h i e r, $ c o m p t e u r) ; f l o c k($ f i c h i e r, L O C K _ U N) ; f c l o s e($ f i c h i e r) ;
r e t u r n $ c o m p t e u r; }
? >
pagecompteurs.php
<?php r e q u i r e(’ c o m p t e u r . inc . php ’) ; ? >
<!D O C T Y P E html>
<html>
<head>
<m e t a http-e q u i v=" content - t y p e " c o n t e n t=" t e x t / h t m l ; c h a r s e t = utf -8 " / >
<title>C o m p t e u r</title>
</head>
<body>
<p>C o m p t e u r de h i t s : <?php e c h o c o m p t e u r(’ / tmp / c o m p t e u r . txt ’) ; ? > </p>
</body>
</html>
2 Forum ASI (4) : PHP, 1ère version
L’objectif de l’exercice est de développer le code nécessaire à l’affichage de messages en mode anonyme, dans le forum ASI.
Vous développerez une page affichant :
— un formulaire identique à celui réalisé au cours du TDM sur Javascript,
— la liste des messages précédemment déposés.
Les différents messages seront sauvegardés dans un fichier côté serveur, dans le répertoire /tmp.
Le script devra afficher en dessous du formulaire un tableau qui contiendra tous les messages formatés comme suit : sur la première ligne, la date où le message a été enregistré suivi du nom avec l’email entre parenthèse, et sur la deuxième ligne le message enregistré.
Pensez également à mettre à jour la CSS pour un affichage plus propre de la liste de messages.
Correction
livredor.inc.php :
<?php
f u n c t i o n l i r e M e s s a g e s($ n o m f i c h i e r) : a r r a y {
$ f i c h i e r = f o p e n($ n o m f i c h i e r, " r ") ;
$ m e s s a g e s = a r r a y() ;
w h i l e($ m e s s a g e c o d e = f g e t s($ f i c h i e r) )
$ m e s s a g e s[] = d e c o d e M e s s a g e($ m e s s a g e c o d e) ; r e t u r n $ m e s s a g e s;
}
f u n c t i o n e c r i r e M e s s a g e($ n o m f i c h i e r, $ m e s s a g e) {
$ f i c h i e r = f o p e n($ n o m f i c h i e r," a ") ; f l o c k($ f i c h i e r, L O C K _ E X) ;
f p u t s($ f i c h i e r, e n c o d e M e s s a g e($ m e s s a g e) ." \ n ") ; f l o c k($ f i c h i e r, L O C K _ U N) ;
f c l o s e($ f i c h i e r) ; }
f u n c t i o n c o n s t r u i r e M e s s a g e($auteur, $ t e x t e) : a r r a y {
r e t u r n a r r a y(s t r f t i m e(" % A % d % B % Y % T ",(int)t i m e() ) , $auteur, $ t e x t e) ; }
f u n c t i o n e n c o d e M e s s a g e($ m e s s a g e) :s t r i n g {
r e t u r n s t r _ r e p l a c e(" | ", " { pp } ", s t r _ r e p l a c e(" \ r ", " { cr } ", s t r _ r e p l a c e(" \ n ", " { nl } ",
$ m e s s a g e[ 0 ] ) ) )
." | ".s t r _ r e p l a c e(" | ", " { pp } ", s t r _ r e p l a c e(" \ r ", " { cr } ", s t r _ r e p l a c e(" \ n ", " { nl } ",
$ m e s s a g e[ 1 ] ) ) )
." | ".s t r _ r e p l a c e(" | ", " { pp } ", s t r _ r e p l a c e(" \ r ", " { cr } ", s t r _ r e p l a c e(" \ n ", " { nl } ",
$ m e s s a g e[ 2 ] ) ) ) ; }
f u n c t i o n d e c o d e M e s s a g e($ m e s s a g e c o d e) : a r r a y {
l i s t($date, $auteur, $ t e x t e) = e x p l o d e(" | ",$ m e s s a g e c o d e) ;
r e t u r n a r r a y(s t r _ r e p l a c e(" { nl } ", " \ n ", s t r _ r e p l a c e(" { cr } ", " \ r ", s t r _ r e p l a c e(" { pp } ", " | "
, $ d a t e) ) ) ,
s t r _ r e p l a c e(" { nl } ", " \ n ", s t r _ r e p l a c e(" { cr } ", " \ r ", s t r _ r e p l a c e(" { pp } ", " | ", $ a u t e u r) ) ) , s t r _ r e p l a c e(" { nl } ", " \ n ", s t r _ r e p l a c e(" { cr } ", " \ r ", s t r _ r e p l a c e(" { pp } ", " | ", $ t e x t e) ) ) ) ;
}
? >
livredor.php :
<?php
h e a d e r(’ Content - T y p e : t e x t / h t m l ; c h a r s e t = UTF -8 ’) ; s e t l o c a l e(LC_TIME, " f r _ F R . u t f 8 ") ;
r e q u i r e(’ f o r u m . inc . php ’) ;
? >
<!D O C T Y P E html>
<html>
<head>
<title>F o r u m ASI</title>
<m e t a http-e q u i v=" Content - T y p e " c o n t e n t=" t e x t / h t m l ; c h a r s e t = utf -8 ">
<l i n k h r e f=" s t y l e . css " rel=" s t y l e s h e e t " t y p e=" t e x t / css " / >
<l i n k h r e f=" R e s s o u r c e s / s t y l e . css " rel=" s t y l e s h e e t " t y p e=" t e x t / css " / >
</head>
<body>
<h e a d e r id=" top ">
<img id=" l o g o " src=" I m a g e s / logo - asi . png " alt=" ASI " w i d t h=" 125 " h e i g h t=" 58 " / >
<h1>F o r u m ASI</h1>
</header>
<p id=" p r o b l e m e "> </p>
<f o r m n a m e=" f o r m u l a i r e " a c t i o n=" f o r u m . php " m e t h o d=" p o s t ">
<f i e l d s e t>
<legend>C o o r d o n n é e s : </legend>
<l a b e l for=" n a m e ">Nom : </label> <i n p u t p l a c e h o l d e r=" S a i s i s s e z v o t r e nom " id=" n a m e "
n a m e=" n a m e " t y p e=" t e x t " s i z e=" 30 " / >
<l a b e l for=" e m a i l ">E-m a i l : </label> <i n p u t p l a c e h o l d e r=" S a i s i s s e z v o t r e e m a i l " id="
e m a i l " n a m e=" e m a i l " t y p e=" t e x t " s i z e=" 30 " / >
</f i e l d s e t>
<f i e l d s e t>
<legend>M e s s a g e : </legend>
<t e x t a r e a r o w s=" 4 " c o l s=" 50 " id=" m e s s a g e " n a m e=" m e s s a g e "> </t e x t a r e a>
</f i e l d s e t>
<i n p u t t y p e=" b u t t o n " v a l u e=" P o s t e r le m e s s a g e " o n c l i c k=" J a v a s c r i p t : v e r i f i c a t i o n () ; " / >
<i n p u t t y p e=" b u t t o n " v a l u e=" E f f a c e r " o n c l i c k=" J a v a s c r i p t : n e t t o y a g e () ; " / >
</form>
<?php // e n r e g i s t r e m e n t du m e s s a g e si s u b m i t if (i s s e t($ _ P O S T) && !e m p t y($ _ P O S T[’ n a m e ’]) ) {
$ m e s s a g e = c o n s t r u i r e M e s s a g e($ _ P O S T[’ n a m e ’]." ( ".$ _ P O S T[’ e m a i l ’]." ) ", $ _ P O S T[’ m e s s a g e ’])
;
e c r i r e M e s s a g e(’ / tmp / l i v r e d o r . dat ’,$ m e s s a g e) ; }
? >
<h1>L i s t e des m e s s a g e s postés</h1>
<?php // a f f i c h a g e du l i v r e d ’ or
f o r e a c h(l i r e M e s s a g e s(’ / tmp / l i v r e d o r . dat ’) as $ m e s s a g e) :
? >
<table>
<tr>
<td c l a s s=" d a t e "> <?php e c h o h t m l e n t i t i e s($ m e s s a g e[ 0 ] ) ? > </td>
<td c l a s s=" a u t e u r "> <?php e c h o h t m l e n t i t i e s($ m e s s a g e[ 1 ] ) ? > </td>
</tr>
<tr>
<td c o l s p a n=" 2 "> <pre> <?php e c h o h t m l e n t i t i e s($ m e s s a g e[ 2 ] ) ? > </pre> </td>
</tr>
</table>
<?php
e n d f o r e a c h;
? >
<footer>
<s t y l e t y p e=" t e x t / css " s c o p e d >
o b j e c t { o v e r f l o w: a u t o; }
</style>
<s t y l e t y p e=" t e x t / css " s c o p e d >
p# p r o b l e m e { c o l o r : Red ; }
</style>
<s c r i p t t y p e=" t e x t / j a v a s c r i p t ">
f u n c t i o n v e r i f i c a t i o n() {
var e m a i l B o x = d o c u m e n t.g e t E l e m e n t B y I d(" e m a i l ") ; var p r o b l e m e = d o c u m e n t.g e t E l e m e n t B y I d(" p r o b l e m e ") ; if(e m a i l B o x.v a l u e==" ") {
if (!p r o b l e m e.h a s C h i l d N o d e s() ) { e m a i l B o x.s t y l e.o u t l i n e=" s o l i d Red ";
p r o b l e m e.a p p e n d C h i l d(d o c u m e n t.c r e a t e T e x t N o d e(" C h a m p o b l i g a t o i r e . ") ) ; }
} e l s e {
d o c u m e n t.f o r m s[’ f o r m u l a i r e ’].s u b m i t() ; }
};
f u n c t i o n n e t t o y a g e() {
if(c o n f i r m(’ E t e s v o u s sur ? ’) ) {
d o c u m e n t.g e t E l e m e n t B y I d(" e m a i l ") .s t y l e.o u t l i n e=" i n i t i a l ";
var p r o b l e m e = d o c u m e n t.g e t E l e m e n t B y I d(" p r o b l e m e ") ; if (p r o b l e m e.h a s C h i l d N o d e s() ) {
p r o b l e m e.r e m o v e C h i l d(p r o b l e m e.c h i l d N o d e s.i t e m(0) ) ; }
d o c u m e n t.f o r m s[’ f o r m u l a i r e ’].r e s e t() ; }
};
</script>
</footer>
</body>
</html>