我在运行Windows 10的笔记本电脑上使用Apache 2.4服务器。我在运行一段PHP代码时出现问题,该代码应该显示供用户使用的随机数字,以便我们知道此用户不是机器人。下图是我正在谈论的内容: The numbers have yellow background is what I'm referring.
因此,我生成这些数字的代码如下:
<?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&h=50&s=30&bgcolor=ffffff&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);
?>
当我在Apache上运行此代码时,我总是得到
“localhost页面无效。 localhost目前无法处理此请求。 HTTP ERROR 500“
这个错误。我知道这是一个服务器错误,但它使用我预装了Apache的Mac OX操作系统运行正常。但是,如果我使用Windows 10,并且按照Youtube上的说明安装了Apache和PHP,则此代码会出现上述错误。
任何人都可以帮忙吗?提前谢谢!
答案 0 :(得分:2)
这告诉您Apache无法找到本机错误页面500.您的PHP代码出错,将display_errors
更改为1并将其添加到error_reporting(-1);