Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output error #21

Open
hiddekat opened this issue May 24, 2016 · 17 comments
Open

Output error #21

hiddekat opened this issue May 24, 2016 · 17 comments
Labels

Comments

@hiddekat
Copy link

hiddekat commented May 24, 2016

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

@rhwssi
Copy link

rhwssi commented Oct 26, 2016

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

@claviska
Copy link
Collaborator

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.

@iwanrst85
Copy link

iwanrst85 commented Dec 10, 2016

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

@gregor85pl
Copy link

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.

@kisanbhat
Copy link

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....

@claviska
Copy link
Collaborator

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.

@kisanbhat
Copy link

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

@keyurvala
Copy link

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

@GregorTxyz
Copy link

GregorTxyz commented Apr 29, 2017

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.

@sticksoon
Copy link

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?

@claviska
Copy link
Collaborator

@sticksoon
Copy link

@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.

@gGambit
Copy link

gGambit commented Nov 5, 2017

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.

@dybuk87
Copy link

dybuk87 commented Jan 22, 2018

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

@fatcoderr
Copy link

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

@sixstringv
Copy link

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.

@assoft89
Copy link

assoft89 commented Sep 18, 2019

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());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests