Php 尝试验证码时出现空白页
您好,我现在学习php,并为我的教育开发网站。我面临的问题,如果我试图添加验证码图像。我不知道问题出在哪里,但我没有使用验证码,而是得到了空白页。 我甚至从github尝试了一些已经完成的验证码,但也遇到了同样的问题,我认为问题可能出在字体上,但我不确定“这可能是我的愚蠢,我做错了什么:D”。无论如何,如果有人能帮我,那就太好了。 我尝试使用的代码: captcha.phpPhp 尝试验证码时出现空白页,php,Php,您好,我现在学习php,并为我的教育开发网站。我面临的问题,如果我试图添加验证码图像。我不知道问题出在哪里,但我没有使用验证码,而是得到了空白页。 我甚至从github尝试了一些已经完成的验证码,但也遇到了同样的问题,我认为问题可能出在字体上,但我不确定“这可能是我的愚蠢,我做错了什么:D”。无论如何,如果有人能帮我,那就太好了。 我尝试使用的代码: captcha.php <?php class captcha { private static $captcha = "__ca
<?php
class captcha {
private static $captcha = "__captcha__";
public static $font;
private static $width = 70;
private static $height = 70;
private static $font_size = 40;
private static $character_width = 40;
private static function session_exists() {
return isset($_SESSION);
}
private static function set_font() {
self::$font = self::$captcha;
$AnonymousClippings ='there is inserted chars from font you can';
self::$font = tempnam(sys_get_temp_dir(), self::$captcha);
$handle = fopen(self::$font,"w+");
fwrite($handle,base64_decode($AnonymousClippings));
fclose($handle);
return self::$font;
}
private static function get_random() {
$type = rand(0,2);
switch($type) {
case 2:
$random = chr(rand(65,90));
break;
case 1:
$random = chr(rand(97,122));
break;
default:
$random = rand(0,9);
}
return $random;
}
private static function generate_code($length) {
$code = null;
for($i = 0; $i < $length; $i++) {
$code .= self::get_random();
}
if(self::session_exists()) {
$_SESSION[self::$captcha] = $code;
}
self::$width = $length * self::$character_width;
return $code;
}
private static function get_width() {
return self::$width;
}
private static function get_height() {
return self::$height;
}
public static function image() {
$length = 6;
$code = self::generate_code($length);
self::set_font();
ob_start();
$image = imagecreatetruecolor(self::get_width(),self::get_height());
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, self::get_width(), self::get_height(), $white);
for($dot = 0; $dot < 2000; $dot++) {
$r = rand(0,255);
$g = rand(0,255);
$b = rand(0,255);
$dot_color = imagecolorallocate($image, $r, $g, $b);
$x1 = rand(0, self::get_width());
$y1 = rand(0, self::get_height());
$x2 = $x1 + 1;
$y2 = $y1 + 1;
imageline($image, $x1, $y1, $x2, $y2, $dot_color);
}
for($start = - $length; $start < 0; $start++) {
$color = imagecolorallocate($image, rand(0,177), rand(0,177), rand(0,177));
$character = substr($code, $start, 1);
$x = ($start+6) * self::$character_width;
$y = rand(self::get_height() - 20, self::get_height() - 10);
imagettftext($image, self::$font_size, 0, $x, $y, $color, self::$font, $character);
}
imagepng($image);
imagedestroy($image);
$source = ob_get_contents();
ob_end_clean();
unlink(self::$font);
return "data:image/png;base64,".base64_encode($source);
}
public static function get_code() {
if(self::session_exists()) {
return $_SESSION[self::$captcha];
}
return rand();
}
}
函数刷新验证码(目标){
var req=新的XMLHttpRequest();
请求打开(“POST”,窗口位置,真);
请求setRequestHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
req.onreadystatechange=函数(){
如果(req.readyState==4&&req.status==200){
target.src=req.responseText;
}
}
请求发送(“rCaptcha=true”);
}
PHP验证码
“onclick=“刷新验证码(此)”
title=“单击以刷新”/>
开始时,在index.php文件的开头添加这两行(在之后)开始时,在index.php文件的开头添加这两行(在之后,在你的问题中发布你的代码。该链接可以作为一个补充,但所有帮助你的必要代码都应该在问题中。空白页通常意味着发生了错误。从命令行,尝试运行php-l yourfile.php
检查可能的语法问题。我做了,并且没有错误@Paul T。好的,该链接@barmar提供的是另一种方式,您也可以查看服务器日志。日志的位置取决于您拥有的Web服务器。在您的问题中发布您的代码。该链接可以作为补充,但所有帮助您的必要代码都应该在问题中。空白页通常意味着发生了错误。从cOmand line,试着运行php-l yourfile.php
来检查可能的语法问题。我这样做了,并且没有错误@Paul T。好的,@barmar提供的链接是另一种方式,还有服务器日志,你也可以检查。日志的位置取决于你使用的Web服务器。我的坏分号在那里。当我复制代码时我没有复制分号,所以,您是否还留下任何错误消息?有时您必须使用Ctrl+u检查页面的源代码才能看到它们,因为它们隐藏在HTML中。是的,我在服务器日志中找到了它,它说:C:\\xampp\\htdocs\\mysite\\captcha.php第123行的资源暂时不可用:\\xampp\\htdocs\\mysite\\captcha.php第123行是取消链接的行(self:$font);我的坏分号在那里。当我复制代码时,我没有复制分号。那么,您是否还留下任何错误消息?有时您必须使用Ctrl+u检查页面的源代码以查看它们,因为它们隐藏在HTML中。是的,我在服务器日志中找到了它,它显示:C:\\xampp\\htdocs\\mysite\\captch中的资源暂时不可用:\\xampp\\htdocs\\mysite\\captcha、 第123行的php是取消链接的行(self:$font);
<?php
session_start();
require_once("captcha.php");
if(isset($_POST['rCaptcha'])) {
echo captcha::image();
exit;
}
else if(isset($_POST["code"])) {
if($_POST["code"] == captcha::get_code()) {
echo "Good";
}
else {
echo "Bad";
}
echo "<br/>";
}
?>
<script>
function refreshCaptcha(target) {
var req = new XMLHttpRequest();
req.open("POST", window.location, true);
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.onreadystatechange = function() {
if(req.readyState == 4 && req.status == 200) {
target.src = req.responseText;
}
}
req.send("rCaptcha=true");
}
</script>
<form method="post" autocomplete="off">
<fieldset>
<legend>PHP Captcha</legend>
<input type="text" name="code" placeholder="Captcha Code" /><br/>
<img src="<?= captcha::image() ?>" onclick="refreshCaptcha(this)"
title="click to refresh" /><br/>
<input type="submit" value="Check" /><br/>
</fieldset>
</form>
error_reporting(-1);
ini_set('display_errors', 'On');
$AnonymousClippings ='there is inserted chars from font you can'