2016-07-04 88 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