search
HomeBackend DevelopmentPHP Tutorialheader跳转和file_get_contents,哪种速度快些?

header file_get_contents

不管是显示还是下载,就是比较它们的速度。我不会,请叫大家。

第一种写法:
header(url地址);
exit();

第二种写法:
echo @file_get_contents(url地址);
exit();

回复讨论(解决方案)

个人认为应该是header更快,看看我的思路:

header()  访问-输出
file_get_contents() 访问-返回数据-输出

本帖最后由 xuzuning 于 2013-02-19 17:23:37 编辑

这要看:
客户端到目标页

服务器到目标页 + 服务器到客户端

哪个快了

个人认为应该是header更快,看看我的思路:

header()  访问-输出
file_get_contents() 访问-返回数据-输出

header() 访问-跳转-输出显示
file_get_contents() 访问-返回时间-输出显示

组长让我测试一下,然后通过数据告诉他,但我迷茫啊......

引用 1 楼 kyzy_yy_pm 的回复:个人认为应该是header更快,看看我的思路:

header()  访问-输出
file_get_contents() 访问-返回数据-输出

header() 访问-跳转-输出显示
file_get_contents() 访问-返回时间-输出显示

组长让我测试一下,然后通过数据告诉他,但我迷茫啊......……

我个人认为,header() 访问- 跳转-输出显示中的跳转可以放到这里考虑吗?因为说的是跳转其实就是访问,我认为应该是这样

这要看:
客户端到目标页

客户端到服务器 + 服务器到目标页

哪个快了

我们的这样的,URL对应的文件在A服务器上,网站在B服务器上。

引用 1 楼 kyzy_yy_pm 的回复:个人认为应该是header更快,看看我的思路:

header()  访问-输出
file_get_contents() 访问-返回数据-输出

header() 访问-跳转-输出显示
file_get_contents() 访问-返回时间-输出显示

组长让我测试一下,然后通过数据告诉他,但我迷茫啊......……[


引用 3 楼 xiongmao000738 的回复:引用 1 楼 kyzy_yy_pm 的回复:个人认为应该是header更快,看看我的思路:

header()  访问-输出
file_get_contents() 访问-返回数据-输出

header() 访问-跳转-输出显示
file_get_contents() 访问-返回时间-输出显示

组长让我……
懂你的意思了,刚刚理解错误了。

在服务器上用file_get_contents获取目标页后发送给用户
相当于客户端使用代理访问目标页

至于谁快谁慢,需要实地测试

在服务器上用file_get_contents获取目标页后发送给用户
相当于客户端使用代理访问目标页

至于谁快谁慢,需要实地测试
具体应该怎么测啊?不会啊......

引用 7 楼 xuzuning 的回复:在服务器上用file_get_contents获取目标页后发送给用户
相当于客户端使用代理访问目标页

至于谁快谁慢,需要实地测试
具体应该怎么测啊?不会啊......
每种方式都循环100次看看所用时间哪个大,当然了两种测试要间隔一段时间在测试,不然会出现误差

引用 8 楼 xiongmao000738 的回复:引用 7 楼 xuzuning 的回复:在服务器上用file_get_contents获取目标页后发送给用户
相当于客户端使用代理访问目标页

至于谁快谁慢,需要实地测试
具体应该怎么测啊?不会啊......
每种方式都循环100次看看所用时间哪个大,当然了两种测试要间隔一段时间在测试,不然会出现误差
    ……
最好是重启机器或者清理内存,不然上次的测试所占用的内存影响下次测试的结果

外在因素太多,首先性质就是不一样一个站内一个站外,header访问考验两次客户端到服务器端的响应时间,file_get_contents 考验客户端到服务器 服务器到源文件服务器两个响应时间,这种东西没法具体判断,只能做分析

这个东西真没测试过,要看需求定吧。

最终是这么测试的。循环500访问同一个地址。ftpstorefile.php里面就是我写的文件下载方法。

$time1 =  time() ;
$url = 'http://192.168.84.72:9500/ftpstorefile.php?fileid=73574';
for($i=0; $i $img_file_str = file_get_contents($url);
$time2 =  time() ;

var_dump($time2 - $time1);
?>

最终得出来的结果是:header跳转需要20秒左右。file_get_contents大概8秒左右。

可能是因为文件服务器和网站服务器是局域网,所以file_get_contents的速度会快些。

你file_get_contents得到的结果没有输出,所以影响了结果的时间,你应该输出,因为不输出无法得到header类似的结果,这样是不公平的

header跳转 是在服务器端直接转向的吗 还是响应到客户端,客户端进行跳转的

根据我的理解header是 服务器响应客户端时候返回的HTTP报头,告诉客户端跳转到这个地址
也就是客户端访问了两次服务器端。
file_get_contents是服务器端读取文件内容返回,客户端只访问了一次服务器 

如果文件是在另一台服务器上的话,就是客户端访问了一次服务器,web服务器访问了一次文件服务器,同样是两次访问,但是我想服务器的速度应该比客户端快吧,包括网速

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)