/    Sign up×
Community /Pin to ProfileBookmark

CAPTCHA: problem incorporating the script into the webpage.

This fully working script does not work when incorporated into a page. I have tried the following: a) put it into a function (function captcha()) and then called that function; b) created a separate page just for the ‘captcha’ script, and the included that page with the ‘include’ function. The result is always the same: [B]The image (…. path …) cannot be displayed, because it contains errors.[/B] This is so frustrating! As I said, the script DOES work on its own. Take a look:

[code=php]
ob_start();
$image = imagecreate(80,30);
$bg_color = imagecolorallocate($image,102,102,102);

// —————– text ————————-
$letters = array(“a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”,”i”,”j”,”k”,”l”,”m”,”n”,”o”,”p”,”q”,”r”,”s”,”t”,”u”,”v”,”w”,”x”,”y”,”z”);
$numbers = array(1,2,3,4,5,6,7,8,9,0);
$joined = array_merge($letters, $numbers);
$word = “”;
for($i=0;$i<5;$i++) {
$ran_pos = array_rand($joined);
$word .= $joined[$ran_pos];
}

$textColor = imagecolorallocate($image, 255,255,255);
imagestring($image, 5, 15, 5, $word, $textColor);

header(‘Content-type: image/png’);
imagepng($image);
ob_flush();
[/code]

If I put the script into a function

[code=php]function captcha() {
….
….
}[/code]

and then call it, it does work [U]as long as I do NOT call it from within any HTML tags, like this:

[code=html]<body>
<?php captcha(); ?>
</body>
[/code]

I don’t know what to do. Please help. (I wasn’t been able to find a solution in Google or anywhere else).

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@Phill_PaffordJun 03.2008 — Try this:


file name: captcha.php

[code=php]
class Captcha
{
function getCaptcha()
{
ob_start();

$image = imagecreate(80,30);
$bg_color = imagecolorallocate($image,102,102,102);

// ----------------- text -------------------------
$letters = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
$numbers = array(1,2,3,4,5,6,7,8,9,0);
$joined = array_merge($letters, $numbers);
$word = "";

for($i=0;$i<5;$i++)
{
$ran_pos = array_rand($joined);
$word .= $joined[$ran_pos];
}

$textColor = imagecolorallocate($image, 255,255,255);
imagestring($image, 5, 15, 5, $word, $textColor);

header('Content-type: image/png');
imagepng($image);

ob_flush();
}
};

$captcha = new Captcha;
$captcha->getCaptcha();
[/code]


web page name: page.php
[code=html]
<?php

include('captcha.php');

?>

<img src="<?php $captcha ?>">
[/code]
Copy linkTweet thisAlerts:
@andkhlauthorJun 03.2008 — Thanks for your reply!!! Unfortunately, I got the very same error I had getting earlier. BUT, your script gave me an idea that solved the problem.

Instead of doing this:
[code=html]
<img src="<?php include(.....); ?>" />
[/code]

I did it with no PHP tags:
[code=html]
<img src="captcha.php" />
[/code]


CAPTCHA works now. A minor problem is, though, for some reason I can't set the SESSION key [B] initially[/B], when the page first loads. Of course, after I refresh the page, the SESSION key is there, but not the first time around.

Like this :
[code=php]
....
....
$textColor = imagecolorallocate($image, 255,255,255);
imagestring($image, 5, 15, 5, $word, $textColor);
$_SESSION['captcha_key'] = sha1($word);
....
.....
[/code]

When the page loads, the following script 'complains' that such SESSION does not exist.
[code=php]echo $_SESSION['captcha_key'][/code]
NOTE: session_start() exist on both pages (captcha.php and login.php).

It should work, but it doesn't. Would you be willing to help me with this? Thanks.
×

Success!

Help @andkhl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...