搜尋
首頁後端開發php教程php用header('content-type: image/png')输出验证码,但响应回来的是text/html

<code><?php header("Content-type: image/png");
session_start();
$image = imagecreatetruecolor(70,30);
$bgcolor = imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$bgcolor);

// 生成干扰像素点
for ($i=0; $i < 200; $i++) {
    $pixelcolor = imagecolorallocate($image,rand(121,254),rand(121,254),rand(121,254));
    imagesetpixel($image,rand(1,69),rand(1,29),$pixelcolor);
}

    // 生成干扰直线
for ($i=0; $i < 4; $i++) {
    $linecolor = imagecolorallocate($image,rand(121,254),rand(121,254),rand(121,254));
    imageline($image,rand(0,69),rand(0,29),rand(0,69),rand(0,29),$linecolor);
}

$captch_code = "";
// 生成验证码
for ($i=0; $i < 4; $i++) {
    $fontsize = 6;
    $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
    $data = 'abcdefghijkmnpqrstuvwxy3456789';
    $fontcontent = substr($data,rand(0,strlen($data)-1),1);
    $captch_code .= $fontcontent;
    $x = ($i*70/4)+rand(5,10);
    $y = rand(5,10);
    imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$_SESSION['authcode'] = $captch_code;
imagepng($image);
imagedestroy($image);
</code></code>

上面为生成验证码的代码,在本地服务器可以生成验证码,但把代码上传到远程服务器后就无法生成验证码,返回500(intetnal server error)错误,远程服务器已开启GD库,代码开头header('content-type: image/png'),但响应回来的却是text/html。

php用header('content-type: image/png')输出验证码,但响应回来的是text/html
远程服务器返回text/html

php用header('content-type: image/png')输出验证码,但响应回来的是text/html
本地成功返回image/png

回复内容:

<code><?php header("Content-type: image/png");
session_start();
$image = imagecreatetruecolor(70,30);
$bgcolor = imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$bgcolor);

// 生成干扰像素点
for ($i=0; $i < 200; $i++) {
    $pixelcolor = imagecolorallocate($image,rand(121,254),rand(121,254),rand(121,254));
    imagesetpixel($image,rand(1,69),rand(1,29),$pixelcolor);
}

    // 生成干扰直线
for ($i=0; $i < 4; $i++) {
    $linecolor = imagecolorallocate($image,rand(121,254),rand(121,254),rand(121,254));
    imageline($image,rand(0,69),rand(0,29),rand(0,69),rand(0,29),$linecolor);
}

$captch_code = "";
// 生成验证码
for ($i=0; $i < 4; $i++) {
    $fontsize = 6;
    $fontcolor = imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));
    $data = 'abcdefghijkmnpqrstuvwxy3456789';
    $fontcontent = substr($data,rand(0,strlen($data)-1),1);
    $captch_code .= $fontcontent;
    $x = ($i*70/4)+rand(5,10);
    $y = rand(5,10);
    imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
$_SESSION['authcode'] = $captch_code;
imagepng($image);
imagedestroy($image);
</code></code>

上面为生成验证码的代码,在本地服务器可以生成验证码,但把代码上传到远程服务器后就无法生成验证码,返回500(intetnal server error)错误,远程服务器已开启GD库,代码开头header('content-type: image/png'),但响应回来的却是text/html。

php用header('content-type: image/png')输出验证码,但响应回来的是text/html
远程服务器返回text/html

php用header('content-type: image/png')输出验证码,但响应回来的是text/html
本地成功返回image/png

都500错误了你还想啥呢?

控制台点那个500看具体错误响应内容,如果没有开启错误打印

1- 远端服务器没有开启php的错误输出,此时当致命错误发生的时候直接返回web server 500错误,需要开启错误输出或查看php的错误日志获知具体错误。
2- 不应该在代码的开始处使用header('image/png');而是应该将该行移到imagepng($image);之前,否则就算有错误信息也会由于错误的头导致无法看到正确的结果。

感觉是500错误后,没有调用到header("Content-type: image/png");
因为500错误本身就要返回的是一些错误文本,因此就成text/html了

出错了,没有执行到header png这一块的逻辑

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何使PHP應用程序更快如何使PHP應用程序更快May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

PHP性能優化清單:立即提高速度PHP性能優化清單:立即提高速度May 12, 2025 am 12:07 AM

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

PHP依賴注入:提高代碼可檢驗性PHP依賴注入:提高代碼可檢驗性May 12, 2025 am 12:03 AM

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

PHP性能優化:數據庫查詢優化PHP性能優化:數據庫查詢優化May 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

簡單指南:帶有PHP腳本的電子郵件發送簡單指南:帶有PHP腳本的電子郵件發送May 12, 2025 am 12:02 AM

phpisusedforsenderemailsduetoitsbuilt-inmail()函數andsupportivelibrariesLikePhpMailerAndSwiftMailer.1)usethemail()functionForbasiceMails,butithasimails.2)butithasimail.2)

PHP性能:識別和修復瓶頸PHP性能:識別和修復瓶頸May 11, 2025 am 12:13 AM

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显著提升PHP应用的性能。

PHP的依賴注入:快速摘要PHP的依賴注入:快速摘要May 11, 2025 am 12:09 AM

依賴性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增強量強制性,可驗證性和MATIALWINABIOS.ItallowSpasspassingDepentenciesLikEdenciesLikedAbaseConnectionStoclasseconnectionStoclasseSasasasasareTers,interitationAseTestingEaseTestingEaseTestingEaseTestingEasingAndScalability。

提高PHP性能:緩存策略和技術提高PHP性能:緩存策略和技術May 11, 2025 am 12:08 AM

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具