【发布时间】:2016-11-06 08:36:48
【问题描述】:
我在运行 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 上运行这段代码时,我总是得到 p>
“本地主机页面不工作。 localhost 当前无法处理此请求。 HTTP 错误 500"
这个错误。我知道这是一个服务器错误,但使用我预装了 Apache 的 Mac OX 操作系统运行正常。但是,如果我使用已按照 Youtube 上的说明安装了 Apache 和 PHP 的 Windows 10,此代码会出现上述错误。
谁能帮忙??提前致谢!
【问题讨论】:
-
您可以发布 Apache 错误日志吗? (仅相关位)
-
这是它:" [:error] [pid 3972:tid 1004] [client ::1:53340] PHP 致命错误:未捕获的错误:调用 C:\ 中未定义的函数 imagecreate() \apache\\htdocs\\captcha.php:72\n堆栈跟踪:\n#0 {main}\n 在第 72 行的 C:\\apache\\htdocs\\captcha.php 中抛出,引用者:localhost"
-
尝试安装 GD 扩展。见stackoverflow.com/questions/3106991/…。
-
我在我的 php.in 文件中取消了“php_gd2.dll”扩展名的注释,但它似乎没有帮助,我仍然得到同样的错误....
-
我不知道这是否适用于 Windows,但是许多 Linux Apache 安装都有多个 php.ini,请检查它们。