Cours Système Web PHP Fichiers, sessions TD6
5 décembre 2005
1 Fichiers
cf corrigé du TP5.
2 Sessions, exemple élémentaire
cf corrigé du TP4.
3 Sessions, authentication
1. <!DOCTYPE HTML PUBLIC "−//W3C//DTD HTML 4.01 S t r i c t //EN"
" http : / /www. w3 . org /TR/html4/ s t r i c t . dtd">
<html lang=" f r ">
<head>
<meta http−equiv="Content−Type" content=" te xt /html ; c h a r s e t=iso−8859−1">
<t i t l e >Login</ t i t l e >
</head>
<body>
<h1>Login</h1>
<form a c t i o n=" l o g i n . php" method=" get ">
<div>
<l a b e l for=" l o g i n ">Login : </l a b e l >
<input type=" te xt " name=" l o g i n " id=" l o g i n ">
</div>
<div>
<l a b e l for="mot_de_passe">Mot de passe : </l a b e l >
<input type=" password " name="mot_de_passe" id="mot_de_passe">
</div>
<div>
<input type=" submit " value="OK">
</div>
</form>
</body>
</html>
2. <?php
session_start ( ) ;
$_SESSION [ ' l o g i n ' ]=$_REQUEST[ ' l o g i n ' ] ;
$_SESSION [ ' mot_de_passe ' ]=$_REQUEST[ ' mot_de_passe ' ] ; header ( " Location : main . php" ) ;
?>
1
3. <?php
f u n c t i o n login_ok ( ) {
$ f i l e=fopen ( " . . / f i c h i e r s / i d e n t i f i a n t s . txt " , " r " ) ; i f ( $ f i l e ) {
while ( $ l i g n e=f g e t s ( $ f i l e ) ) {
$space=strpos ( $ l i g n e , ' ' ) ;
$ l o g i n=substr ( $ l i g n e , 0 , $space ) ;
$password=substr ( $ l i g n e , $space +1, strlen ( $ l i g n e )−$space−2);
$_SESSION [ ' mot_de_passe ' ] . "<br>" ;
i f ($_SESSION [ ' l o g i n ']== $ l o g i n && $_SESSION [ ' mot_de_passe ']==$password ) { fclose ( $ f i l e ) ;
return true ; } }
}return f a l s e ;
?>}
4. <?php session_start ( ) ; include " t o o l s . php" ; ?>
<!DOCTYPE HTML PUBLIC "−//W3C//DTD HTML 4.01 S t r i c t //EN"
" http : / /www. w3 . org /TR/html4/ s t r i c t . dtd">
<html lang=" f r ">
<head>
<meta http−equiv="Content−Type" content=" te x t /html ; c h a r s e t=iso−8859−1">
<t i t l e >Page p r i n c i p a l e </ t i t l e >
</head>
<body>
<?php i f (empty($_SESSION [ " l o g i n " ] ) ) { ?>
<p>Vous devez vous i d e n t i f i e r pour acceder a c e t t e page.</p>
<p><a h r e f=" form_login . html">Retour au f o r m u l a i r e d ' i d e n t i f i c a t i o n </a></p>
<?php } e l s e i f ( ! login_ok ( ) ) { ?>
<p>I d e n t i f i a n t s i n c o r r e c t s .</p>
<p><a h r e f="form_login . html">Retour au f o r m u l a i r e d ' i d e n t i f i c a t i o n </a></p>
<?php } else { ?>
<p>Vous avez acces au contenu protege , <?= $_SESSION [ ' l o g i n ' ] ?> !</p>
<?php } ?>
</body>
</html>
2