iT邦幫忙

1

驗證碼問題

想請問為什麼我的驗證碼會跑不出來?
https://ithelp.ithome.com.tw/upload/images/20201225/20132806DGiNYysVf2.png

<?php include 'inc/inc_set.php'; ?>
<?php
header("Content-Type: image/png");
$im = @imagecreate(110, 35) //(圖形寬度,圖形高度)
    or die("Cannot Initialize new GD image stream");

$bg_color1 = mt_rand(0, 100);
$bg_color2 = mt_rand(100, 255);    
$bg_color3 = mt_rand(0, 255);
$background_color = imagecolorallocate($im, 0, 0, 0);

$grey = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocate($im, 255, 0, 0);
$green = imagecolorallocate($im, 0, 255, 0);
$color0 = imagecolorallocate($im, 0, 0, 255);
$color1 = imagecolorallocate($im, 100, 100, 100);
$color2= imagecolorallocate($im, 0, 100, 0);
$color3 = imagecolorallocate($im, 0, 0, 100);
$color4 = imagecolorallocate($im, 100, 52, 44);
$color_arr=array($grey, $red, $green, $color1);

$rtext = mt_rand(10000, 99999);
$_SESSION[$g_proj."rand"] = $rtext;	
$font1 = '/local/htdocs'.$g_proj.'/font/DroidSans.ttf';
$font2 = '/local/htdocs'.$g_proj.'/font/UbuntuMono-R.ttf';
$font3 = '/local/htdocs'.$g_proj.'/font/XeroxSansSerifWideBoldOblique.ttf';
$font4 = '/local/htdocs'.$g_proj.'/font/XeroxSerifNarrow.ttf';
$font_arr=array($font1, $font2, $font4);

$color1=$color_arr[mt_rand(0, sizeof($color_arr)-1)];
$color2=$color_arr[mt_rand(0, sizeof($color_arr)-1)];
while ($color1==$background_color)
	$color1=$color_arr[mt_rand(0, sizeof($color_arr)-1)];
while ($color2==$color1 || $color2==$background_color)
	$color2=$color_arr[mt_rand(0, sizeof($color_arr)-1)];

$shadow_r = mt_rand(0, 8);
$shadow_r2 = mt_rand(0, 4);
imagettftext($im, 26+$shadow_r2, 0+$shadow_r, 2+$shadow_r, 28+$shadow_r, $color1, $font_arr[mt_rand(0, sizeof($font_arr)-1)], $rtext);

$shadow_rr = mt_rand(0, 8);
$shadow_rr2 = mt_rand(0, 4);
$shadow_rrr = mt_rand(-10, 10);
$shadow_rrrr = mt_rand(10, 28);

imagettftext($im, 26+$shadow_rr2, $shadow_rrr, 2+$shadow_rr, 10+$shadow_rrrr, $color2, $font_arr[mt_rand(0, sizeof($font_arr)-1)], $rtext);

for ($i = 0; $i < 5000; $i++) {
	$pink = imagecolorallocate($im, mt_rand(100,255), mt_rand(0,100), mt_rand(100, 200));
	imagesetpixel($im, mt_rand(0,200), mt_rand(0,200), $pink);
}
 

imagepng($im);
imagedestroy($im);
?>

謝謝各位,我順利解決了,後來發現是自己路徑位置寫錯!
噗~
路徑位置寫錯!!!
那理論上f12也會報路徑錯誤吧。

因為你跟j大說沒錯誤。所以就沒往那去想了。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

2
japhenchen
iT邦超人 1 級 ‧ 2020-12-25 11:31:07

你在瀏覽器上按F12,看一下那格驗証碼所帶的圖片是否載入錯誤,看一下Response裡面是否有錯誤內容

看更多先前的回應...收起先前的回應...

沒有耶

直接開啟那驗証碼圖片網址複製出來貼在瀏覽器網址看看(如果是用IMG或background-image呈現的話)

好像是圖片被CACHED了,在瀏覽器上按 SHIFT + F5 強制清理快取應該可以解決

如果可以解決,在圖片的PHP檔開頭加上

<?php

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>

呈上面的緩存。其實我驗証圖大多是直接再連結後對上時間來解決。

<img src="/getIMg?<?php echo time();?> />"
1

先把

header("Content-Type: image/png");

暫時拿掉。
然後直接跑驗証圖的程式碼。看有沒有報錯。

如果有內容出來,那就是驗証圖沒有問題。

https://ithelp.ithome.com.tw/upload/images/20201225/20132806v73ZFwpzez.png
跑出的內容長這樣

再來就是再放回去

header("Content-Type: image/png");

然後先直接連結,看圖是否正常。
如果圖不正常的話。

就得看生成的方式是否正確了。

我要發表回答

立即登入回答