• Aucun résultat trouvé

Cours Système Web PHP TD6 Fichiers, sessions

N/A
N/A
Protected

Academic year: 2022

Partager "Cours Système Web PHP TD6 Fichiers, sessions"

Copied!
2
0
0

Texte intégral

(1)

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

(2)

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

Références

Documents relatifs

[r]

[r]

Pour sélectionner des données nuplet_d_Attributs dans la table MaTable selon une condition Cond, on utilise la requête suivante : SELECT nuplet_d_attributs!. FROM MaTable

[r]

– INSERT INTO Table (Colonne1,Colonne2) VALUES (valeur1,valeur2) – SELECT ∗ FROM Table WHERE Condition ORDER BY Colonne – SELECT Colonne1,Colonne2 FROM Table. – DELETE FROM

La création d’un nouveau fichier se fait dans le répertoire courant. Suppression d’un fichier

La création d’un nouveau fichier se fait dans le répertoire courant.. Suppression d’un fichier

Écrire une fonction PHP prenant en argument un tableau associatif $assoc, et produisant (avec echo) un tableau HTML à deux colonnes, la première colonne contenant les clés du