2016-07-04 89 views
3

我在我的笔记本电脑上运行Windows 10时使用了Apache 2.4服务器。我遇到了一个运行一段php代码的问题,该代码应该显示随机数字供用户使用,以便我们知道这一点用户不是机器人。下面是一个图片显示了我说的: The numbers have yellow background is what I'm referring.Apache“localhost当前无法处理此请求。” php请求

所以,我对产生这些数字的代码如下:

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

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

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

 

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

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

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

 

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

 

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

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

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

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

 
?>

当我运行这个代码在Apache上,我总是得到

“本地主机页面不工作 本地主机当前无法处理此请求 HTTP ERROR 500“

此错误。我知道这是一个服务器错误,但它使用我预先安装了Apache的Mac OX操作系统运行正常。但是,如果我使用的是Windows 10,并且按照Youtube上的指示安装了Apache和PHP,则此代码会出现上述错误。

任何人都可以帮忙吗?提前致谢!

+1

您可以发布Apache的错误日志? (只有相关位) – Pete

+0

这就是:“[:error] [pid 3972:tid 1004] [client :: 1:53340] PHP致命错误:未捕获错误:调用未定义函数imagecreate()in C:\\ apache \\ htdocs \\ captcha.php:72 \ nStack trace:\ n#0 {main} \ n在第72行引用C:\\ apache \\ htdocs \\ captcha.php,引用者:http:// localhost /“ – DawnZHANG

+0

尝试安装GD扩展。请参阅http://stackoverflow.com/questions/3106991/fatal-error-call-to-undefined-function-imagecreate。 – Pete

回答

2

这是告诉你的Apache无法找到本地错误页面500你在你的PHP代码有错误,改变display_errors为1,添加此旁边error_reporting(-1);

+0

但为什么相同的代码在我的mac上工作? – DawnZHANG

+0

我改变了代码,如你所说,浏览器说:致命错误:未捕获错误:调用未定义的函数imagecreate()在C:\ apache \ htdocs \ captcha.php:72堆栈跟踪:#0 {main}抛出C:\ apache \ htdocs \ captcha.php 72行 – DawnZHANG

+0

问题在于你没有添加模块来处理图像('imagecreate')。 [这](http://stackoverflow.com/questions/3106991/fatal-error-call-to-undefined-function-imagecreate?lq=1)是你的解决方案。请记住标记为答案。 – matiaslauriti