Sécuriser mon index par un captcha

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : Sécuriser mon index par un captcha

Re: Sécuriser mon index par un captcha

par pat01200 » 21 janv. 2011, 12:41

OUPS !

Après une utltime vérif, mon dossier était nommé "font" au lieu de "fonts", d'ouù l'erreur...

Merci beaucoup, cette fois tout fonctionne !

Re: Sécuriser mon index par un captcha

par pat01200 » 21 janv. 2011, 12:33

Oui, oui, il y est...

Re: Sécuriser mon index par un captcha

par moogli » 21 janv. 2011, 12:25

tu a bien mis un fichier de police Verdana.ttf dans le répertoire fonts qui est dans le même répertoire que ton script ?
/-image.php
-fonts
-------Verdanna.ttf

ton code fonctionne chez moi (enfin ton cadre est pas assez large mais ça fonctionne).


@+

Re: Sécuriser mon index par un captcha

par pat01200 » 21 janv. 2011, 11:35

Voilà mon code :

Code : Tout sélectionner

<?php session_start(); function ChaineAleatoire($nbcar) { $chaine = 'abcdefghjkmnpqrstuvwyz123456789'; srand((double)microtime()*1000000); $variable=''; for($i=0; $i<$nbcar; $i++) $variable .= $chaine{rand()%strlen($chaine)}; return $variable; } $_SESSION['captcha'] = ChaineAleatoire(5); $img = imagecreate (100,40) or die ("Problème de création GD"); $background_color = imagecolorallocate ($img, 255, 128, 0); $ecriture_color = imagecolorallocate($img, 255, 255, 255); $font = 'fonts/verdana.ttf'; $abscisse = 15; $ordonnée = imagesy($img) -(imagesy($img)/4); $taille = strlen($_SESSION['captcha']); $espaceentreleslettres = 20; for ($i=0;$i<$taille;$i++){ $text = $_SESSION['captcha'][ $i ]; $abs = $abscisse + $espaceentreleslettres*$i; $angle = rand(-20,20); imagettftext($img, 25, $angle, $abs-2, $ordonnée, $background_color, $font, $text); imagettftext($img, 25, $angle, $abs, $ordonnée, $ecriture_color, $font, $text); } header("Content-type: image/png"); imagepng($img); imageDestroy($img); ?>

Re: Sécuriser mon index par un captcha

par moogli » 20 janv. 2011, 22:17

qu'a tu essayé ?

fait voir le code

Re: Sécuriser mon index par un captcha

par pat01200 » 20 janv. 2011, 12:12

Merci moogli,
j'ai tout trituré et visiblement il y a un problème avec la commande "imagettftext" qui ne fonctionne pas chez moi...

Re: Sécuriser mon index par un captcha

par moogli » 19 janv. 2011, 20:45

aller je suis sympa et je me suis amuser un peu
pour captcha.php il te faut
<?php
/* Fichier captcha.png.php */
session_start();
function ChaineAleatoire($nbcar){
        $chaine = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        srand((double)microtime()*1000000);
        $variable = '';
        for($i=0; $i<$nbcar; $i++) $variable .= $chaine{rand()%strlen($chaine)};
        return $variable;
}
// on place en session la chaine qui sera à vérifier lors de la validation du formulaire
$_SESSION['Captcha'] = ChaineAleatoire(5);
// création de l'image a toi de voir la taille qui te convient en fonction de la police
$img = imagecreate (175,50) or die ("Problème de création GD");
$white = imagecolorallocate ($img, 255, 255,255); // le blanc
$grey = imagecolorallocate($img, 128, 128, 128); // le gris
$black = imagecolorallocate($img, 0, 0, 0); // le noir
// le fichier de police a utiliser pour l'affichage
$font = 'fonts/HITROAD.ttf';
//imagettftext(image , taille , angle , X , Y , couleur , fontfile , texte);
$abscisse = 15; // abscisse de base pour la première lettre du texte
// ordonnée du texte dans l'image, c'est valable pour toutes les lettres
$ordonnée = imagesy($img) -(imagesy($img)/4);
// nb de caractère
$taille = strlen($_SESSION['Captcha']);
// permet de gérer l'espace entre les lettres pour éviter le chevauchement
$espaceentreleslettres = 25;
// permet d'afficher la suite suivant un angle différent à chaque fois
for ($i=0;$i<$taille;$i++){
    $text = $_SESSION['Captcha'][ $i ];//la lettre a afficher
    $abs = $abscisse + $espaceentreleslettres*$i;//on définis sont abscisse
    $angle = rand(-20,20);// on choisis l'angle au pif entre -20 et 20
    // on affiche deux fois la chose pour fair un effet d'ombre :) (d'où le 2 sur l'abscisse)
    imagettftext($img    , 25    ,    $angle  , $abs-2  , $ordonnée , $grey   , $font    , $text);
    //le texte sur le devant en noir
    imagettftext($img    , 25    ,    $angle  , $abs  , $ordonnée , $black   , $font    , $text);
}
// on envoi le type de l'image au navigateur
header("Content-type: image/png");
// on affiche l'image
imagepng($img);
?>
n'oublie pas d'indiquer un fichier de fonts valide avant de dire que ça marche pas :)


