Captcha en php

Bonjour,

Je viens de créer un programme de captcha en php après avoir lu un mag de programmation.

et J'ai ces messages :
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/venteart/public_html/php/captcha/captcha.php:9) in /home/venteart/public_html/php/captcha/captcha.php on line 10

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/venteart/public_html/php/captcha/captcha.php:9) in /home/venteart/public_html/php/captcha/captcha.php on line 10

Warning: Cannot modify header information - headers already sent by (output started at /home/venteart/public_html/php/captcha/captcha.php:9) in /home/venteart/public_html/php/captcha/captcha.php on line 16

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/venteart/public_html/php/captcha/captcha.php on line 46

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/venteart/public_html/php/captcha/captcha.php on line 48

Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/venteart/public_html/php/captcha/captcha.php on line 68

Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/venteart/public_html/php/captcha/captcha.php on line 68

Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/venteart/public_html/php/captcha/captcha.php on line 68

Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/venteart/public_html/php/captcha/captcha.php on line 68

Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/venteart/public_html/php/captcha/captcha.php on line 68

13 réponses

  1. Voici le code php correspondant:

    <!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>
    <?php
    session_start();
    $largeur=100;
    $hauteur=50;

    /**Préparer le texte*/

    header('Content-type:image/png');
    $img=imagecreatetruecolor($largeur, $hauteur);

    function chaine()
    {
    $md5 = $md5(microtime()*mktime());
    $chaine=substr($md5, 0, 5);
    return $chaine;
    }

    /**fond**/

    $fond=imagecolorallocate($img, 0, 0, 0);
    imagerectangle($img, 3, 3, 40, 40, $fond);

    /**Effet sur le fond**/

    for($cube=0; $cube<25; $cube++)
    {
    $fondCol=imagecolorallocate($img, rand(0, 130)+1, rand(0, 130), rand(0, 130));
    $x=rand(0, $largeur);
    $y=rand(0,$hauteur);
    $w=rand(5, $largeur/2);
    $h=rand(5, $hauteur/2);
    imagefilledrectangle($img, $x, $y, $x+$w, $y+$h, $fondCol);
    imagecolordeallocate($img, $fondCol);
    }

    /**tracer des lignes**/

    $col=imagecolorallocate(img, 50, 128, 250);
    imageline($img, 10, 10, 400, 150, $col);
    $col=imagecolorallocate(img, 128, 250, 30);
    imageline($img, 0, 0, 39, 29, $col);
    imageline($img, 40, 0, 84, 59, $col);

    /** tracer des ellipses **/

    $col=imagecolorallocate($img, 250, 199, rand(128, 255));
    imagearc($img, 20, 25, 60, 60, 0, 360, $col);

    /** effets textes **/

    $angle=mt_rand(10, 40);
    $font="time.ttf";
    $x=rand(10, 35);
    for($c=0; $c<5; $c++)
    {
    $size=mt_rand(13, 24);
    $text=$_SESSION['text'][$c];
    $y=30+rand(0, 18);
    $color=imagecolorallocate($img, rand(100, 254), rand(100, 254), rand(100, 254));
    imagettftext($img, $size, $angle, $x+15*$c, $y, $color, $font, $text);
    }

    ?>

    </body>
    </html>
    1. Modérateur
      essaie en plaçant:
      <?php
      session_start(); 

      en première ligne ...

      le 'www' est fait aussi pour communiquer, partager et échanger, non ?
      merci d'avoir la politesse de répondre à ceux qui essaient de vous aider
      1. Salut.

        Le script PHP génère une image ... la source d'une image n'est pas directement dans le code source d'une page HTML ...

        Mets le script dans un fichier séparés et inclus-le avec la balise <img />.
        1. J'ai déjà créé un formulaire écrit en php avec une balise <img src=captcha.php /> et lorsque je teste celui-ci je n'ai pas d'image
        2. Affiche le script captcha.php dans ton navigateur.
      2. J'ai mis en première ligne

        <?php
        session_start();
        $largeur=100;
        $hauteur=50;

        ?> et

        "<?php" une ligne après <body>

        Mais j'ai ça affiché :
        Warning: Cannot modify header information - headers already sent by (output started at /home/venteart/public_html/php/captcha/captcha.php:16) in /home/venteart/public_html/php/captcha/captcha.php on line 19

        Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/venteart/public_html/php/captcha/captcha.php on line 49

        Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/venteart/public_html/php/captcha/captcha.php on line 51

        Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/venteart/public_html/php/captcha/captcha.php on line 71

        Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/venteart/public_html/php/captcha/captcha.php on line 71

        Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/venteart/public_html/php/captcha/captcha.php on line 71

        Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/venteart/public_html/php/captcha/captcha.php on line 71

        Warning: imagettftext() [function.imagettftext]: Could not find/open font in /home/venteart/public_html/php/captcha/captcha.php on line 71
        1. J'ai retiré toutes les balises html et j'ai ce message :
          L'image "http://ventearticles.webou.net/php/captcha/captcha.php" ne peut être affichée car elle contient des erreurs.
          1. J'ai essayé le captcha avec les nombres mais la page affiche :

            Warning: Cannot modify header information - headers already sent by (output started at /home/venteart/public_html/php/captcha/captchanombre.php:1) in /home/venteart/public_html/php/captcha/captchanombre.php on line 35

            Warning: Cannot modify header information - headers already sent by (output started at /home/venteart/public_html/php/captcha/captchanombre.php:1) in /home/venteart/public_html/php/captcha/captchanombre.php on line 41

            en début de page suivit du captcha et sa zone de formulaire.
            On dirait qu'il fonctionne malgré tout
            1. Modérateur
              en début de page suivit du captcha et sa zone de formulaire.
              On dirait qu'il fonctionne malgré tout

              de toutes façons il est bon et fonctionne, donc il te faut tester ...
              et si encore problèmes, là c'est chez toi ....
              donc il faudra repartir sur des bases plus simples
              :-)
              1. Perso, j'ai abandonner le probleme de la generation de captcha en image, car pour etre efficaces, elles sont presque illisible pour l'utilisateur ce qui est un tres bon moyen de faire fuire la personne ou de lui faire modifier le message qu'elle desirait ecrire.

                perso j'ai fait une table avec 'quest' et 'rep' ou j'ecris la question et la reponse attendus (la plus repandu etant bien sur "Quelle est la couleur du cheval blanc d'Henri IV?" et la réponse est bien entendu "blanc".

                Pour afficher cela aléatoirement (c'est l'interet) on fait un requete qui prend de maniere aléatoire la question et la reponse, on ecris la question et on stocke la reponse dans une variable de session.
                dans la page de reponse, on recupere la reponse donner et on compare la reponse recuperer et la reponse attendus (pensez aussi a faire un remplacement eventuelle des lettre accentues, et caracteres spéciaux afin de ne pas refuser quelqu'un qui a bien mis 'élève' au lieu de 'eleve') si c'est bon on autorise le passage sinon on refuse et on reecris les infos qui ont ete envoyer.

                Peros je prefere cela qui est plus simple a lire et plus facile a comprendre pour beaucoup de personne et j'encode la reponse avant de la stocke en session avec md5, sha1 et une clé ainsi pas de risque que cela soit lu par un robot.
                1. Modérateur
                  regarde bien, les deux solutions que je lui ai proposées ne génèrent pas d'images, beaucoup plus simples et aussi efficaces :-)
              2. Le problème avec ta (Japan-O-Fan) solution, c'est qu'il pourrait y avoir plusieurs réponses. On pourrait par exemple mettre "blanche" au lieu de "blanc". Pour améliorer cette solution, mets en gras "blanc" et précise bien que le mot et dans la phrase, la personne aura plus tendance à mettre "blanc" que "blanche".
                1. En effet, apres c'est une question de gout :p. Mais il faut simplement pensez a une dizaine de phrase dont la reponse ne peux pas s'ecrire de plusieurs facon.

                  Je donne une piste sur la maniere que j'utilise. Chaqu'un fait comme il veux :p
                  1. Et est-ce qu'il faut aussi créer une commande qui génère des cookies, c'est surement à cause de ça que j'ai toujours ces messages?
                    1. Pour écrire des cookies, aucun texte ne doit avoir été envoyé au navigateur avant.
                      Donc pas de echo et rien à l'extérieur des instructions de traitement PHP (<?php ... ?>).
                  2. Cette fois-ci, j'ai fait :
                    <?php
                    session_start();
                    $largeur=100;
                    $hauteur=50;
                    ?>

                    /** Préparer le texte **/
                    <?php
                    header('Content-type : image/png');
                    $img = imagecreatetruecolor ($largeur, $hauteur);
                    ?>

                    <?php
                    function chaine ()
                    {
                    $md5 = $md5(microtime()*mktime());
                    $chaine = substr($md5, 0, 5);
                    return $chaine;
                    }
                    $_SESSION['text'] = chaine();
                    ?>

                    /**fond **/
                    <?php
                    $fond=imagecolorallocate($img, 0, 0, 0);
                    imagerectangle($img, 3, 3, 40, 40, $fond);
                    ?>

                    /**Effet sur le fond**/
                    <?php
                    for ($cube=0; $cube<25; $cube++)
                    {
                    $fondCol=imagecolorallocate($img, rand(0,130)+1, rand(0,130), rand(0,130));
                    $x = rand(0, $largeur);
                    $y = rand(0, $hauteur);
                    $w = rand(5, $largeur/2);
                    $h = rand(5, $hauteur/2);
                    imagefilledrectangle ($img, $x, $y, $x+$w, $y+$h, $fondCol);
                    imagecolordeallocate($img, $fondCol);
                    }
                    ?>

                    /**Tracer des lignes**/
                    <?php
                    $col=imagecolorallocate($img,250,199,rand(128,255));
                    imagearc($img,20,25,60,60,0,360,$col);
                    ?>

                    /**Effet su le texte**/
                    <?php
                    $angle=mt_rand(10,40);
                    $font="time.ttf";
                    $x=rand(10,35);
                    for($c=0;$c<5;$c++)
                    {
                    $size=mt_rand(13,24);
                    $text=$_SESSION['text'][$c];
                    $y=30+rand(0,18);
                    $color=imagecolorallocate($img,rand(100,254), rand(100,254), rand(100,254));
                    imagettftext($img, $size, $angle, $x+15*$c, $y, $color, $font, $text);
                    }
                    ?>
                    <?php
                    imagepng ($img);
                    imagedestroy($img);
                    ?>
                    1. Et j'ai ce message :
                      /** Préparer le texte **/
                      Warning: Cannot modify header information - headers already sent by (output started at /home/venteart/public_html/captcha/captcha.php:8) in /home/venteart/public_html/captcha/captcha.php on line 9

                      Fatal error: Function name must be a string in /home/venteart/public_html/captcha/captcha.php on line 16
                    2. J'ai supprimé tous les commentaires mais j'ai toujours:

                      Warning: Cannot modify header information - headers already sent by (output started at /home/venteart/public_html/captcha/captcha.php:7) in /home/venteart/public_html/captcha/captcha.php on line 8

                      Fatal error: Function name must be a string in /home/venteart/public_html/captcha/captcha.php on line 15
                    3. Quel est ton code actuel ? (utilise la balise "code" de CCM ou un pastebin).
                    4. <?php
                      session_start();
                      $largeur=100;
                      $hauteur=50;
                      ?>

                      <?php
                      header('Content-type:image/png');
                      $img = imagecreatetruecolor ($largeur, $hauteur);
                      ?>

                      <?php
                      function chaine()
                      {
                      $md5 = $md5(microtime()*mktime());
                      $chaine = substr($md5, 0, 5);
                      return $chaine;
                      }
                      $_SESSION['text'] = chaine();
                      ?>

                      <?php
                      $fond=imagecolorallocate($img, 0, 0, 0);
                      imagerectangle($img, 3, 3, 40, 40, $fond);
                      ?>

                      <?php
                      for ($cube=0; $cube<25; $cube++)
                      {
                      $fondCol=imagecolorallocate($img, rand(0,130)+1, rand(0,130), rand(0,130));
                      $x = rand(0, $largeur);
                      $y = rand(0, $hauteur);
                      $w = rand(5, $largeur/2);
                      $h = rand(5, $hauteur/2);
                      imagefilledrectangle ($img, $x, $y, $x+$w, $y+$h, $fondCol);
                      imagecolordeallocate($img, $fondCol);
                      }
                      ?>

                      <?php
                      $col=imagecolorallocate($img,250,199,rand(128,255));
                      imagearc($img,20,25,60,60,0,360,$col);
                      ?>

                      <?php
                      $angle=mt_rand(10,40);
                      $font="time.ttf";
                      $x=rand(10,35);
                      for($c=0;$c<5;$c++)
                      {
                      $size=mt_rand(13,24);
                      $text=$_SESSION['text'][$c];
                      $y=30+rand(0,18);
                      $color=imagecolorallocate($img,rand(100,254), rand(100,254), rand(100,254));
                      imagettftext($img, $size, $angle, $x+15*$c, $y, $color, $font, $text);
                      }
                      ?>
                      <?php
                      imagepng ($img);
                      imagedestroy($img);
                      ?>