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
<? php
f u n c t i o n compteur ( $nomdufichier , $ f l a g i n c r e m e n t =TRUE) { i f( 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 = fopen ( $nomdufichier ,’ r+ ’) ; 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 ( $compteur ) )
$compteur = 0 ; e l s e
s e t t y p e ( $compteur , " i n t e g e r ") ; i f ( $ f l a g i n c r e m e n t )
$compteur++;
} e l s e {
$ f i c h i e r = fopen ( $nomdufichier ,’ c ’) ; f l o c k ( $ f i c h i e r , LOCK_EX) ;
$compteur = 1 ; }
i f ( $ f l a g i n c r e m e n t ) { 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) ; f c l o s e ( $ f i c h i e r ) ;
return $compteur ; }?>
pagecompteurs.php
<? php s e s s i o n _ s t a r t ( ) ;
r e q u i r e (’ compteur . 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=" content−t y p e " content=" t e x t / html ; c h a r s e t=u t f−8" />
</head>
<body>
<p>Compteur de h i t s : <? php echo compteur (’ compteur . t x t ’) ; ?></p>
<p>Compteur de v i s i t e s : <? php echo compteur (’ 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 connexion.</a></p>
</body>
</html>
logout.php
<? php
s e s s i o n _ s t a r t ( ) ;
s e s s i o n _ d e s t r o y ( ) ;
header (" Location : . / pagecompteurs . 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=" content−t y p e " content=" t e x t / html ; c h a r s e t=u t f−8" />
<l i n k h r e f=" data / 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=" submit " 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 (" data / 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 as $ 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) { echo "<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 as $ a u t e u r) { echo "<auteur >" . $ a u t e u r . "</auteur >";
}// Le t i t r e
echo "< 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 ’) {
echo " Jo u r n al : <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 ’) {
echo " Conférence : <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
echo "<pages >" . $ p u b l i c a t i o n−>pages . "</pages ><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=" content−t y p e " content=" t e x t / html ; c h a r s e t=u t f−8" />
<t i t l e>Cr éation de l a BD pour 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) ; t r y
{ $ r e q u e t e = "CREATE TABLE IF NOT EXISTS messages ( i d INTEGER PRIMARY KEY,
d a t e INTEGER NOT NULL, email TEXT NOT NULL, t e x t e TEXT NOT NULL ) ";
/∗ c r e a t i o n de l a BD ∗/
$db = new PDO(" s q l i t e : . /DATABASE/ messages . 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−>query($ r e q u e t e) ; u n s e t($db) ;
echo ’ Table c r e a t e d ’; }c a t c h(PDOException $e) { echo $e−>getMessage( ) ;
?>}
</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 ; p r i v a t e $ t e x t e ;
p u b l i c f u n c t i o n __construct ( $email , $ 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−>timestamp = time( ) ; e l s e
$ t h i s−>timestamp = $ d a t e;
$ t h i s−>email = h t m l e n t i t i e s($email) ;
$ t h i s−>t e x t e = h t m l e n t i t i e s($ t e x t e) ; }
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−>timestamp; }
p u b l i c f u n c t i o n g e t D a t e( ) {
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−>timestamp) ; }
p u b l i c f u n c t i o n ge tEma il( ) {
r e t u r n html_entity_decode($ t h i s−>email) ; }
p u b l i c f u n c t i o n g e t T e x t e( ) {
r e t u r n html_entity_decode($ 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) ; t r y {
$ t h i s−>db = new PDO(" s q l i t e : . /DATABASE/ messages . 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) {
echo $e−>getMessage( ) ; } }
p u b l i c f u n c t i o n __destruct( ) { t r y {
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) {
echo $e−>getMessage( ) ;
} }
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) { t r y {
$ t h i s−>db−>query("INSERT INTO messages VALUES (NULL, ’ " . $message−>getTimeStamp( ) . " ’ , ’ " . $message−>getEmail( ) . " ’ , ’ " . $message−>g e t T e x t e( ) . " ’ ) ; ") ;
} c a t c h(E x c e p t i o n $e) { echo $e−>getMessage( ) ; } }
p u b l i c f u n c t i o n l i r e M e s s a g e s( ) {
$messages = array( ) ;
$ l i s t e _ m e s s a g e s = $ t h i s−>db−>query("SELECT ∗ FROM messages ") ; f o r e a c h($ l i s t e _ m e s s a g e s as $row) {
$message = new Message($row[’ email ’] , $row[’ t e x t e ’] , $row[’ d a t e ’] ) ;
$messages[ ] = $message; }r e t u r n $messages;
} }
?>
forum.php :
<? php
header (’ Content−Type : t e x t / html ; c h a r s e t=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 ’) ;
$db = new Database ( ) ;
?>
<!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 / html ; c h a r s e t=u t f−8">
<l i n k h r e f=" Resso ur c es / 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>
<header i d=" t o p ">
<img i d=" l o g o " s r c=" Images / logo−a s i . png " a l t=" ASI " width=" 125 " height=" 58 " />
<h1>Forum ASI</h1>
</ header>
<p i d=" probleme "></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>Coordonnées :</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" i d="
name " name=" name " type=" t e x t " s i z e=" 30 " />
<l a b e l f o r=" email ">E−mail : </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 email "
i d=" email " name=" email " 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>
<t e x t a r e a rows=" 4 " c o l s=" 50 " i d=" message " name=" message "></t e x t a r e a>
</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 message " 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 message s i submit i f ( i s s e t ($_POST) && ! empty ($_POST[’ name ’] ) ) {
$message = new Message ($_POST[’ name ’] ." ( ".$_POST[’ email ’] ." ) ", $_POST[’ message ’ ] ) ;
$db−>e c r i r e M e s s a g e($message) ; }?>
<h1>L i s t e des messages p o s t é 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 ( $db−>l i r e M e s s a g e s ( ) as $message ) :
?>
< t a b l e w i d t h ="80%" b o r d e r ="1">
<tr >
<t d c l a s s =" d a t e "> <?php echo $message−>g e t D a t e ( ) ?> </td>
<t d c l a s s =" email "> <?php echo $message−>g etEmai l ( ) ?> </td>
</tr >
<tr >
<t d c o l s p a n ="2"> <pre> <?php echo $message−>g e t T e x t e ( ) ?> </pre> </td>
</tr >
</ t a b l e >
<br/>
<?php
e n d f o r e a c h ;
?>
<f o o t e r >
< s t y l e t y p e =" t e x t / c s s " scoped >
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 t y p e =" t e x t / c s s " scoped >
p#probleme { c o l o r : Red ; }
</ s t y l e >
< 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 emailBox = document . getElementById ( " email " ) ; var probleme = document . getElementById ( " probleme " ) ; i f ( emailBox . v a l u e =="") {
i f ( ! probleme . hasChildNodes ( ) ) { emailBox . s t y l e . o u t l i n e =" s o l i d Red " ;
probleme . appendChild ( document . createTextNode ( " Champ o b l i g a t o i r e . " ) ) ; } e l s e {}
document . forms [ ’f o r m u l a i r e’ ] . submit ( ) ; } ;}
f u n c t i o n n e t t o y a g e ( ) {
i f ( confirm ( ’Etes vous sur ?’ ) ) {
document . getElementById ( " email " ) . s t y l e . o u t l i n e =" i n i t i a l " ; var probleme = document . getElementById ( " probleme " ) ; i f ( probleme . hasChildNodes ( ) ) {
probleme . removeChild ( probleme . c h i l d N o d e s . item ( 0 ) ) ; }document . forms [ ’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>