simple-php-captcha icon indicating copy to clipboard operation
simple-php-captcha copied to clipboard

Output error

Open hiddekat opened this issue 8 years ago • 17 comments

The captcha does not show.

Output: Array ( [code] => 49cTm [image_src] => /hp-captcha.php?_CAPTCHA&t=0.39992300+1464080100 ) Even when I change the file simple-php-captcha.php to force the proper output, the image won't load, so I guess there is another problem.

Please help

hiddekat avatar May 24 '16 08:05 hiddekat

I also encountered this problem. Have you found a resolution?

rhwssi avatar Oct 26 '16 23:10 rhwssi

Need more info. Anything in the error log?

Was that output copy and pasted? If so, the path seems wrong and the issue may be easier to identify.

claviska avatar Oct 27 '16 00:10 claviska

Is this problem resolved? I experience this problem too. Image won't upload, even the URL show proper output.

iwanrst85 avatar Dec 10 '16 04:12 iwanrst85

The problem appears when I set array of parameters. When I use simple_php_captcha without parameters ($_SESSION['captcha'] = simple_php_captcha()) then everything is ok. Sorry for my english.

gregor85pl avatar Jan 04 '17 19:01 gregor85pl

The problem persists. I see a extra / before the path. I checked simple-php-captcha.php and on line 78 $image_src = '/' . ltrim(preg_replace('/\\/', '/', $image_src), '/'); changed it to $image_src = ltrim(preg_replace('/\\/', '/', $image_src), '/'); and this worked. Need to know if this is right....

kisanbhat avatar Jan 11 '17 04:01 kisanbhat

I think this is a bug in how the are generated in certain environments. A better approach might be to output a data URI to avoid paths altogether.

claviska avatar Jan 14 '17 16:01 claviska

That is how I fixed the bug by checking data URI output. Thanks for the simple php captcha.

kisanbhat avatar Jan 14 '17 16:01 kisanbhat

I am comparing this two things but it is not working sir any idea ?

keyurvala avatar Feb 10 '17 09:02 keyurvala

Sometimes when you use more than 7 characters for captcha code, generated captcha image become bigger than background image.

Variable $text_pos_x_max at line 147 become negative and when you calculate variable $text_pos_x at line 148, mt_rand throw error.

Line from error log: mt_rand(): max(-67) is smaller than min(0) in simple-php-captcha.php on line 148.

GregorTxyz avatar Apr 29 '17 21:04 GregorTxyz

How to verify if the captcha image is equal with input

So if in image is : 7m34z

And user type: 7m34z , This is ok otherwise error!

How to do this?

sticksoon avatar Jun 14 '17 22:06 sticksoon

@sticksoon This is shown here: http://labs.abeautifulsite.net/simple-php-captcha/

claviska avatar Jun 14 '17 22:06 claviska

@claviska , I have try the following code:

if($_SESSION['captcha']['code'] === $captcha) {
		echo "good";
	} else {
		echo "bad";
	}

So $captcha is the variable from which client what has post.

But that code not work.

sticksoon avatar Jun 14 '17 22:06 sticksoon

The problem is in the crude way he is determining the relative path of "simple-php-captcha.php" when you include it and it is not located in the 'WEB_ROOT' but in a sub-directory, like '/captcha/simple-php-captcha.php'.

The bug occurs if the following is not true, and you enter the else statement: if ( strpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['DOCUMENT_ROOT']))

I'm sure there is a more elegant way do this, but I feel lazy today so in 'simple-php-captcha.php' on line 77, change: $image_src = substr(__FILE__, strlen( realpath($_SERVER['WEB_ROOT']) )) . '?_CAPTCHA&t=' . urlencode(microtime()); To $image_src = substr(__FILE__, (strlen(dirname(realpath($_SERVER['WEB_ROOT']))))) . '?_CAPTCHA&t=' . urlencode(microtime());

This provides a path that doesn't end with an extra slash that strlen will count and remove.

gGambit avatar Nov 05 '17 17:11 gGambit

I also had problem with captcha not showing, but after I removed line simple-php-captcha:122, problem disappeared // unset($_SESSION['_CAPTCHA']);

dybuk87 avatar Jan 22 '18 20:01 dybuk87

