Captcha en php

Fermé
derrick - 9 juil. 2010 à 19:02
 derrick - 31 juil. 2010 à 11:24
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

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>
0
bg62 Messages postés 23682 Date d'inscription samedi 22 octobre 2005 Statut Modérateur Dernière intervention 14 février 2025 2 396
9 juil. 2010 à 20:13
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
0
avion-f16 Messages postés 19252 Date d'inscription dimanche 17 février 2008 Statut Contributeur Dernière intervention 10 février 2025 4 505
10 juil. 2010 à 00:37
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 />.
0
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
0
avion-f16 Messages postés 19252 Date d'inscription dimanche 17 février 2008 Statut Contributeur Dernière intervention 10 février 2025 4 505
10 juil. 2010 à 13:34
Affiche le script captcha.php dans ton navigateur.
0
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
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
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.
0
bg62 Messages postés 23682 Date d'inscription samedi 22 octobre 2005 Statut Modérateur Dernière intervention 14 février 2025 2 396
11 juil. 2010 à 15:17
regarde ici:
https://www.unesourisetmoi.info/pages/captchapage.php
choisis celui que tu veux, avec lettres ou nombres
met-le en ligne et teste sur ton hébergement de captcha
on verra alors, s'il fonctionne, si le problème vient ou non de la génération de ton script ...
:-)
0
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
0
bg62 Messages postés 23682 Date d'inscription samedi 22 octobre 2005 Statut Modérateur Dernière intervention 14 février 2025 2 396
13 juil. 2010 à 11:45
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
:-)
0
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.
0
bg62 Messages postés 23682 Date d'inscription samedi 22 octobre 2005 Statut Modérateur Dernière intervention 14 février 2025 2 396
13 juil. 2010 à 14:52
regarde bien, les deux solutions que je lui ai proposées ne génèrent pas d'images, beaucoup plus simples et aussi efficaces :-)
0
avion-f16 Messages postés 19252 Date d'inscription dimanche 17 février 2008 Statut Contributeur Dernière intervention 10 février 2025 4 505
13 juil. 2010 à 15:08
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".
0
Profil bloqué
13 juil. 2010 à 17:23
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
0
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?
0
avion-f16 Messages postés 19252 Date d'inscription dimanche 17 février 2008 Statut Contributeur Dernière intervention 10 février 2025 4 505
21 juil. 2010 à 18:25
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 ... ?>).
0
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);
?>
0
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
0
avion-f16 Messages postés 19252 Date d'inscription dimanche 17 février 2008 Statut Contributeur Dernière intervention 10 février 2025 4 505
29 juil. 2010 à 12:25
C'est normal, il y a du texte qui est envoyé au navigateur avant
header('Content-type : image/png');
Les commentaires doivent être directement placés dans le PHP, il ne faut pas fermer la "balise".

https://openclassrooms.com/fr/courses/918836-concevez-votre-site-web-avec-php-et-mysql#ss_part_3
0
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
0
avion-f16 Messages postés 19252 Date d'inscription dimanche 17 février 2008 Statut Contributeur Dernière intervention 10 février 2025 4 505
30 juil. 2010 à 17:56
Quel est ton code actuel ? (utilise la balise "code" de CCM ou un pastebin).
0
<?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);
?>
0