Captcha

Fermé
jameskougar Messages postés 55 Date d'inscription jeudi 28 août 2008 Statut Membre Dernière intervention 18 mars 2013 - 5 déc. 2010 à 20:27
bg62 Messages postés 23640 Date d'inscription samedi 22 octobre 2005 Statut Modérateur Dernière intervention 2 septembre 2024 - 6 déc. 2010 à 09:58
Bonjour,
J'ai suivi un tutoriel pour créer un captcha et le problème c'est que l'image n'est pas affiché lorsque je teste le formulaire. Est ce que quelqu'un pourrait m'aider s'il vous plait?
Merci d'avance

<?php

session_start();

function genPWD ($int_min, $int_max=0) {
	mt_srand();
	if ($int_max != 0) $longueur = mt_rand($int_max, $int_min);
	else $longueur = $int_min;

	$mdp = '';
	for($i=0; $i<$longueur; $i++){
		$quoi= mt_rand(1,3);
		switch($quoi){
			case 1: $mdp .= mt_rand(0,9); break;
			case 2: $mdp .= chr(mt_rand(65,90)); break;
			case 3: $mdp .= chr(mt_rand(97,122)); break;
		}
	}
	return $mdp;
}

function generate_captcha()
{
	putenv('GDFONTPATH=' . realpath('.'));

	$pwd = array();
	$final = array();

	mt_srand();
	for($i=0;$i<mt_rand(500,1000);$i++)
	{
		$pwd[] = genPWD(6,8);
	}

	while(count($final)<6)
	{
		mt_srand();
		$value = mt_rand(0,count($pwd));
		if(!in_array($pwd[$value],$final))
		{
			$final[] = $pwd[$value];
		}
	}

	mt_srand();
	$nb = mt_rand(0,5);
	$texte = 'Veuillez choisir ';
	$texte2 = 'le code écrit en rouge.';
	$img = imagecreate(220,150);
	$bg = imagecolorallocate($img,255,255,255);
	$txt = imagestring($img,5,10,10,$texte,imagecolorallocate($img,0,0,0));
	$txt = imagestring($img,5,10,23,$texte2,imagecolorallocate($img,0,0,0));
	$i = 0;	
	$font = 'kmkdspt';
	
	foreach($final as $key => $value)
	{
		if($key == $nb)
		{
			$color = imagecolorallocate($img,200,0,0);
			$return = $value;
		}
		else
		{
			$color = imagecolorallocate($img,0,0,0);
		}

		if( ($i%2) == 0)
		{
			$txt = imagettftext($img,14,15,20,80+($i*13),$color,$font,$value);
		}
		else
		{
			$txt = imagettftext($img,14,15,110,80+(($i-1)*13),$color,$font,$value);
		}

		$i++;
	}

	imagepng($img);	
	return $return;
}
$_SESSION['code'] = generate_captcha();
?>



1 réponse

bg62 Messages postés 23640 Date d'inscription samedi 22 octobre 2005 Statut Modérateur Dernière intervention 2 septembre 2024 2 381
6 déc. 2010 à 09:58
vois ici:
https://www.commentcamarche.net/faq/19213-php-code-antispam-avec-chiffres-et-lettres-captcha
et:
https://www.unesourisetmoi.info/pages/captchapage.php
:-)
0