@+

Re: Sécuriser mon index par un captcha

par moogli » 19 janv. 2011, 18:54

salut,

le second paramètre de imagestring ne peut être que de 1 à 5 (5 étant la police la plus large). Il est aussi possible de charger une font perso avec imageloadfont.

le plus simple dans ton cas serais d'utiliser imagettftext cela te permettra de gérer la taille l'angle et tous ce que tu peut voir comme artifice sur les captchas.


@+

Re: Sécuriser mon index par un captcha

par pat01200 » 19 janv. 2011, 12:19

(Re)Bonjour le forum,

après quelques essais, je viens d'arriver à faire fonctionner tout ça...

Une seule petite chose : j'aimerais que la taille des caractères qui s'affichent soit plus grosse. Est-ce possible et si oui, quel bout de code rajouter pour y parvenir ?

Re: Sécuriser mon index par un captcha

par pat01200 » 18 janv. 2011, 15:08

Merci, je vais essayer...

Re: Sécuriser mon index par un captcha

par crash » 18 janv. 2011, 13:33

Voici un petit exemple sans code de vérification du captcha. Après c'est à toi d'adapter pour faire la vérification si le captcha est bon.

index.php
<html>
<HEAD>
<title><?php echo TITRE; ?></title>
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.Style1 {font-size: xx-small}
-->
</style>
</HEAD>
<BODY onLoad="document.entree.login.focus();">
<br/>
<div id="conteneur">
<div id="titre">
<?php echo TITRE; ?>
  </div>
<div id="page">


  <form action="accueil.php" method="post" name="entree" id="entree">
    <table border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
            <td width="4" height="4" background="img/cadre/hg.gif"></td>
            <td height="4" background="img/cadre/h.gif"></td>
            <td width="4" height="4" background="img/cadre/hd.gif"></td>
             </tr>
             <tr>
            <td width="4" background="img/cadre/g.gif"></td>
         <td>

    <table border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
            <td width="4" height="4"></td>
            <td height="4"></td>
            <td width="4" height="4"></td>
             </tr>
             <tr>
            <td width="4"></td>
         <td>
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td colspan="3" class="tabtitre">Accès restreint</td>
            </tr>
            <tr>
              <td height="10" colspan="2" class="tabtit">&nbsp;</td>
              <td class="simple">&nbsp;</td>
            </tr>
            <tr>
              <td class="tabtitd">Identifiant :</td>
              <td width="10" class="tabtit">&nbsp;</td>
              <td class="simpleg"><input name="login" type="text" style="simpled"></td>
            </tr>
            <tr>
              <td class="tabtit">&nbsp;</td>
              <td class="tabtit">&nbsp;</td>
              <td class="tabtit">&nbsp;</td>
            </tr>
            <tr>
              <td class="tabtitd">Mot de passe :</td>
              <td>&nbsp;</td>
              <td class="simple"><input name="mdp" type="password"></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td colspan="3" class="ligne"></td>
            </tr>

	// Ici se met le code du captcha
	    <tr>
	        <td class="tabtitd">Recopiez le code :</td>
		<td><?php echo '<img src="captcha.php?PHPSESSID='.session_id().'" alt="Recopiez le code"/>';?></td>
		<td><input type="text" name="code"></td>
	    </tr>
       // fin du code     
         
            <tr>
              <td colspan="3" class="ligne"></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
            </tr>         
            <tr>
              <td colspan="3" class="tabtit"><input type="submit" name="button" id="button" value="Valider la saisie et acc&eacute;der au <?php echo LOGO; ?>">
                <input name="envoi" type="hidden" value="oui"> </td>
            </tr>
          </table>
         </td>
            <td width="4"></td>
             </tr>
         <tr>
            <td width="4" height="4"></td>
            <td height="4"></td>
            <td width="4" height="4"></td>
             </tr>