I have try the following code: if($_POST['captcha'] !== $_SESSION['captcha']['code']){ echo '<script>alert("'.$_SESSION['captcha']['code'].'");</script>'; }else{ echo '<script>alert("Chính Xác");</script>'; }But not working

fatcoderr avatar Jun 22 '18 12:06 fatcoderr

After several hours of playing with this here's what I've found.

First before doing anything I made the changes addressed here: https://github.com/yasirmturk/simple-php-captcha/pull/28/files/adabbfd514599be67400ca7d1834f4c42289ca91 as they had not been integrated into the script yet.

If you have the script in a subfolder such as /captcha, the image will only display if you use the default settings. If you want to use your own settings you MUST spell out the different subdirectory in your settings, because the script while allowing for subfolders in the default settings, does not do so when compiling special settings. I followed the example within the script itself and used the following: // INITIALIZE CAPTCHA if (session_status() === PHP_SESSION_NONE){session_start();} include('captcha/simple-php-captcha.php');

$bg_path = dirname(__FILE__) . '/captcha/backgrounds/'; $font_path = dirname(__FILE__) . '/captcha/fonts/'; $_SESSION['captcha'] = simple_php_captcha( array( 'min_length' => 5, 'max_length' => 7, 'backgrounds' => array( $bg_path . 'grey-sandbag.png', $bg_path . 'kinda-jean.png', $bg_path . 'polyester-lite.png', $bg_path . 'stitched-wool.png', $bg_path . 'white-wave.png' ), 'fonts' => array( $font_path . 'crud.ttf', $font_path . 'goodbye.ttf', $font_path . 'GreatStorm.ttf', $font_path . 'Nosebled.ttf' ), 'characters' => 'ABCDEFGHJKLMNPRSTUVWXYZabcdefghjkmnprstuvwxyz23456789', 'min_font_size' => 28, 'max_font_size' => 28, 'color' => '#606060', 'angle_min' => 0, 'angle_max' => 10, 'shadow' => true, 'shadow_color' => '#fff', 'shadow_offset_x' => -1, 'shadow_offset_y' => 1 ));

Now the image displayed as expected.

Next I had the problem of every time I submitted the form with the correct captcha code, it would fail to validate because the action of submitting the form was causing the script to generate a completely new captcha which no longer matched the one I submitted. So I made the following change to the script. Original :: // Generate CAPTCHA code if not set by user if( empty($captcha_config['code']) ) { $captcha_config['code'] = ''; $length = mt_rand($captcha_config['min_length'], $captcha_config['max_length']); while( strlen($captcha_config['code']) < $length ) { $captcha_config['code'] .= substr($captcha_config['characters'], mt_rand() % (strlen($captcha_config['characters'])), 1); } }

Changed to :: // Generate CAPTCHA code if not set by user if( !isset($_SESSION['captcha']['code']) || $_SESSION['captcha']['code'] == '') { $captcha_config['code'] = ''; $length = mt_rand($captcha_config['min_length'], $captcha_config['max_length']); while( strlen($captcha_config['code']) < $length ) { $captcha_config['code'] .= substr($captcha_config['characters'], mt_rand() % (strlen($captcha_config['characters'])), 1); } } else {$captcha_config['code'] = $_SESSION['captcha']['code'];}

Now the captcha code is remembered no matter how many times the form is submitted and fails.

It may not be the cleanest fix, but for the moment it's working for me. Hope this makes sense and helps answer some questions.

sixstringv avatar Aug 20 '18 22:08 sixstringv

If you have the script in a subfolder such as /captcha


<?php
header('Content-type: application/html; charset=windows-1251');

    session_start(); 
	$pathMain = 'captcha/simple-php-captcha.php';
    include($pathMain);


    $_SESSION['captcha'] = simple_php_captcha( array(
		'pathMain' => $pathMain,
        'min_length' => 4,
....
....
        'shadow_offset_y' => 1
    ));

    $out = '<img src='. $_SESSION['captcha']['image_src'] . ' alt="">'; 

	
    echo $out;
?>

in simple-php-captcha.php : $image_src = $captcha_config['pathMain'] . '?_CAPTCHA&amp;t=' . urlencode(microtime());

assoft89 avatar Sep 18 '19 04:09 assoft89