par
AB » 16 avr. 2010, 15:54
Pour protéger des photos dans un répertoire une solution est de mettre un fichier .htaccess dans ce même répertoire avec la mention deny from all.
Ainsi on ne pourra pas avoir accès à l'image en rentrant simplement sont adresse.
Pour afficher l'image on passera donc par php avec un code qui sera protégée par authentification.
Par exemple un script php que l'on nomme affiche_img.php et qui sera appelé par la page d'affichage des photos :
<?php
// Code de protection
if(!isset($_SESSION['login'])) exit('message');
$img = isset($_GET['img'])? $_GET['img'] :'';
header("Content-Type: image/jpeg");
readfile("$img");
?>
Bien entendu il faudrait vérifier les variables $_GET
Code de la page d'affichage des images qui fait appel à affiche_img.php. (Le répertoire photo doit être protégé par le fichier .htaccess)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Document sans titre</title>
</head>
<body>
<div>
<img src="affiche_img.php?img=PHOTO/photo1.jpg" />
</div>
</body>
</html>
Pour protéger des photos dans un répertoire une solution est de mettre un fichier .htaccess dans ce même répertoire avec la mention deny from all.
Ainsi on ne pourra pas avoir accès à l'image en rentrant simplement sont adresse.
Pour afficher l'image on passera donc par php avec un code qui sera protégée par authentification.
Par exemple un script php que l'on nomme affiche_img.php et qui sera appelé par la page d'affichage des photos :
[php]
<?php
// Code de protection
if(!isset($_SESSION['login'])) exit('message');
$img = isset($_GET['img'])? $_GET['img'] :'';
header("Content-Type: image/jpeg");
readfile("$img");
?>[/php]
Bien entendu il faudrait vérifier les variables $_GET
Code de la page d'affichage des images qui fait appel à affiche_img.php. (Le répertoire photo doit être protégé par le fichier .htaccess)
[html]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Document sans titre</title>
</head>
<body>
<div>
<img src="affiche_img.php?img=PHOTO/photo1.jpg" />
</div>
</body>
</html>[/html]