TDM07 - Plateforme e-learning Moodle de l`INSA de Rouen
Transcription
TDM07 - Plateforme e-learning Moodle de l`INSA de Rouen
TDM07 de Technologies Web: PHP2 ASI4 - INSA Rouen correction 1 Compteur PHP (2) Reprenez le compteur PHP développé la semaine dernière et modifiez-le afin de lui ajouter un second compteur de visites qui s’incrémente uniquement pour chaque première visite au cours d’une même session pour un utilisateur. Aucune saisie de login/mdp n’est nécessaire, il s’agit uniquement de s’appuyer sur les sessions. Correction compteur.inc <? php function c o m p t e u r ( $ n o m d u f i c h i e r , $ f l a g i n c r e m e n t = TRUE) { $ 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 + ’) ; if ( $flagincrement ) f l o c k ( $ f i c h i e r , LOCK_EX) ; $compteur = f g e t s ( $ f i c h i e r , 1 0 0 ) ; i f ( empty ( $ c o m p t e u r ) ) $ c o m p t e u r =0; else s e t t y p e ( $compteur , " i n t e g e r " ) ; if ( $flagincrement ) { $ c o m p t e u r ++; 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 , $compteur ) ; f l o c k ( $ f i c h i e r , LOCK_UN) ; } } ?> fclose ( $fichier ) ; return $ c o m p t e u r ; pagecompteurs.php <? php s e s s i o n _ s t a r t ( ) ; r e q u i r e ( ’ c o m p t e u r . i n c . php ’ ) ; ?> <html> <head> < t i t l e>Compteurs</ t i t l e> <meta h t t p −e q u i v=" c o n t e n t −t y p e " content=" t e x t / h t m l ; c h a r s e t=u t f −8" /> </head> <body> <p>Compteur de h i t s : <? php echo c o m p t e u r ( ’ c o m p t e u r . t x t ’ ) ; ?></p> <p>Compteur de v i s i t e s : <? php echo c o m p t e u r ( ’ c o m p t e u r v i s i t e . t x t ’ , ! i s s e t ( $_SESSION [ ’ d e j a p a s s e ’ ] ) ) ; ?></p> <? php i f ( ! i s s e t ( $_SESSION [ ’ d e j a p a s s e ’ ] ) ) $_SESSION [ ’ d e j a p a s s e ’ ] =TRUE; ?> <p><a h r e f=" l o g o u t . php "> R é i n i t i a l i s a t i o n de l a c o n n e x i o n .</a></p> </body> </html> logout.php <? php session_start () ; session_destroy () ; h e a d e r ( " L o c a t i o n : . / p a g e c o m p t e u r s . php " ) ; ?> 2 Serveur bibliographique (2) : PHP et SimpleXML Reprenez les fichiers développés au cours du TDM sur XML et créez une page PHP affichant, à l’aide de SimpleXML, le résultat de la recherche d’une publication saisie à partir d’un champs texte (par exemple une recherche sur l’année). Seul le premier résultat sera affiché. NB : l’affichage étant fait ici par lecture du contenu d’un fichier XML à l’aide de SimpleXML, pour appliquer la CSS il est nécessaire de réinsérer des balises HTML pour lesquelles il existe un traitement CSS. Correction serveurbiblio.php : < !DOCTYPE html> <? php s e t l o c a l e (LC_ALL, " fr_FR " ) ; ?> <html> <head> < t i t l e> B i b l i o</ t i t l e> <meta h t t p −e q u i v=" c o n t e n t −t y p e " content=" t e x t / h t m l ; c h a r s e t=u t f −8" /> <l i n k h r e f=" d a t a / s t y l e B i b l i o g r a p h i e . c s s " r e l=" s t y l e s h e e t " type=" t e x t / c s s " /> </head> <body> <form a c t i o n=" s e r v e u r b i b l i o . php " method=" p o s t "> <p><l a b e l>Annee de l a p u b l i c a t i o n : <input type=" t e x t " name=" annee " /></ l a b e l></p> <p><input type=" s u b m i t " value=" A f f i c h e r " /></p> </form> <? php $ p u b l i c a t i o n s = s i m p l e x m l _ l o a d _ f i l e ( " d a t a / l i s t e −p u b l i c a t i o n s . xml " ) ; f o r e a c h ( $ p u b l i c a t i o n s −> p u b l i c a t i o n a s $ p u b l i c a t i o n ) { i f ($_POST [ ’ annee ’]== $ p u b l i c a t i o n −>annee ) { e c h o "<p>P u b l i c a t i o n : " ; // Les a u t e u r s f o r e a c h ( $ p u b l i c a t i o n −>l i s t e a u t e u r s −>a u t e u r a s $ a u t e u r ) { e c h o "<a u t e u r >" . $ a u t e u r . "</ a u t e u r >" ; } // Le t i t r e e c h o "< t i t r e >" . $ p u b l i c a t i o n −> t i t r e . "</ t i t r e >" ; // Le t y p e $ t y p e = $ p u b l i c a t i o n −> a t t r i b u t e s ( ) ; i f ( $ t y p e ==’ a r t i c l e ’ ) { e c h o " J o u r n a l : <j o u r n a l >" . $ p u b l i c a t i o n −>j o u r n a l . "</ j o u r n a l >" ; } e l s e i f ( $ t y p e ==’ j o u r n a l ’ ) { e c h o " C o n f é r e n c e : <c o n f e r e n c e >" . $ p u b l i c a t i o n −>c o n f e r e n c e . "</ c o n f e r e n c e >" ; } // Pages e t année e c h o "<p a g e s >" . $ p u b l i c a t i o n −>p a g e s . "</p a g e s ><annee>" . $ p u b l i c a t i o n −>annee . " </annee ></p>" ; } } ?> </body> </html> 3 Forum ASI (5) : PHP, 2nde version Reprenez la page permettant le dépôt de messages en mode non connecté réalisée lors du TDM précédent et modifiez-la afin de gérer la persistance des messages à l’aide de SQLite à la place d’un fichier simple. Vous encapsulerez maintenant la gestion des messages dans une classe. Correction createDB.php : <html> <head> <meta h t t p −e q u i v=" c o n t e n t −t y p e " content=" t e x t / h t m l ; c h a r s e t=u t f −8" /> < t i t l e>C r é a t i o n de l a BD p o u r l e Forum ASI</ t i t l e> </head> <body> <? php e r r o r _ r e p o r t i n g (E_ALL) ; try { $ r e q u e t e = "CREATE TABLE IF NOT EXISTS m e s s a g e s ( i d INTEGER PRIMARY KEY, d a t e INTEGER NOT NULL, e m a i l TEXT NOT NULL, t e x t e TEXT NOT NULL )"; /∗ c r e a t i o n de l a BD ∗/ $ d b = new PDO( " s q l i t e : . / DATABASE/ m e s s a g e s . s q l i t e " ) ; /∗ e r r o r s −> e x c e p t i o n s ∗/ $db−> s e t A t t r i b u t e (PDO : : ATTR_ERRMODE, PDO : : ERRMODE_EXCEPTION) ; /∗ r e q u e t e de c r e a t i o n ∗/ $db−>q u e r y ( $ r e q u e t e ) ; u n s e t ( $db ) ; echo ’ Table created ’ ; } c a t c h ( PDOException $e ) { e c h o $e−>g e t M e s s a g e ( ) ; } ?> </body> </html> forum.inc.php : <? php c l a s s Message { p r i v a t e $timestamp ; p r i v a t e $email ; private $texte ; p u b l i c function _ _ c o n s t r u c t ( $ e m a i l , $ t e x t e , $ d a t e = n u l l ) { i f ( $ d a t e==n u l l ) $ t h i s −>t i m e s t a m p = t i m e ( ) ; else $ t h i s −>t i m e s t a m p = $ d a t e ; $ t h i s −>e m a i l = h t m l e n t i t i e s ( $email ) ; $ t h i s −>t e x t e = htmlentities ( $texte ) ; } p u b l i c f u n c t i o n getTimeStamp ( ) { r e t u r n $ t h i s −>t i m e s t a m p ; } p u b l i c function getDate () { r e t u r n s t r f t i m e ( "%A %d %B %Y %T" , ( i n t ) $ t h i s −>t i m e s t a m p ) ; } p u b l i c function getEmail () { r e t u r n h t m l _ e n t i t y _ d e c o d e ( $ t h i s −>e m a i l ) ; } } public function getTexte () { r e t u r n h t m l _ e n t i t y _ d e c o d e ( $ t h i s −>t e x t e ) ; } c l a s s Database { p r i v a t e $db ; p u b l i c f u n c t i o n __construct ( ) { e r r o r _ r e p o r t i n g (E_ALL) ; try { $ t h i s −>db = new PDO( " s q l i t e : . / DATABASE/ m e s s a g e s . s q l i t e " ) ; $ t h i s −>db−> s e t A t t r i b u t e (PDO : : ATTR_ERRMODE, PDO : : ERRMODE_EXCEPTION) ; } c a t c h ( E x c e p t i o n $e ) { e c h o $e−>g e t M e s s a g e ( ) ; } } p u b l i c f u n c t i o n __destruct () { try { u n s e t ( $ t h i s −>db ) ; } c a t c h ( E x c e p t i o n $e ) { e c h o $e−>g e t M e s s a g e ( ) ; } } p u b l i c f u n c t i o n e c r i r e M e s s a g e ( $message ) { try { $ t h i s −>db−>q u e r y ( "INSERT INTO m e s s a g e s VALUES (NULL, ’ " . $message −>getTimeStamp ( ) } } . " ’ , ’ " . $message −>g e t E m a i l ( ) . " ’ , ’ " . $message −>g e t T e x t e ( ) . " ’ ) ; " ) ; } c a t c h ( E x c e p t i o n $e ) { e c h o $e−>g e t M e s s a g e ( ) ; } public function lireMessages () { $messages = array () ; $ l i s t e _ m e s s a g e s = $ t h i s −>db−>q u e r y ( "SELECT ∗ FROM m e s s a g e s " ) ; f o r e a c h ( $ l i s t e _ m e s s a g e s a s $row ) { $ m e s s a g e = new Message ( $row [ ’ e m a i l ’ ] , $row [ ’ t e x t e ’ ] , $row [ ’ d a t e ’ ] ) ; $messages [ ] = $message ; } return $messages ; } ?> forum.php : <? php h e a d e r ( ’ Content−Type : text /html ; charset=UTF−8 ’) ; s e t l o c a l e (LC_TIME, " fr_FR . u t f 8 " ) ; r e q u i r e ( ’ forum . i n c . php ’ ) ; $ d b = new D a t a b a s e ( ) ; ?> < !DOCTYPE html> <html> <head> < t i t l e>Forum ASI</ t i t l e> <meta h t t p −e q u i v=" Content −Type " content=" t e x t / h t m l ; c h a r s e t=u t f −8"> <l i n k h r e f=" R e s s o u r c e s / s t y l e . c s s " r e l=" s t y l e s h e e t " type=" t e x t / c s s " /> </head> <body> <h e a d e r id=" t o p "> <img id=" l o g o " s r c=" Images / l o g o −a s i . png " a l t=" ASI " width=" 125 " height=" 58 " /> <h1>Forum ASI</h1> </ h e a d e r> <p id=" p r o b l e m e "></p> <form name=" f o r m u l a i r e " a c t i o n=" forum . php " method=" p o s t "> < f i e l d s e t> <legend>C oo rd onn ée s :</ legend> <l a b e l f o r=" name ">Nom : </ l a b e l><input 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=" name " name=" name " type=" t e x t " s i z e=" 30 " /> <l a b e l f o r=" e m a i l ">E−m a i l : </ l a b e l><input 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 " name=" e m a i l " type=" 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>Message :</ legend> <textarea rows=" 4 " c o l s=" 50 " id=" m e s s a g e " name=" m e s s a g e "></ textarea> </ f i e l d s e t> <input type=" b u t t o n " value=" P o s t e r l e 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 ( ) ; " /> <input type=" b u t t o n " value=" 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 s i s u b m i t i f ( i s s e t ($_POST) && ! empty ($_POST [ ’ name ’ ] ) ) { $ m e s s a g e = new Message ($_POST [ ’ name ’ ] . " ( " . $_POST [ ’ e m a i l ’ ] . " ) " , $_POST [ ’ message ’]) ; $db−>e c r i r e M e s s a g e ( $ m e s s a g e ) ; } ?> <h1> L i s t e d e s m e s s a g e s p o s t é s</h1> <? php // a f f i c h a g e du l i v r e d ’ o r f o r e a c h ( $db−> l i r e M e s s a g e s ( ) a s $ m e s s a g e ) : ?> <t a b l e width=" 80% " border=" 1 "> <t r> <td c l a s s=" d a t e "> <? php echo $message −>g e t D a t e ( ) ?> </ td> <td c l a s s=" e m a i l "> <? php echo $message −>g e t E m a i l ( ) ?> </ td> </ t r> <t r> <td colspan=" 2 "> <pre> <? php echo $message −>g e t T e x t e ( ) ?> </ pre> </ td> </ t r> </ t a b l e> <br/> <? php endforeach ; ?> < f o o t e r> <s t y l e type=" t e x t / c s s " s c o p e d > o b j e c t { o v e r f l o w : auto ; } </ s t y l e> <s t y l e type=" t e x t / c s s " s c o p e d > p#p r o b l e m e { c o l o r : Red ; } </ s t y l e> <s c r i p t type=" t e x t / j a v a s c r i p t "> function v e r i f i c a t i o n () { v a r e m a i l B o x = document . g e t E l e m e n t B y I d ( " e m a i l " ) ; v a r p r o b l e m e = document . g e t E l e m e n t B y I d ( " p r o b l e m e " ) ; i f ( e m a i l B o x . v a l u e==" " ) { i f ( ! probleme . hasChildNodes () ) { 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 ( document . c r e a t e T e x t N o d e ( " Champ o b l i g a t o i r e . " ) ) ; } } else { document . f o r m s [ ’ f o r m u l a i r e ’ ] . s u b m i t ( ) ; } }; function nettoyage () { i f ( confirm ( ’ Etes vous sur ? ’) ) { document . 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 " ; v a r p r o b l e m e = document . g e t E l e m e n t B y I d ( " p r o b l e m e " ) ; i f ( probleme . hasChildNodes () ) { probleme . removeChild ( probleme . childNodes . item (0) ) ; } document . f o r m s [ ’ f o r m u l a i r e ’ ] . r e s e t ( ) ; } }; </ s c r i p t> </ f o o t e r> </body> </html> Remarques 1. Le serveur asi-technoweb utilisé lors de la session précédente est toujours à votre disposition et est configuré pour interpréter les scripts PHP. 2. Pour plus d’informations sur PHP : http://www.php.net/manual/fr/ 3. Continuez à vérifier vos pages HTML et CSS sur le site du W3C (http://validator.w3.org/). 4. À l’issu de la séance, vous aurez accès à la correction de ce TDM au format PDF. 5. Déposez votre compte-rendu sur moodle sous la forme d’un fichier PDF de 2p. (1 feuille recto-verso) nommé TDM07-login.pdf, chez chacune des 2 personnes du binôme.