</table>
   </td>
            <td width="4" background="img/cadre/d.gif"></td>
             </tr>
         <tr>
            <td width="4" height="4" background="img/cadre/bg.gif"></td>
            <td height="4" background="img/cadre/b.gif"></td>
            <td width="4" height="4" background="img/cadre/bd.gif"></td>
             </tr>
</table>
</form>
<p class="simplec"><?php echo LOGO; ?>. Version 1.1</p>
</div>
<div id="pied">
</body>
</html>
captcha.php
<?php
/* Fichier captcha.png.php */
session_start();

header("Content-type: image/png");

$img = imagecreate (50,15) or die ("Problème de création GD");
$background_color = imagecolorallocate ($img, 255, 255, 255);
$ecriture_color = imagecolorallocate($img, 0, 0, 0);
imagestring ($img, 20, 4, 0, $_SESSION['Captcha'] , $ecriture_color);
imagepng($img);

?>

<?php
/*
Page contenant le captcha PHP
*/
session_start();
function ChaineAleatoire($nbcar)
{
	$chaine = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

	srand((double)microtime()*1000000);

	$variable='';
        
	for($i=0; $i<$nbcar; $i++) $variable .= $chaine{rand()%strlen($chaine)};
	return $variable;
}
$_SESSION['Captcha'] = ChaineAleatoire(5);
?>

Re: Sécuriser mon index par un captcha

par pat01200 » 18 janv. 2011, 11:02

Même chose crash, ton lien ne m'est pour l'instant d'aucune utilité, comme tu le dis, c'est un simple exemple et je n'y comprends pas grand chose. J'ai modifié mon captcha php comme ceci :

Code : Tout sélectionner

<?php session_start(); header("Content-type: image/png"); $width=110; $height=40; $image=imagecreatetruecolor($width,$height); $couleur1 = imagecolorallocate($image, $rouge=rand(0,255), $vert=rand(0,255), $bleu=rand(0,255)); imagefill($image,0,0,$couleur1); $couleur2= imagecolorallocate($image,255-$rouge,255-$vert,255-$bleu); $nbr_caractere=rand(5,7); $taille_char_min=14; $taille_char_max=18; $char_autorise = 'ABCDEFGHKLMNPRTWXYZ234569'; $x = 4; $inter_space = 18; $i=0; while($i<$nbr_caractere) { $caractere_setting[$i]['caractere']=$char_autorise{rand(0,strlen($char_autorise)-1)}; $caractere_setting[$i]['taille']=rand($taille_char_min, $taille_char_max); $caractere_setting[$i]['angle']=rand(-20,20); imagettftext($image,$caractere_setting[$i]['taille'],$caractere_setting[$i]['angle'],$x,30,$couleur2,'ELECHA.TTF',$caractere_setting[$i]['caractere']); $x+=$inter_space; $i++; } imagepng($image, "captcha.png"); ?>
Est-ce bien cela qu'il fallait faire ? Si oui, que dois-je écrire dans mon index. php, pour que ce captcha fonctionne ?

Re: Sécuriser mon index par un captcha

par crash » 17 janv. 2011, 15:18

Je te conseille d'aller voir ce lien, c'est un simple exemple de comment il faut faire, de plus comme le dit moogli il manque la balise img et une session ce qui est décrit dans ce lien => http://www.zone-webmasters.net/codes-so ... n-php.html

Re: Sécuriser mon index par un captcha

par pat01200 » 17 janv. 2011, 14:47

Merci pour tes suggestions moogli, mais mon niveau est encore bien trop faible pour m'en sortir tout seul, même avec l'aide tuto que tu m'as mis en lien, c'est bien pour cela que je fais appel au forum...

En fait, je cherche quelqu'un qui soit capable de me fournir le code à insérer entre mes deux codes lignes, code faisant référence au captcha de mon fichier captcha.php... et de me dire s'il y a d'autres manipulations à faire, auquel cas lesquelles.

Merci d'avance à tous ceux qui voudront bien se pencher sur mon problème et m'apporter une solution.

Re: Sécuriser mon index par un captcha

par moogli » 17 janv. 2011, 12:51

Salut,

Il faut simplement utiliser la balise html img pour afficher l'image
Ensuite je te suggere de sauvegarder la chaine de caractere quelque part (session par exemple) si tu veut que la chose soit exploitable ;)

Je re suggere aussi la lecture de se tuto sur la librairie GD pour comprendre la chise et ne rien oublier (comme un header par exemple).

@+