3

I'm using Apache 2.4 server on my laptop running under Windows 10. I’m having a problem running a piece of php code which is supposed to show random numbers for users to tap in, so that we know that this user is not a robot. Below is a picture shows what I’m talking about: The numbers have yellow background is what I'm referring.

So, the code that I have for generating these number is as follows:

<?php
/**
    USAGE:
 <img alt="" rel="nofollow,noindex" width="50" height="18" src="php/captcha.php" />
 $_SESSION['captcha']  - use it in your script.

 Supported link:
  util/captcha.php?w=100&amp;h=50&amp;s=30&amp;bgcolor=ffffff&amp;txtcolor=000000
**/
 session_start();
 @ini_set('display_errors', 0);
 @ini_set('track_errors', 0);

 header('Cache-Control: no-cache');
 header('Pragma: no-cache');


/** ********************************** 
 @RANDOM GENERATORS [LN, N, L]
/** ******************************* **/
    function random($length=6,$type=null) { // null = letters+numbers, L = letters, N = numbers

  switch($type) {
   case 'N' : $chars = '0123456789';         break;
   case 'L' : $chars = 'abcdefghijklmnopqrstuvwxyz';     break;
   default  : $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';  break;
  }

  $numChars = strlen($chars); $string = '';
        for ($i = 0; $i < $length; $i++) { $string .= substr($chars, rand(1, $numChars) - 1, 1); }
  unset($chars);
  return $string; 
 }


/** ********************************** 
 @_GET shortcut and protect
/** ******************************* **/
 function _getVar($var) {
  if(isset($_GET[$var])) {
   return trim($_GET[$var]);
  } else { return null; }
 }


/** ********************************** 
 @CAPTCHA
/** ******************************* **/
 // Put the code in session to use in script
 if(_getVar('c') != '') 
  $c = (int) _getVar('c'); 
 else 
  $c = 6;
 $mycode = random($c,'N');
 
 $_SESSION['captcha'] = $mycode;

 // Image Size from a specified dimensions
 $w =  (int) _getVar('w'); if($w == '') $w = 60; // width
 $h =  (int) _getVar('h'); if($h == '') $h = 18; // height
 $s =  (int) _getVar('s'); if($s == '') $s = 5; // font size [5 max.]
 $bgcolor  = _getVar('bgcolor');  if($bgcolor == '')  $bgcolor   = 'ffffff'; // background color [ffffff default]
 $txtcolor = _getVar('txtcolor'); if($txtcolor == '') $txtcolor  = '000000'; // text color [000000 default]

 // convert color to R  G  B 
 // [from ffffff to  ff ff ff]
 $bgcol   = sscanf($bgcolor,  '%2x%2x%2x');
 $txtcol  = sscanf($txtcolor, '%2x%2x%2x');
 // Create image
 $code  = $_SESSION['captcha'];
 $image = imagecreate($w, $h);            // image size [50x18 default]
 $bgcol = imagecolorallocate($image, $bgcol[0], $bgcol[1],  $bgcol[2]);  // background color
 $txcol = imagecolorallocate($image, $txtcol[0], $txtcol[1], $txtcol[2]); // text color
 $strn2 = imagestring($image, $s, 0, 0, $code, $txcol);     // text size [4 default]
 header('Content-Type: image/jpeg');

//    imagepng($image);  // make sure --with-png-dir is set
 imagejpeg($image);
 imagedestroy($image);

?>

When I run this code on Apache, I always get

"The localhost page isn’t working. localhost is currently unable to handle this request. HTTP ERROR 500"

this error. I'm aware this is a server error but it runs OK using my Mac OX operating system which has Apache pre-installed. But this code get the above error on if I use Windows 10 which I have Apache and PHP installed as instructed on Youtube.

Can anyone help?? Thanks in advance!

DawnZHANG
  • 339
  • 2
  • 4
  • 15
  • 1
    Can you post the Apache error log? (Only the relevant bits) – Pieter van den Ham Jul 04 '16 at 15:04
  • This is it: " [:error] [pid 3972:tid 1004] [client ::1:53340] PHP Fatal error: Uncaught Error: Call to undefined function imagecreate() in C:\\apache\\htdocs\\captcha.php:72\nStack trace:\n#0 {main}\n thrown in C:\\apache\\htdocs\\captcha.php on line 72, referer: http://localhost/" – DawnZHANG Jul 04 '16 at 15:16
  • Try installing the GD extension. See http://stackoverflow.com/questions/3106991/fatal-error-call-to-undefined-function-imagecreate. – Pieter van den Ham Jul 04 '16 at 15:25
  • I have uncomment the "php_gd2.dll" extension in my php.in file, but it doesn't seem to help, I still get the same error.... – DawnZHANG Jul 04 '16 at 15:46
  • I don't know whether this applies to Windows, but many Linux Apache installs have multiple php.ini's, check them all. – Pieter van den Ham Jul 04 '16 at 15:51
  • I have two of them: "php.ini-production" and "php.in" and I've changed both of them, doesn't do the magic..... – DawnZHANG Jul 04 '16 at 15:58
  • OH, problem solved! I restart my Apache and it works! Thanks a lot! – DawnZHANG Jul 04 '16 at 16:07

1 Answers1

3

That is telling you that Apache cant find native error page 500. You have an error in your PHP code, change display_errors to 1 and add this next to that error_reporting(-1);

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • I changed the code as you said and the browser says this: Fatal error: Uncaught Error: Call to undefined function imagecreate() in C:\apache\htdocs\captcha.php:72 Stack trace: #0 {main} thrown in C:\apache\htdocs\captcha.php on line 72 – DawnZHANG Jul 04 '16 at 15:18
  • The problem is that you have not added the module to handle images (`imagecreate`). [This](http://stackoverflow.com/questions/3106991/fatal-error-call-to-undefined-function-imagecreate?lq=1) is your solution. Remember to mark this as answer. – matiaslauriti Jul 04 '16 at 15:35
  • I have uncomment the "php_gd2.dll" extension in my php.in file, but it doesn't seem to help, I still get the same error.... – DawnZHANG Jul 04 '16 at 15:49
  • You have to go to your php installation and see in the extension what name has it. I recommend you search in google. – matiaslauriti Jul 04 '16 at 15:58
  • 1
    OH, problem solved! I restart my Apache and it works! Thanks a lot! – DawnZHANG Jul 04 '16 at 16:07