1

My captcha code is not working, I am able to bring up the lines at back but the numbers are not visible in the captcha. The code is working perfect on localhost but not on my hosting website.

here's the code:

<?php
session_start();
header('Content-type: image/jpeg');

$text= $_SESSION['secure'];

$font_size =30;
$image_width= 110;
$image_height=40;

$image=imagecreate($image_width,$image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);


for($x=1; $x<=30; $x++){
$x1 = rand(1,100);
$x2 = rand(1,100);
$y1 = rand(1,100);
$y2 = rand(1,100);
imageline($image, $x1, $x2, $y1, $y2, $text_color);
}

imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font.TTF', $text);
imagejpeg($image);  
?>
4
  • 2
    you should definitely use recaptcha. its api is so easy to use, it'd be a crime not to. Commented Aug 5, 2012 at 8:06
  • Thanks for the suggestion :) Can you help me with the code above? If you can figure out what the problem is? Commented Aug 5, 2012 at 8:11
  • comment this line: //header('Content-type: image/jpeg'); and see what error u get ? Commented Aug 5, 2012 at 9:59
  • @siamak.a.m I'm getting something like this >> CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality >> and a lot of rubbish things written: Check here www.pingcampus.com/mysql/generate.php Commented Aug 5, 2012 at 10:04

2 Answers 2

1

I commented that line and i got this warning and notice:

Notice: Undefined index: secure in F:\xampp\htdocs\captcha.php on line 4 
Warning: imagettftext() [function.imagettftext]: Invalid font filename in F:\xampp\htdocs\captcha.php on line 23

so first check out the font location. then check out that the $_SESSION['secure'] value and if it's empty check why that session is empty. hope it helps you.

Edited: 1. I saved the code in a php file in my localhost. 2. commneted line that includes header('Content-type: image/jpeg'); cause it set the page mime-type as jpg 3. i read the notice and the warning. Anyway i think your problem is empty session value or check gd is enable in your server.

Sign up to request clarification or add additional context in comments.

2 Comments

can you please give me a walkthrough about how were you able to get the errors exactly? Also i would like you to remind you that my code works perfectly on localhost, but not on my webhost
Check out the edit and put this code above the script: ini_set('error_reporting', E_ALL); to turn on the error_reporting.
0

i guess youre missing the font on your webhosting server. try a free one, which is likeley installed on the server or just upload the font to your webhost an set the correct path to the fontfile

2 Comments

I have already uploaded the font on the host, i am now trying to define a particular path by using $_Server['DOCUMENT_ROOT'];
I made a new variable which stores the location of the font file: $font= $_SERVER['DOCUMENT_ROOT'].'/mysql/font.TTF'; and used the function as **imagettftext($image,$font_size,0,15,30,$text_color,$font,$text); ** Still didn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.